How to make Pipeline work wait for all running parallel jobs?

I have a Groovy script as part of a Jipkins Pipeline job, as shown below:

node { stage('Testing') { build job: 'Test', parameters: [string(name: 'Name', value: 'Foo1')], quietPeriod: 2, wait: false build job: 'Test', parameters: [string(name: 'Name', value: 'Bar1')], quietPeriod: 2, wait: false build job: 'Test', parameters: [string(name: 'Name', value: 'Baz1')], quietPeriod: 2, wait: false build job: 'Test', parameters: [string(name: 'Name', value: 'Foo2')], quietPeriod: 2, wait: false build job: 'Test', parameters: [string(name: 'Name', value: 'Bar2')], quietPeriod: 2, wait: false build job: 'Test', parameters: [string(name: 'Name', value: 'Baz2')], quietPeriod: 2, wait: false } } 

which performs several other freestyle jobs in parallel, because the wait flag is set to false . However, I would like the caller's job to end when all jobs have been completed. Currently, the Pipeline task starts all the tasks and finishes it in a few seconds, which is not what I want, because I can’t keep track of the total time, and I don’t have the ability to cancel all the running tasks in one go.

How can I fix the above script to complete the Pipeline job when all parallel jobs are complete?

I tried to complete the build jobs in the waitUntil {} block, but that didn't work.

+11
source share
4 answers

You should use a parallel pipeline expression that will wait for all jobs / subtasks to complete:

 stage('testing') { def branches = [:] for(i = 0; i < params.size(); i += 1) { def param = params[i] branches["Test${i}"] = { build job: 'Test', parameters: [string(name: 'Name', value: param)], quietPeriod: 2 } } parallel branches } 

You can find a few more examples in the pipeline documentation: jenkins.io

+20
source

Just run into the same problem and find a working solution. Just use foreach.

 stage('testing') { def jobs = [:] [1,2,3,4,5].each{ i -> jobs["Test${i}"] = { build job: 'Test', parameters: [string(name: 'theparam', value: "${i}")], quietPeriod: 2 } } parallel jobs } 
+4
source

However, the @ agg3l example does not work with multiple jobs.

 Map jobResults = [:] Boolean failedJobs = false def buildJobWithParams(def jobs_list, Map results) { def branches = [:] for(job in jobs_list) { print job branches["Test-${job}"] = { def jobBuild = build job: job, propagate: false def jobResult = jobBuild.getResult() echo "Build of '${job}' returned result: ${jobResult}" results[job] = jobResult } } return branches } stage('Run integration tests') { steps { def job_branch = buildJobWithParams(item_list, jobResults) print job_branch parallel job_branch } } 

item_list has more than one assignment, however it will only perform the last assignment several times.

+3
source

This works for me. Triggers 3 jobs. Wait for them to finish. Pay attention to the extra "->" to indicate winding closures. I have one β†’ on each loop, and one on a parallel line. This means that the value will be evaluated when starting the parallel section.

 def jobsString = "job1,job2,job3" ArrayList jobsList = jobsString.split('\\,') def parallelJobs2Run = [:] jobsList.each { job -> echo "Going to parallel for job ${job}" parallelJobs2Run["${job}"] = { -> echo "Calling job ${job}" jobResults=build job: "${pathJenkinsFolder}${job}", parameters: [ string(name: 'param1', value: "${value1}"), string(name: 'param2', value: "${value2}") ], propagate: true, wait: true // List of values: https://stackoverflow.com/questions/46262862/how-to-i-get-the-url-of-build-triggered-with-build-step-on-jenkins buildNumber = ${jobResults.number} echo "${job} Build number |${buildNumber}| result: |${jobResults.result}|" echo "See details on: |${jobResults.absoluteUrl}|" } }; parallel parallelJobs2Run 
0
source

Source: https://habr.com/ru/post/1258502/


All Articles