Trigger multi Jenkins jobs in parallel – Pipeline project

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
  • 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
  • Blue Ocean view after completion of the pipeline job

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s