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
parallelis inside a stage and contains named stages, each can have its ownagentfailFast trueaborts the other branches as soon as one fails. Or setparallelsAlwaysFailFast()inoptions- The parallel branches share the build, but each branch may use a different workspace when it uses a different agent
- Use
agent noneat 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).
| Use stash for | Do not use stash for |
|---|---|
| Compiled output needed by test stages on other agents | Large files (hundreds of MB): use Nexus, Artifactory or S3 |
| Test scripts and small data sets | Files needed after the build ends: use archiveArtifacts |
Matrix: Test Many Combinations
- 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
wheninside a matrix,failFastandexcludesto 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.
Post Conditions in Parallel
Reduce Pipeline Time: Checklist
| Idea | How |
|---|---|
| Run independent work in parallel | parallel, matrix |
| Skip unneeded work | when { changeset }, skip tests if only docs changed |
| Cache dependencies | Persistent agent caches, Docker layer cache, ~/.m2 volume, artifact proxy such as Nexus |
| Use faster agents | More CPU and fast disks for build stages |
Avoid heavy checkout repeated in every stage | Check out once and stash, or use a shallow clone (depth: 1) |
| Do not hold agents while waiting | Put input and long sleeps in stages with agent none |
| Split the slow test suite | Shard tests across several parallel branches |
| Clean the workspace sensibly | cleanWs() for hygiene, but keep caches you want to reuse |
Practice Tasks
- Build a pipeline with three parallel test branches and measure the saved time
- Make one branch fail and compare behaviour with and without
failFast - Create a matrix of two OS values and two runtimes with one exclusion
- Use stash and unstash to pass a build output between two agents
- 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.

