尝试使用共享库并行触发所有作业

时间:2019-07-03 19:39:36

标签: jenkins-pipeline jenkins-groovy jenkins-shared-libraries

我想从另一个作业触发jenkins文件夹中的所有作业,为此,我尝试编写并使用了jenkins共享库。但是,它触发了构建,然后由于异常而失败

我的共享库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
}

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

在詹金斯的工作中,我用     runAllJobsInFolder“测试文件夹”

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

在作业日志中

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)

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

2 个答案:

答案 0 :(得分:0)

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

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

您可以在构建执行后参考此以获取更多可用属性。 How to I get the url of build triggered with build step on Jenkins?

答案 1 :(得分:0)

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

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

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

这解决了我的用例,但无法弄清楚怎么做。