3

I wanted to trigger all jobs in a jenkins folder from another job, for this i tried writing jenkins shared library and used it. However it triggers the builds and then it fails with exception

My shared library 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
}

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

In Jenkins job, i called it using runAllJobsInFolder "testfolder"

I want to propagate the result of triggered jobs to main job

in the job log

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)

enter code here enter code here

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

2 Answers2

0

Good practice in shared library would be, use "propagate: false, wait:true" while calling build and read the return status of the build job to populate onto currentBuild.result = build_job?.result.toString()

You can also read other parameter available for the handle job_url = build_job?.absoluteUrl

You can refer this for more properties available post the build execution. How to I get the url of build triggered with build step on Jenkins?

0

I fixed it by splitting my shared library into two.

1.shared library for generating list of jobs in folder and return the list to the jenkins

2.shared library for running the list of jobs, it will take the array as argument now.

This solved my use case, But Couldn't figured it out how.