Speeding Up Jenkins Builds with Declarative Pipeline Parallel Stages
Learn how to use parallel stages in Jenkins Declarative Pipeline to run independent build steps concurrently, see a concrete example with sleep commands, and understand the trade‑offs with executor limits and workspace isolation.
05 Jan 2026, 06:19 UTC

Problem: Long sequential builds waste time
Many teams notice that their Jenkins pipelines spend minutes (or even hours) waiting for independent steps to finish one after another. When unit tests, integration tests, and static analysis could run at the same time, a sequential pipeline leaves agents idle and delays feedback.
Thesis: Use parallel stages to run independent work concurrently
Declarative Pipeline’s parallel directive lets you split a stage into multiple branches that execute simultaneously on available agents. This reduces total wall‑clock time to roughly the duration of the longest branch, provided you have enough executors and avoid shared mutable state.
Setting up parallel stages in a Declarative Pipeline
Place a parallel block inside a stage. Each branch gets its own workspace unless you explicitly share a directory with the ws step or stash/unstash artifacts.
pipeline {
agent any
stages {
stage('Build and Test') {
parallel {
stage('Unit Tests') {
steps {
sh 'sleep 10'
echo 'Unit tests done'
}
}
stage('Integration Tests') {
steps {
sh 'sleep 10'
echo 'Integration tests done'
}
}
}
}
}
}
Workspace considerations
Because each parallel branch runs in an isolated workspace, any file you need to share must be explicitly stashed in one branch and unstashed in another, or you can use ws to point all branches at the same directory. Forgetting this leads to missing artifacts or confusing “file not found” errors.
Worked example: verifying parallel execution
- Create a new Pipeline job that points to your source repository (you need Job Configure and Build permissions).
- Add the Jenkinsfile above; replace the
sh 'sleep 10'lines with a quick command likeecho helloif you want a faster test. - Start the build and watch the Console Output; the total duration should be close to the longest branch (≈10 seconds in the example).
- Open Blue Ocean for the run. You should see two columns under the “Build and Test” stage with overlapping timestamps, indicating simultaneous execution.
- To confirm the executor limit effect, temporarily reduce the agent’s executor count to 1 and rerun; the build time will now approach the sum of the branches (~20 seconds) because the second branch waits in the queue.
Trade‑offs and limitations
- Executor cap: If you launch more parallel branches than the agent has executors, extra branches queue, eliminating the speed‑up benefit.
- Resource usage: Each branch consumes CPU and memory on the agent; monitor node load to avoid saturation.
- Workspace isolation: Sharing data requires explicit steps; accidental sharing via the default workspace can cause race conditions.
Actionable closing
Start small: add a parallel block around two independent test suites, verify the timing with the steps above, then expand to other independent tasks like linting, security scans, or documentation generation. Keep an eye on executor counts and workspace sharing, and you’ll see measurable reductions in pipeline latency without major refactoring.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.