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

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

问题描述

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

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

<小时>

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

直到我在下面添加代码 ## 此处添加时出错" 脚本才能正常运行.甚至低于该点的代码也会自行运行.但是当合并时,我得到以下错误.

我应该注意到触发的作业被调用并成功运行,但出现以下错误并且主作业失败.因此,主作业不会等待触发作业的返回.我可以尝试/抓住构建作业:但是我希望主要作业等待触发的作业完成.

有人可以帮忙吗?如果您需要更多信息,请告诉我.

干杯

def slurpJSON() {返回新的 groovy.json.JsonSlurper().parseText(BUILD_CHOICES);}节点{准备"阶段;echo '加载选项作为构建属性';def object = slurpJSON();def serverChoices = [];def serverChoicesStr = '';for(对象中的环境){envName = env.name;envServers = env.servers;for(envServers 中的服务器){如果(服务器.选择){serverChoicesStr += server.Server;serverChoicesStr += ',';}}}serverChoicesStr = serverChoicesStr[0..-2];println("服务器选择:" + serverChoicesStr);## 此处添加以下时出错舞台工作"构建作业:'Dummy Start App',参数:[[$class:'StringParameterValue',名称:'SERVER_NAME',值:'TestServer'],[$class:'StringParameterValue',名称:'SERVER_DOMAIN',值:'domain.uk'], [$class: 'StringParameterValue', name: '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(MarshallerObjectOutputStream.java:50)在 org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)在 java.io.ObjectOutputStream.writeObject(来源不明)在 java.util.LinkedHashMap.internalWriteEntries(Unknown Source)在 java.util.HashMap.writeObject(来源不明)......引起:发生的异常:在外地代表在田间关闭在对象 org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@5288c 中

解决方案

我今天自己遇到了这个问题,通过一些蛮力我已经想出了如何解决它以及可能的原因.

可能最好从原因开始:

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

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

你如何修复它:

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

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

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.

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

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