Parallel Execution in Jenkins: A Practical Guide (with examples)

Updated: February 3, 2024 By: Guest Contributor Post a comment

Overview

Parallel execution in Jenkins allows developers and testers to run jobs concurrently, significantly cutting down the time required for builds and tests. This tutorial will walk you through the basics of parallel execution in Jenkins, providing practical examples to help you integrate parallelism into your continuous integration and deployment pipelines.

Understanding Parallel Execution

Before diving into the examples, it’s important to understand what parallel execution means in the context of Jenkins. Parallel execution refers to the process of running multiple jobs or stages within a pipeline simultaneously, as opposed to serial execution, where each job waits for the previous one to finish before starting.

Setting Up Jenkins for Parallel Execution

First, make sure your Jenkins installation is up to date and has the necessary plugins installed. The Pipeline plugin is essential for defining and running parallel stages in Jenkins.

Next, create a new pipeline job in Jenkins and open the pipeline script editor. Here, we’ll start inserting our code examples.

Basic Parallel Execution

pipeline {
  agent any
  stages {
    stage('Parallel Stage') {
      parallel {
        stage('Job 1') {
          steps {
            echo 'Running Job 1'
          }
        }
        stage('Job 2') {
          steps {
            echo 'Running Job 2'
          }
        }
      }
    }
  }
}

This script defines a basic parallel execution structure where two jobs, Job 1 and Job 2, are executed concurrently. The output will show both jobs running simultaneously, demonstrating the primary advantage of parallel execution.

Advanced Parallel Execution with Parameters and Conditions

pipeline {
  agent any
  stages {
    stage('Conditional Parallel Stage') {
      when {
        expression {
          params.RUN_CONDITIONAL == 'true'
        }
      }
      parallel {
        stage('Job 1') {
          steps {
            script {
              if (env.BUILD_NUMBER == '1') {
                echo 'Running Job 1 in a special mode'
              } else {
                echo 'Standard Job 1'
              }
            }
          }
        }
        stage('Job 2') {
          steps {
            echo 'Running Job 2 with additional parameters'
            script {
              def myparam = params.MY_PARAM
              echo "MY_PARAM is $myparam"
            }
          }
        }
        stage('Optional Job 3') {
          steps {
            script {
              if(params.INCLUDE_JOB3 == 'yes') {
                echo 'Including optional Job 3'
              } else {
                echo 'Skipping Job 3'
              }
            }
          }
        }
      }
    }
  }
}

This example demonstrates a more advanced use of parallel execution, incorporating conditional logic and parameters into the pipeline. It allows for dynamic job execution based on build parameters and conditions.

Reporting Results from Parallel Stages

Gathering results from parallel stages can be challenging, as each job reports its outcome individually. To aggregate results for unified reporting, you can use post-build actions and plugins such as the JUnit plugin for test reports.

Conclusion

Parallel execution in Jenkins can significantly reduce build and test times, making your CI/CD pipelines more efficient. By understanding the basics and exploring more advanced examples, you can tailor parallel execution to fit your project’s needs.