jenkinsfile中的NotSerializableException

我正在处理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的查找操作。它返回一个java.util.regex.Matcher实例,不是Serializable。如果Jenkins在将结果存储在本地变量中后决定将脚本存储在Jenkins序列化的本地变量中,即在您获取异常之后。通过sleep(1)在调用后立即添加一个步骤,并观察是否引发了相同的异常,可以轻松地测试这一点。

要解决此问题,您应该:

  • 不将java.util.regex.Matcher结果存储在CPS转换的代码中
  • 将用法移动到带@NonCPS注释的方法中,或使用匹配运算符(==~)返回一个boolean(如果适合您的用例)

以上是 jenkinsfile中的NotSerializableException 的全部内容, 来源链接: utcz.com/qa/420033.html

回到顶部