Jenkins管道NotSerializableException:groovy.json.internal.LazyMap [英] Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap

查看:1862
本文介绍了Jenkins管道NotSerializableException:groovy.json.internal.LazyMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决:感谢S.Richmond的下面的答案。我需要取消设置 groovy.json.internal.LazyMap 类型的所有存储的映射,这意味着取消变量 envServers 对象使用后。



其他:搜索的人员对于这个错误可能有兴趣使用Jenkins管道步骤 readJSON 改为 - 查找更多信息 here




我正在尝试使用Jenkins Pipeline从作为json字符串传递给作业的用户接收输入。管道然后使用slurper解析这个,我挑选出重要的信息。然后它将使用这些信息多次运行1个作业,同时使用不同的作业参数。



直到我在>##错误时,在这里下面添加脚本将运行良好。即使低于该点的代码也会自行运行。但是如果合并,我会得到下面的错误。

我应该注意到,触发的作业被调用并且成功运行,但发生下面的错误并且主作业失败。正因为如此,主要工作并不等待被触发工作的回归。我可以尝试/捕捉构建作业:但是我希望主作业等待触发作业完成。



任何人都可以在这里协助吗?如果您需要更多信息,请告诉我。



干杯

  def slurpJSON(){
返回新的groovy.json.JsonSlurper()。parseText(BUILD_CHOICES);
}

节点{
stage'Prepare';
echo'将选项加载为构建属性';
def object = slurpJSON();

def serverChoices = [];
def serverChoicesStr ='';

(env in object){
envName = env.name;
envServers = env.servers; (服务器在envServers中)
$ b $ {
if(server.Select){
serverChoicesStr + = server.Server;
serverChoicesStr + =',';
}
}
}
serverChoicesStr = serverChoicesStr [0 ..- 2];

println(Server choices:+ serverChoicesStr);

##在下面添加

时出错'Jobs'
build job:'Dummy Start App',参数:[[$ class:'StringParameterValue '名称: 'SERVER_NAME',值: 'TESTSERVER'],[$类: 'StringParameterValue',名称: 'SERVER_DOMAIN',值: 'domain.uk'],[$类: 'StringParameterValue',名称:' APP ',value:'application1']]

}

错误:

  java.io.NotSerializableException:groovy.json.internal.LazyMap在org.jboss.marshalling.river.RiverMarshaller 
.doWriteObject(RiverMarshaller.java:860)
在org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:569)
在org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller的.java:65)在org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56

在org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(三月shallerObjectOutputStream.java:50)
在org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
在java.io.ObjectOutputStream.writeObject(来源不明)
。在java.util.LinkedHashMap.internalWriteEntries(未知源)
在java.util.HashMap.writeObject(未知源)
...
...
引起:异常发生的情况:
在字段委托
在字段中关闭
在对象中org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@5288c


解决方案

我今天自己遇到了这个问题,并且通过一些暴力手段,我找出了解决方法和潜在原因。 / p>

可能最好从以下原因开始:



Jenkins有一个范例,所有的工作都可以被中断,暂停和通过服务器重启可恢复。要实现这一点,管道及其数据必须完全可序列化 - IE需要能够保存所有事情的状态。同样,它需要能够序列化构建中的节点和子作业之间的全局变量的状态,这是我认为正在发生的事情,以及为什么只有在添加额外的构建步骤时才会发生。 p>

无论什么原因,JSONObject的默认情况下都不是可序列化的。我不是一个Java开发人员,所以我不能就这个话题说更多的话。虽然我不知道他们对Groovy和Jenkins的适用性,但有很多关于如何正确解决这个问题的答案。 看到这个帖子了解更多信息。



你如何修正它:



如果您知道如何,您可以以某种方式使JSONObject可序列化。否则,你可以通过确保没有全局变量是这种类型来解决它。



尝试取消设置对象 var或将其封装在方法中,以使其范围不是节点全局。 p>

Solved: Thanks to below answer from S.Richmond. I needed to unset all stored maps of the groovy.json.internal.LazyMap type which meant nullifying the variables envServers and object after use.

Additional: People searching for this error might be interested to use the Jenkins pipeline step readJSON instead - find more info here.


I am trying to use Jenkins Pipeline to take input from the user which is passed to the job as json string. Pipeline then parses this using the slurper and I pick out the important information. It will then use that information to run 1 job multiple times in parallel with differeing job parameters.

Up until I add the code below "## Error when below here is added" the script will run fine. Even the code below that point will run on its own. But when combined I get the below error.

I should note that the triggered job is called and does run succesfully but the below error occurs and fails the main job. Because of this the main job does not wait for the return of the triggered job. I could try/catch around the build job: however I want the main job to wait for the triggered job to finish.

Can anyone assist here? If you need anymore information let me know.

Cheers

def slurpJSON() {
return new groovy.json.JsonSlurper().parseText(BUILD_CHOICES);
}

node {
  stage 'Prepare';
  echo 'Loading choices as build properties';
  def object = slurpJSON();

  def serverChoices = [];
  def serverChoicesStr = '';

  for (env in object) {
     envName = env.name;
     envServers = env.servers;

     for (server in envServers) {
        if (server.Select) {
            serverChoicesStr += server.Server;
            serverChoicesStr += ',';
        }
     }
  }
  serverChoicesStr = serverChoicesStr[0..-2];

  println("Server choices: " + serverChoicesStr);

  ## Error when below here is added

  stage 'Jobs'
  build job: 'Dummy Start App', parameters: [[$class: 'StringParameterValue', name: 'SERVER_NAME', value: 'TestServer'], [$class: 'StringParameterValue', name: 'SERVER_DOMAIN', value: 'domain.uk'], [$class: 'StringParameterValue', name: 'APP', value: 'application1']]

}

Error:

java.io.NotSerializableException: groovy.json.internal.LazyMap
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:569)
    at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
    at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
    at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
    at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at java.util.LinkedHashMap.internalWriteEntries(Unknown Source)
    at java.util.HashMap.writeObject(Unknown Source)
...
...
Caused by: an exception which occurred:
    in field delegate
    in field closures
    in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@5288c

解决方案

I ran into this myself today and through some bruteforce I've figured out both how to resolve it and potentially why.

Probably best to start with the why:

Jenkins has a paradigm where all jobs can be interrupted, paused and resumable through server reboots. To achieve this the pipeline and its data must be fully serializable - IE it needs to be able to save the state of everything. Similarly, it needs to be able to serialize the state of global variables between nodes and sub-jobs in the build, which is what I think is happening for you and I and why it only occurs if you add that additional build step.

For whatever reason JSONObject's aren't serializable by default. I'm not a Java dev so I cannot say much more on the topic sadly. There are plenty of answers out there about how one may fix this properly though I do not know how applicable they are to Groovy and Jenkins. See this post for a little more info.

How you fix it:

If you know how, you can possibly make the JSONObject serializable somehow. Otherwise you can resolve it by ensuring no global variables are of that type.

Try unsetting your object var or wrapping it in a method so its scope isn't node global.

这篇关于Jenkins管道NotSerializableException:groovy.json.internal.LazyMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆