Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Pipeline job to wait for all triggered parallel jobs?

Tags:

I've Groovy script as part of the Pipeline job in Jenkins as 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 executes multiple other freestyle jobs in parallel, because of wait flag being set to false. However I'd like for the caller job to finish when all jobs are finished. Currently the Pipeline job triggers all the jobs and finishes it-self after few seconds which is not what I want, because I cannot track the total time and I don't have ability to cancel all triggered jobs at one go.

How do I correct above script for Pipeline job to finish when all jobs in parallel are completed?

I've tried to wrap build jobs in waitUntil {} block, but it didn't work.

like image 583
kenorb Avatar asked Oct 20 '16 00:10

kenorb


People also ask

How do I run sequentially in Jenkins?

Many 'phases' can be set up as part of the MultiJob project and each phase "contains" one or more "other" Jenkins jobs. When the MultiJob project is run, the phases will be run sequentially. Therefore, in order to run N jobs sequentially, add N phases to your MultiJob project, and then add one job to each phase.


1 Answers

You should use pipeline parallel expression, which will wait for all spawned 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 some more examples in pipeline docs at jenkins.io

like image 183
agg3l Avatar answered Sep 29 '22 13:09

agg3l