0
0
Jenkinsdevops~5 mins

Parallel stages execution in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to run multiple tasks at the same time to save time. Jenkins lets you run different parts of your build or test process in parallel. This helps finish work faster by using multiple workers at once.
When you want to run tests for different parts of your app at the same time to get results quicker
When you need to build multiple versions of your software for different platforms simultaneously
When you want to deploy to multiple environments at once without waiting for one to finish
When you want to run different code quality checks in parallel to speed up feedback
When you want to run multiple independent jobs that do not depend on each other
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Parallel Example') {
      parallel {
        Build: {
          steps {
            echo 'Building the project'
            sh 'sleep 5'
          }
        },
        Test: {
          steps {
            echo 'Running tests'
            sh 'sleep 3'
          }
        },
        Lint: {
          steps {
            echo 'Checking code style'
            sh 'sleep 2'
          }
        }
      }
    }
  }
}

This Jenkinsfile defines a pipeline with one main stage called 'Parallel Example'. Inside it, three stages run at the same time: 'Build', 'Test', and 'Lint'. Each stage has simple commands that simulate work using sleep. This setup shows how Jenkins can run multiple tasks in parallel to save time.

Commands
This command updates the Jenkins job with the new Jenkinsfile that contains parallel stages. It tells Jenkins to use this pipeline definition.
Terminal
jenkins-jobs --conf jenkins.ini update Jenkinsfile
Expected OutputExpected
Job updated successfully
This command starts the Jenkins pipeline job named 'example-pipeline' and waits for it to finish. It runs the parallel stages defined in the Jenkinsfile.
Terminal
jenkins-cli build example-pipeline -s
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] stage [Pipeline] { (Parallel Example) [Pipeline] parallel [Pipeline] { (Build) Building the project [Pipeline] sh [Pipeline] } // (Build) [Pipeline] { (Test) Running tests [Pipeline] sh [Pipeline] } // (Test) [Pipeline] { (Lint) Checking code style [Pipeline] sh [Pipeline] } // (Lint) [Pipeline] } // parallel [Pipeline] } // (Parallel Example) [Pipeline] End of Pipeline Finished: SUCCESS
-s - Waits for the job to finish and shows the console output
This command fetches the console output of the last run of the 'example-pipeline' job to verify the parallel stages ran correctly.
Terminal
jenkins-cli console example-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] stage [Pipeline] { (Parallel Example) [Pipeline] parallel [Pipeline] { (Build) Building the project [Pipeline] sh [Pipeline] } // (Build) [Pipeline] { (Test) Running tests [Pipeline] sh [Pipeline] } // (Test) [Pipeline] { (Lint) Checking code style [Pipeline] sh [Pipeline] } // (Lint) [Pipeline] } // parallel [Pipeline] } // (Parallel Example) [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

If you remember nothing else from this pattern, remember: Jenkins can run multiple stages at the same time using the 'parallel' block to save time and speed up your pipeline.

Common Mistakes
Placing 'parallel' outside of a 'stage' block
Jenkins requires 'parallel' to be inside a stage; otherwise, the pipeline syntax is invalid and will fail to run.
Always put the 'parallel' block inside a named 'stage' block in your Jenkinsfile.
Using the same stage name multiple times inside parallel
Stage names must be unique; duplicate names cause Jenkins to throw an error or overwrite stages.
Give each parallel stage a unique name to avoid conflicts.
Running long blocking commands without proper timeout
If a parallel stage hangs, it can block the entire pipeline and waste resources.
Use timeouts or proper error handling inside each parallel stage to avoid indefinite waits.
Summary
Define parallel stages inside a 'parallel' block within a Jenkins pipeline stage.
Use the Jenkins CLI to update the job and run the pipeline to see parallel execution.
Check the console output to verify that stages ran simultaneously and completed successfully.