Jenkins pipeline to create and trigger jobs dynamically inside a loop
There is a requirement from the customer, to build a pipeline, which will be executed to call 100 remote entry points. Of course, we have a classic approach to do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
stage('aa_11'){ steps{ build job: 'Tasks/pipeline_to_call_remote_api', wait: true, parameters: [ string(name: 'PARAM1', value: 'aa'), string(name: 'PARAM2', value: '11') ] } } stage('aa_22'){ steps{ build job: 'Tasks/pipeline_to_call_remote_api', wait: true, parameters: [ string(name: 'PARAM1', value: 'aa'), string(name: 'PARAM2', value: '22') ] } } ....... |
Is there any other approach to create stages dynamically and execute them sequentially inside a loop? Yes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
stage('Init data'){ steps{ script { list = ["aa_11", "aa_22", "aa_33", ......."cc_11", "dd_22"] } } post { cleanup { cleanWs() } } } stage('Dynamic Stages') { steps { script { for(int i=0; i < list.size(); i++) { def t = list[i].split('_')[0] def p = list[i].split('_')[1] stage("Sync ${list[i]}"){ echo "Sync ${list[i]}" //steps{ //DO NOT use steps here //Even stage fails here, but pipeline will continue the execution. catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') { build job: 'Tasks/pipeline_to_call_remote_api', wait: true, parameters: [ string(name: 'PARAM1', value: t), string(name: 'PARAM2', value: p) ] } //} } } } } post { cleanup { cleanWs() } } } |
Use catchError to let the pipeline continue building even some stages fail.
1 |
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {} |
Bingo!
Reference: