This post helps you understand how to parallelize multiple Jenkins jobs in a single run. If you need a basic understanding of how pipeline Jenkins job works, please follow this post
Approach #1
- All the stages declared under a parallel block will be executed in parallel; let’s say,
parallel { // the below 3 stages execute at the same time stage('Parallel Test 1') { build(job: "jenkins_job_1") } stage('Parallel Test 2') { // the below jobs run sequentially one after the other build(job: "jenkins_job_1") build(job: "jenkins_job_2") build(job: "jenkins_job_3") } stage('Parallel Test 3') { echo "executing at last" } }
- The below image depicts how the multiple Jenkins jobs were triggered at the same time
- Copy the below example Jenkinsfile for practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pipeline { | |
agent any | |
stages { | |
stage('Non-Parallel Stage') { | |
steps { | |
echo "Executing this stage first" | |
} | |
} | |
stage('Parallel Stage') { | |
parallel { | |
stage('Parallel Test 1') { | |
steps { | |
echo "Here trigger job: jenkins_job_1. Triggered at time:" | |
sh(script: "date -u") | |
build(job: "jenkins_job_1") | |
} | |
} | |
stage('Parallel Test 2') { | |
steps { | |
echo "Here trigger job: jenkins_job_2. Triggered at time:" | |
sh(script: "date -u") | |
build(job: "jenkins_job_2") | |
echo "Here trigger job: jenkins_job_3. Triggered at time:" | |
sh(script: """date -u""") | |
build(job: "jenkins_job_3") | |
} | |
} | |
} | |
} | |
} | |
} |
- Blue Ocean view after completion of the pipeline job
Approach #2
In this approach, we will see how to group multiple jobs and run them in parallel (this is the only extended approach for parallelization in Jenkins pipeline project)
- Custom syntax to group multiple jobs to run in parallel
stages { stage('single run') { parallel { stage('Parallel Test 1') { steps { script { def group1 = [:] group1['test_1'] = { build(job: 'jenkins_job_1') } group1['test_2'] = { build(job: 'jenkins_job_2') } parallel group1 } } } stage('Parallel Test 2') { steps { script { def group2 = [:] group2['test_3'] = { build(job: 'jenkins_job_3') } group2['test_4'] = { build(job: 'jenkins_job_4') } parallel group2 } } } } } }
- Copy the below example Jenkinsfile for practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pipeline { | |
agent any | |
stages { | |
stage('single run') { | |
parallel { | |
stage('Parallel Test 1') { | |
steps { | |
script { | |
def group1 = [:] | |
group1["test_1"] = { | |
echo "test_1" | |
sh(script: "date -u") | |
build(job: 'jenkins_job_1') | |
} | |
group1["test_2"] = { | |
echo "test_2" | |
sh(script: "date -u") | |
build(job: 'jenkins_job_2') | |
} | |
parallel group1 | |
} | |
} | |
} | |
stage('Parallel Test 2') { | |
steps { | |
script { | |
def group2 = [:] | |
group2["test_3"] = { | |
echo "test_3" | |
sh(script: "date -u") | |
build(job: 'jenkins_job_3') | |
} | |
group2["test_4"] = { | |
echo "test_4" | |
sh(script: "date -u") | |
build(job: 'jenkins_job_4') | |
} | |
parallel group2 | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
- Blue Ocean view after completion of the pipeline job