2

我想从另一个工作中触发詹金斯文件夹中的所有工作,为此我尝试编写詹金斯共享库并使用它。但是它会触发构建,然后失败并出现异常

我的共享库 runAllJobsInFolder.groovy:

def call(String foldername) {
def jobsList = []
def parallelJobs2Run = [:]

def project = Jenkins.instance.getItemByFullName(foldername)
def childItems = project.items
for (def i = 0; i < childItems.size(); i++) {
def childItem = childItems[i]
if (!childItem instanceof AbstractProject) continue;
if (childItem.fullName == foldername) continue;

jobsList.add(childItem.fullName)
 }
  for (int i = 0; i < jobsList.size(); i++) {
   def job = jobsList[i]
    echo "Going to parallel for job ${job}"
    parallelJobs2Run["${job}"] = { ->
        echo "Calling job ${job}"
        build job: "${job}",        
        propagate: true
    }
}
parallel parallelJobs2Run
}

========================================

在 Jenkins 工作中,我使用 runAllJobsInFolder "testfolder" 调用它

我想将触发作业的结果传播到主要作业

在工作日志中

Going to parallel for job testfolder/test1
[Pipeline] echo
Going to parallel for job testfolder/test2
[Pipeline] parallel
[Pipeline] [testfolder/test1] { (Branch: testfolder/test1)
[Pipeline] [testfolder/test2] { (Branch: testfolder/test2)
[Pipeline] [testfolder/test1] echo
[testfolder/test1] Calling job testfolder/test1
[Pipeline] [testfolder/test1] build (Building testFolder » test1)
[testfolder/test1] Scheduling project: testFolder » test1
[Pipeline] [testfolder/test2] echo
[testfolder/test2] Calling job testfolder/test2
[Pipeline] [testfolder/test2] build (Building testFolder » test2)
[testfolder/test2] Scheduling project: testFolder » test2
[Pipeline] [testfolder/test2] }
[testfolder/test2] Failed in branch testfolder/test2
[Pipeline] [testfolder/test1] }
[testfolder/test1] Failed in branch testfolder/test1
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] End of Pipeline

    an exception which occurred:
    in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
    in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@49674bfd
    in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
    in object com.cloudbees.groovy.cps.impl.LoopBlockScopeEnv@15e9217f
    in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
    in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@6d430606
    in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
    in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@19d10361
    in field com.cloudbees.groovy.cps.impl.CpsClosureDef.capture
    in object com.cloudbees.groovy.cps.impl.CpsClosureDef@ce48f2
    in field com.cloudbees.groovy.cps.impl.CpsClosure.def
    in object org.jenkinsci.plugins.workflow.cps.CpsClosure2@ce6143c
    in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.closures
    in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@7e809250
    in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@7e809250
Caused: java.io.NotSerializableException: com.cloudbees.hudson.plugins.folder.Folder
    at 
 org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:926)
        at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)

在此处输入代码 在此处输入代码

4

2 回答 2

0

共享库中的良好做法是,在调用 build 时使用“propagate: false, wait:true”并读取构建作业的返回状态以填充到 currentBuild.result = build_job?.result.toString()

您还可以读取可用于句柄 job_url = build_job?.absoluteUrl 的其他参数

您可以参考此内容以获取构建执行后可用的更多属性。 如何在 Jenkins 上获取构建步骤触发的构建 URL?

于 2019-07-05T13:59:37.097 回答
0

我通过将共享库分成两个来修复它。

1.shared 库,用于生成文件夹中的作业列表并将列表返回给詹金斯

2.用于运行作业列表的共享库,它现在将数组作为参数。

这解决了我的用例,但无法弄清楚如何。

于 2019-07-09T19:02:29.943 回答