gpt4 book ai didi

json - Jenkins 管道 NotSerializableException : groovy. json.internal.LazyMap

转载 作者:IT老高 更新时间:2023-10-28 12:44:51 25 4
gpt4 key购买 nike

已解决:感谢 below answer来自 S.Richmond。我需要取消设置 groovy.json.internal.LazyMap 类型的 all 存储 map ,这意味着使变量 envServersobject使用后。

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


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

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

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

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

干杯

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

最佳答案

改用 JsonSlurperClassic

由于 Groovy 2.3(注意:Jenkins 2.7.1 使用 Groovy 2.4.7)JsonSlurper 返回 LazyMap 而不是 HashMap。这使得 JsonSlurper not 线程安全且 not 可序列化的新实现。这使得它无法在管道 DSL 脚本中的 @NonDSL 函数之外使用。

但是您可以回退到支持旧 behaviorgroovy.json.JsonSlurperClassic并且可以在管道脚本中安全使用。

示例

import groovy.json.JsonSlurperClassic 


@NonCPS
def jsonParse(def json) {
new groovy.json.JsonSlurperClassic().parseText(json)
}

node('master') {
def config = jsonParse(readFile("config.json"))

def db = config["database"]["address"]
...
}

ps。您仍然需要批准 JsonSlurperClassic 才能调用它。

关于json - Jenkins 管道 NotSerializableException : groovy. json.internal.LazyMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37864542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com