Parallel stages and matrix

Jenkins · Advanced · 12 min

Parallelism is the main lever for cutting pipeline duration on busy Jenkins installations.

Inside a stage, parallel { } holds child stages that run at the same time, each possibly on its own agent. The Pipeline graph view shows them side by side. failFast true (or options { parallelsAlwaysFailFast() }) aborts the other branches as soon as one fails.

The matrix directive generates parallel stages from axes:

  • axes { axis { name 'OS'; values 'linux', 'windows' } axis { name 'JDK'; values '17', '21' } } gives four cells.
  • excludes { } removes combinations.
  • Inside each cell the axis values are environment variables, usable in agent { label "${OS}" } and in steps.

Each parallel branch occupies an executor while it runs, so parallelism is limited by agent capacity; ten branches on a node with two executors mostly wait in the queue. Branches share nothing unless they use the same workspace: two branches on the same node with reuseNode or the same directory can overwrite each other's files. Pass results between stages with stash and unstash, which copy files through the controller (fine for small files only; use artifacts or external storage for big ones).

Example

pipeline {
    agent none
    stages {
        stage('Checks') {
            failFast true
            parallel {
                stage('Lint') { agent any; steps { sh 'make lint' } }
                stage('Unit') { agent any; steps { sh 'make unit' } }
            }
        }
        stage('Compat') {
            matrix {
                agent { label "${PLATFORM}" }
                axes {
                    axis { name 'PLATFORM'; values 'linux', 'windows' }
                    axis { name 'JDK'; values '17', '21' }
                }
                excludes { exclude { axis { name 'PLATFORM'; values 'windows' }; axis { name 'JDK'; values '17' } } }
                stages { stage('Test') { steps { echo "Testing ${PLATFORM} JDK ${JDK}" } } }
            }
        }
    }
}

Remember

  • parallel { } runs child stages concurrently; failFast aborts siblings
  • matrix expands axes into parallel cells, with excludes
  • Parallelism is capped by available executors

Watch out for

  • Using stash for large build outputs; it streams through the controller.

Check yourself

  1. A matrix has axes with 3 and 2 values and one exclude. How many cells run?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: www.jenkins.io/doc/book/pipeline/syntax/#declarative-matrix

Parallel stages and matrix · Jenkins · Topolearn