Jenkins 管道中的 sh 命令在有和没有@NonCps 的情况下都失败
sh command in Jenkins pipeline fails with and without @NonCps
我正在尝试从 git 变更日志中检索到的数据中删除 Jenkins 中的作业。
我不能使用 @NonCPS 函数,因为我正在使用 sh 启动命令,我得到
expected to call WorkflowScript.deleteJob but wound up catching sh; see: https://jenkins.io/redirect/pipeline-cps-method-mismatches/
当我按照上面 link 中的建议删除 @NonCPS 时,我得到
java.io.NotSerializableException: org.jenkinsci.plugins.workflow.job.WorkflowJob
这是触发错误的代码(在从 Whosebug 内外查找各种答案后尝试的众多其他版本中):
// jobName computed by iterating on changeLogSets[i].items
stage ("delete job $jobName") {
println("before compute curl_command")
curl_command = "curl -X POST '$JENKINS_URL/job/$jobName/doDelete' -u user:secret"
println("after compute curl_command")
deleteJob(curl_command)
println("after deleteJob(curl_command)")
}
}
if (jobExists(jobName)) {
echo "inside try"
// echo "curl -X POST '$JENKINS_URL/job/$jobName/doDelete' -u fvila:11ae4ca260dac6d997db028c0cc8b26e29"
// sh "curl -X POST '$JENKINS_URL/job/$jobName/doDelete' -u fvila:11ae4ca260dac6d997db028c0cc8b26e29"
stage ("delete job $jobName") {
deleteJob(jobName)
}
}
echo "after doDelete"
// @NonCPS
def deleteJob(curlCommand) {
println("enter deleteJob(curlCommand)")
def output = sh curlCommand
// alternative :
sh curlCommand
println("exit deleteJob(curlCommand)")
}
def jobExists(jobName) {
println("enter jobExists, jobName=$jobName")
ret = jenkins.model.Jenkins.instance.getItem(jobName)
println("exit jobExists, ret=$ret")
return ret
}
这里是没有@NonCPS 的更完整的错误副本
an exception which occurred:
in field groovy.lang.Closure.delegate
in object org.jenkinsci.plugins.workflow.cps.CpsClosure2@64d02d43
in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.closures
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@6616c092
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@6616c092
Caused: java.io.NotSerializableException: org.jenkinsci.plugins.workflow.job.WorkflowJob
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:274)
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.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:345)
at java.base/java.util.LinkedHashMap.internalWriteEntries(LinkedHashMap.java:333)
at java.base/java.util.HashMap.writeObject(HashMap.java:1412)
at org.jboss.marshalling.reflect.JDKSpecific$SerMethods.callWriteObject(JDKSpecific.java:89)
at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:199)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1089)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1080)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:268)
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.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:345)
at com.cloudbees.groovy.cps.SerializableScript.writeObject(SerializableScript.java:26)
at org.jboss.marshalling.reflect.JDKSpecific$SerMethods.callWriteObject(JDKSpecific.java:89)
at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:199)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1089)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1080)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1080)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:268)
at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1143)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1101)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1080)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1080)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:268)
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.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:345)
at java.base/java.util.HashMap.internalWriteEntries(HashMap.java:1858)
at java.base/java.util.HashMap.writeObject(HashMap.java:1412)
at org.jboss.marshalling.reflect.JDKSpecific$SerMethods.callWriteObject(JDKSpecific.java:89)
at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:199)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1089)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:268)
at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1143)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1101)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:268)
at org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:58)
at org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:111)
at org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverWriter.lambda$writeObject(RiverWriter.java:144)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:237)
at org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverWriter.writeObject(RiverWriter.java:143)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:559)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:536)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgramIfPossible(CpsThreadGroup.java:519)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:443)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access0(CpsThreadGroup.java:96)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:314)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:278)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:67)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:139)
at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService.run(ImpersonatingExecutorService.java:68)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
实际问题是方法“def jobExists(jobName)”,return它是一个作业实例,它是不可序列化的。
所以这个方法应该显式 return 布尔值,这将解决你的问题
boolean jobExists(jobName) {
这意味着使用 Groovy 真相正确地将作业强制转换为 true 或 false,这对您的情况来说已经足够了。
我正在尝试从 git 变更日志中检索到的数据中删除 Jenkins 中的作业。 我不能使用 @NonCPS 函数,因为我正在使用 sh 启动命令,我得到
expected to call WorkflowScript.deleteJob but wound up catching sh; see: https://jenkins.io/redirect/pipeline-cps-method-mismatches/
当我按照上面 link 中的建议删除 @NonCPS 时,我得到
java.io.NotSerializableException: org.jenkinsci.plugins.workflow.job.WorkflowJob
这是触发错误的代码(在从 Whosebug 内外查找各种答案后尝试的众多其他版本中):
// jobName computed by iterating on changeLogSets[i].items
stage ("delete job $jobName") {
println("before compute curl_command")
curl_command = "curl -X POST '$JENKINS_URL/job/$jobName/doDelete' -u user:secret"
println("after compute curl_command")
deleteJob(curl_command)
println("after deleteJob(curl_command)")
}
}
if (jobExists(jobName)) {
echo "inside try"
// echo "curl -X POST '$JENKINS_URL/job/$jobName/doDelete' -u fvila:11ae4ca260dac6d997db028c0cc8b26e29"
// sh "curl -X POST '$JENKINS_URL/job/$jobName/doDelete' -u fvila:11ae4ca260dac6d997db028c0cc8b26e29"
stage ("delete job $jobName") {
deleteJob(jobName)
}
}
echo "after doDelete"
// @NonCPS
def deleteJob(curlCommand) {
println("enter deleteJob(curlCommand)")
def output = sh curlCommand
// alternative :
sh curlCommand
println("exit deleteJob(curlCommand)")
}
def jobExists(jobName) {
println("enter jobExists, jobName=$jobName")
ret = jenkins.model.Jenkins.instance.getItem(jobName)
println("exit jobExists, ret=$ret")
return ret
}
这里是没有@NonCPS 的更完整的错误副本
an exception which occurred:
in field groovy.lang.Closure.delegate
in object org.jenkinsci.plugins.workflow.cps.CpsClosure2@64d02d43
in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.closures
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@6616c092
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@6616c092
Caused: java.io.NotSerializableException: org.jenkinsci.plugins.workflow.job.WorkflowJob
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:274)
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.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:345)
at java.base/java.util.LinkedHashMap.internalWriteEntries(LinkedHashMap.java:333)
at java.base/java.util.HashMap.writeObject(HashMap.java:1412)
at org.jboss.marshalling.reflect.JDKSpecific$SerMethods.callWriteObject(JDKSpecific.java:89)
at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:199)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1089)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1080)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:268)
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.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:345)
at com.cloudbees.groovy.cps.SerializableScript.writeObject(SerializableScript.java:26)
at org.jboss.marshalling.reflect.JDKSpecific$SerMethods.callWriteObject(JDKSpecific.java:89)
at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:199)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1089)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1080)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1080)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:268)
at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1143)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1101)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1080)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1080)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:268)
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.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:345)
at java.base/java.util.HashMap.internalWriteEntries(HashMap.java:1858)
at java.base/java.util.HashMap.writeObject(HashMap.java:1412)
at org.jboss.marshalling.reflect.JDKSpecific$SerMethods.callWriteObject(JDKSpecific.java:89)
at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:199)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1089)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:268)
at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1143)
at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:1101)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:268)
at org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:58)
at org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:111)
at org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverWriter.lambda$writeObject(RiverWriter.java:144)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:237)
at org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverWriter.writeObject(RiverWriter.java:143)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:559)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:536)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgramIfPossible(CpsThreadGroup.java:519)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:443)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access0(CpsThreadGroup.java:96)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:314)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:278)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:67)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:139)
at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService.run(ImpersonatingExecutorService.java:68)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
实际问题是方法“def jobExists(jobName)”,return它是一个作业实例,它是不可序列化的。
所以这个方法应该显式 return 布尔值,这将解决你的问题
boolean jobExists(jobName) {
这意味着使用 Groovy 真相正确地将作业强制转换为 true 或 false,这对您的情况来说已经足够了。