gpt4 book ai didi

jenkins - jenkinsfile 中的 NotSerializableException

转载 作者:行者123 更新时间:2023-12-05 00:12:23 28 4
gpt4 key购买 nike

我正在处理 jenkinsfile 并且在第三阶段遇到异常:

an exception which occurred:
in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@7bbae4fb
in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
in object com.cloudbees.groovy.cps.impl.CaseEnv@6896a2e3
in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@605ccbbc
in field com.cloudbees.groovy.cps.impl.CallEnv.caller
in object com.cloudbees.groovy.cps.impl.FunctionCallEnv@7b8ef914
in field com.cloudbees.groovy.cps.Continuable.e
in object org.jenkinsci.plugins.workflow.cps.SandboxContinuable@11e73f3c
in field org.jenkinsci.plugins.workflow.cps.CpsThread.program
in object org.jenkinsci.plugins.workflow.cps.CpsThread@b2df9bb
in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.threads
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@2b30596a
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@2b30596a
Caused: java.io.NotSerializableException: java.util.regex.Matcher
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860)
at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)

我一直在阅读它,我知道我无法创建不可序列化的变量。所以,我认为它必须与我的代码的这一部分有关:
def artifact_name = sh (
script: "ls -b *.jar | head -1",
returnStdout: true
).trim()
def has_snapshot = artifact_name =~ /-TEST\.jar/
if (has_snapshot) {
//Do something
}

我的问题是,如何定义这两个变量以避免该异常?

最佳答案

你的问题是这一行:

def has_snapshot = artifact_name =~ /-TEST\.jar/
=~Groovy find operator .它返回一个 java.util.regex.Matcher 实例,这不是 Serializable .如果 Jenkins 决定在您将结果存储在由 Jenkins 序列化的局部变量中后暂停您的脚本,这就是您收到异常的时候。这可以通过立即添加 sleep(1) 来轻松测试。在您的调用之后执行步骤并观察抛出相同的异常。

要解决此问题,您应该:
  • 不存储 java.util.regex.Matcher导致 CPS 转换代码
  • 将用法移动到 @NonCPS注释方法或使用 match operator ( ==~ ) which returns a boolean (如果它适合您的用例)
  • 关于jenkins - jenkinsfile 中的 NotSerializableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50855961/

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