Jenkins 管道中的 sh 命令在使用和不使用 @NonCps 的情况下都会失败

发布于 2025-01-19 04:53:42 字数 8415 浏览 0 评论 0原文

我正在尝试从Git ChangElog检索的数据中删除Jenkins的工作。 我无法使用@noncps函数,因为我正在使用sh启动命令,并且

expected to call WorkflowScript.deleteJob but wound up catching sh; see: https://jenkins.io/redirect/pipeline-cps-method-mismatches/

在删除@noncps时得到了,如上所述,我得到

java.io.notserializableException:org:org .jenkinsci.plugins.workflow.job.workflowjob

以下是触发错误的代码(在在stackoverflow中查找各种答案之后,尝试了许多其他版本中的代码):

// 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$1(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.access$400(CpsThreadGroup.java:96)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:314)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:278)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.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)

I'm trying to delete a job in Jenkins from data retrieved from the git changelog.
I can't use a @NonCPS function, because I'm using sh to launch the command, and I get

expected to call WorkflowScript.deleteJob but wound up catching sh; see: https://jenkins.io/redirect/pipeline-cps-method-mismatches/

When I remove the @NonCPS, as suggested in the link above, I get

java.io.NotSerializableException: org.jenkinsci.plugins.workflow.job.WorkflowJob

Here is the code that triggers the error (out of numerous other versions tried after looking up various answers in and out of StackOverflow):

// 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
}

here is a fuller copy of the error without @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$1(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.access$400(CpsThreadGroup.java:96)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:314)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:278)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尘曦 2025-01-26 04:53:42

实际的问题是方法“def jobExists(jobName)”,它返回一个不可序列化的作业实例。

因此,此方法应该显式返回布尔值,这将解决您的问题

boolean jobExists(jobName) {

这意味着使用 Groovy Truth 可以将作业正确强制为 true 或 false,这对于您的情况来说已经足够了。

The actual problem is the method "def jobExists(jobName)", which returns a job isntance, which is not serializable.

So this method should explicitly return boolean, which would fix your problem

boolean jobExists(jobName) {

This means that the job is correctly coerced into true or false using Groovy Truth, which is sufficient for your case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文