Sling Academy
Home/DevOps/Parallel Execution in Jenkins: A Practical Guide (with examples)

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

Last updated: February 03, 2024

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.

Next Article: How to create a shared library in Jenkins

Previous Article: When NOT to use Jenkins: Exploring Alternatives

Series: Jenkins Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide