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

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

问题描述

已解决:感谢 S.Richmond 的以下答案.我需要取消设置 groovy.json.internal.LazyMap 类型的 all 存储地图,这意味着使变量 envServersobject使用后.

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.

附加:搜索此错误的人可能有兴趣改用 Jenkins 管道步骤 readJSON - 查找更多信息 这里.

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

我正在尝试使用 Jenkins Pipeline 从用户那里获取作为 json 字符串传递给作业的输入.然后 Pipeline 使用 slurper 解析它,然后我挑选出重要信息.然后它将使用该信息以不同的作业参数并行运行 1 个作业.

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.

直到我在下面添加代码 "## Error when below here is added" 脚本将运行良好.甚至低于该点的代码也将自行运行.但是当结合我得到以下错误.

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.

我应该注意到触发的作业被调用并且确实运行成功但是发生了以下错误并且使主作业失败.因此,主作业不会等待触发作业的返回.我可以尝试/捕捉 build 作业: 但是我希望主要作业等待触发的作业完成.

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.

干杯

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']]

}

错误:

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.

最好从原因开始:

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

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.

无论出于何种原因,默认情况下 JSONObject 是不可序列化的.我不是 Java 开发人员,所以遗憾的是我不能就这个话题说更多.尽管我不知道它们对 Groovy 和 Jenkins 的适用性如何,但有很多关于如何正确解决此问题的答案.查看这篇文章了解更多信息.

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.

你如何解决它:

如果你知道怎么做,你可能会以某种方式使 JSONObject 可序列化.否则,您可以通过确保没有全局变量属于该类型来解决它.

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.

尝试取消设置您的 object var 或将其包装在一个方法中,使其范围不是节点全局.

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

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

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