</>

Technology

Jenkins

Difficulty

Intermediate

Interview Question

How do you run parallel stages in a Jenkins pipeline?

Answer

Parallel Stages in Jenkins Pipeline

Parallel execution reduces total build time by running independent stages simultaneously.

Basic Parallel Syntax

GROOVY
pipeline {
    agent none

    stages {
        stage('Build') {
            agent any
            steps {
                sh 'mvn clean package -DskipTests'
                stash name: 'build-artifacts', includes: 'target/*.jar'
            }
        }

        stage('Run Tests in Parallel') {
            parallel {

                stage('Smoke Tests — Chrome') {
                    agent { label 'agent-chrome' }
                    steps {
                        unstash 'build-artifacts'
                        sh 'mvn test -Dsuite=smoke -Dbrowser=chrome'
                    }
                    post {
                        always {
                            junit 'target/surefire-reports/*.xml'
                        }
                    }
                }

                stage('Regression Tests — Firefox') {
                    agent { label 'agent-firefox' }
                    steps {
                        unstash 'build-artifacts'
                        sh 'mvn test -Dsuite=regression -Dbrowser=firefox'
                    }
                    post {
                        always {
                            junit 'target/surefire-reports/*.xml'
                        }
                    }
                }

                stage('API Tests') {
                    agent { label 'agent-api' }
                    steps {
                        sh 'mvn test -Dsuite=api-tests'
                    }
                    post {
                        always {
                            junit 'target/surefire-reports/*.xml'
                        }
                    }
                }

            } // end parallel
        }

        stage('Deploy') {
            agent any
            when {
                allOf {
                    branch 'main'
                    expression { currentBuild.result == null || currentBuild.result == 'SUCCESS' }
                }
            }
            steps {
                sh './deploy.sh staging'
            }
        }
    }
}

Matrix Strategy (Run Same Stage Across Multiple Configurations)

GROOVY
pipeline {
    agent none

    stages {
        stage('Cross-Browser Tests') {
            matrix {
                axes {
                    axis {
                        name   'BROWSER'
                        values 'chrome', 'firefox', 'edge'
                    }
                    axis {
                        name   'ENV'
                        values 'staging', 'qa'
                    }
                }
                // Generates 3 × 2 = 6 parallel combinations!

                stages {
                    stage('Test') {
                        agent { label "${BROWSER}-agent" }
                        steps {
                            sh "mvn test -Dbrowser=${BROWSER} -Denv=${ENV}"
                        }
                        post {
                            always {
                                junit 'target/surefire-reports/*.xml'
                            }
                        }
                    }
                }
            }
        }
    }
}

Sharing Artifacts Between Parallel Stages

GROOVY
stage('Build') {
    steps {
        sh 'mvn clean package'
        // stash saves files to pass to another agent
        stash name: 'test-classes', includes: 'target/**'
    }
}

stage('Parallel Tests') {
    parallel {
        stage('Suite A') {
            agent { label 'agent-1' }
            steps {
                unstash 'test-classes'  // retrieve stashed files
                sh 'mvn test -Dsuite=suite-a'
            }
        }
        stage('Suite B') {
            agent { label 'agent-2' }
            steps {
                unstash 'test-classes'
                sh 'mvn test -Dsuite=suite-b'
            }
        }
    }
}

failFast — Stop All Parallel Stages on First Failure

GROOVY
stage('Parallel Tests') {
    failFast true   // if any parallel branch fails, cancel all others
    parallel {
        stage('Suite A') { ... }
        stage('Suite B') { ... }
    }
}

Follow AutomateQA

Related Topics