Jenkins Parallel Stages, Matrix Builds, Stash and Fail-Fast for Faster Pipelines

CI CD Pipelines with Jenkins 35 min min read Updated: Nov 17, 2026 Intermediate
Jenkins Parallel Stages, Matrix Builds, Stash and Fail-Fast for Faster Pipelines
Intermediate Topic 3 of 5

Why Parallel?

A pipeline that runs unit tests (6 min), UI tests (8 min) and a security scan (5 min) one after another takes 19 minutes. If the three are independent, they can run at the same time in about 8 minutes. Faster feedback means developers fix problems while they still remember the change.

Parallel Stages

groovy pipeline { agent none stages { stage('Build') { agent { label 'linux' } steps { sh 'mvn -B -DskipTests package' stash name: 'app', includes: 'target/*.jar' } } stage('Checks') { failFast true // stop the others when one fails parallel { stage('Unit tests') { agent { label 'linux' } steps { sh 'mvn -B test' junit 'target/surefire-reports/*.xml' } } stage('Integration tests') { agent { label 'linux && docker' } steps { unstash 'app' sh './run-integration.sh' } } stage('Security scan') { agent { docker { image 'aquasec/trivy:latest'; args '--entrypoint=' } } steps { sh 'trivy fs --exit-code 1 --severity HIGH,CRITICAL .' } } } } stage('Deploy') { agent { label 'linux' } steps { unstash 'app' sh './deploy.sh' } } } }
  • parallel is inside a stage and contains named stages, each can have its own agent
  • failFast true aborts the other branches as soon as one fails. Or set parallelsAlwaysFailFast() in options
  • The parallel branches share the build, but each branch may use a different workspace when it uses a different agent
  • Use agent none at the top and give agents per stage, so executors are used only when needed

stash and unstash

Each agent has its own workspace. To pass files from one agent or stage to another, stash them and unstash them later. Stashes live on the controller for the duration of the build, so they are meant for small to medium files (use an artifact repository for big ones).

groovy stash name: 'binaries', includes: 'build/**', excludes: '**/*.log', allowEmpty: false // later, maybe on another agent unstash 'binaries'
Use stash forDo not use stash for
Compiled output needed by test stages on other agentsLarge files (hundreds of MB): use Nexus, Artifactory or S3
Test scripts and small data setsFiles needed after the build ends: use archiveArtifacts

Matrix: Test Many Combinations

groovy pipeline { agent none stages { stage('Test matrix') { 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 on ${PLATFORM} with JDK ${JDK}" sh 'echo run-tests' } } } } } } }
  • Jenkins creates one parallel cell for every combination of axis values (here 3 after the exclude)
  • Inside the cells, the axis names become environment variables
  • Matrix is excellent for testing a library on several runtimes or operating systems
  • Use when inside a matrix, failFast and excludes to keep the number of cells reasonable

Parallel in Scripted Style (for Dynamic Lists)

When the list of tasks is not known until run time (for example one test group per folder), build a map in a script block.

groovy pipeline { agent any stages { stage('Dynamic tests') { steps { script { def groups = ['api', 'web', 'db', 'batch'] def branches = [:] groups.each { g -> branches["tests-${g}"] = { node('linux') { checkout scm sh "./run-tests.sh ${g}" } } } branches.failFast = true parallel branches } } } } }

Post Conditions in Parallel

groovy stage('Unit tests') { agent any steps { sh 'mvn test' } post { always { junit 'target/surefire-reports/*.xml' } // each branch reports its own results } }

Reduce Pipeline Time: Checklist

IdeaHow
Run independent work in parallelparallel, matrix
Skip unneeded workwhen { changeset }, skip tests if only docs changed
Cache dependenciesPersistent agent caches, Docker layer cache, ~/.m2 volume, artifact proxy such as Nexus
Use faster agentsMore CPU and fast disks for build stages
Avoid heavy checkout repeated in every stageCheck out once and stash, or use a shallow clone (depth: 1)
Do not hold agents while waitingPut input and long sleeps in stages with agent none
Split the slow test suiteShard tests across several parallel branches
Clean the workspace sensiblycleanWs() for hygiene, but keep caches you want to reuse
groovy checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [[$class: 'CloneOption', shallow: true, depth: 1, noTags: true, timeout: 10]], userRemoteConfigs: [[url: 'https://git.example.com/team/app.git', credentialsId: 'git-token']]])

Practice Tasks

  1. Build a pipeline with three parallel test branches and measure the saved time
  2. Make one branch fail and compare behaviour with and without failFast
  3. Create a matrix of two OS values and two runtimes with one exclusion
  4. Use stash and unstash to pass a build output between two agents
  5. Shard a test suite into four parallel groups from a Groovy list

Conclusion

Parallel stages and matrix builds cut feedback time, failFast stops wasted work, and stash moves small files between agents. Give agents per stage, skip work with when, cache dependencies and use shallow clones to speed up pipelines further.

Quick Summary: parallel + failFast; agent per stage; stash/unstash limits; matrix axes/excludes; dynamic parallel map; speed checklist.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators