jenkinsfile中的NotSerializableException [英] NotSerializableException in jenkinsfile

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

问题描述

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

I'm working on a jenkinsfile and I'm getting and exception in the third stage:

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)

我一直在阅读它,我知道我无法创建不可序列化的变量.因此,我认为这必须与我的代码的这一部分有关:

I've been reading about it and I know I can't create non-serializable variables. So, I think it has to be with this part of my code:

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
}

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

My question is, how do I define that two variables in order to avoid that exception?

推荐答案

您的问题是这一行:

def has_snapshot = artifact_name =~ /-TEST\.jar/

=~ Groovy查找运算符.它返回一个 java.util.regex.Matcher 实例,它不是 Serializable .如果将结果存储在由Jenkins序列化的本地变量(即在您获得异常时)之后,Jenkins决定暂停脚本.通过在调用后立即添加sleep(1)步骤并观察是否引发了相同的异常,可以轻松地对此进行测试.

The =~ is the Groovy find operator. It returns a java.util.regex.Matcher instance, which is not Serializable. If Jenkins decides to pause your script after you have stored the result in a local variable that is serialized by Jenkins that is when you get the exception. This can be easily tested by immediately adding a sleep(1) step after your invocation and watch as that same exception is thrown.

要解决此问题,您应该:

To resolve this, you should :

  • 不将java.util.regex.Matcher结果存储在CPS转换后的代码中
  • 将用法移至带有@NonCPS注释的方法中,或使用匹配运算符(==~)返回boolean (如果适合您的用例)
  • Not store the java.util.regex.Matcher result in CPS transformed code
  • Move the usage into a @NonCPS annotated method or use the match operator (==~) which returns a boolean (if it fits your use case)

这篇关于jenkinsfile中的NotSerializableException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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