Parallel stages, post, retry and timeout
Run checks together, stop early, clean up, and never hang forever.
cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-jenkins/stages.tar.gz && tar -xzf stages.tar.gz, which creates ~/lab/stages/. SHA-256: 389df626d334acc5a5fdba522c36ef038edcf0932a19af32faa6c4ed6f96a7d3Your lint takes four minutes, your unit tests take four more, and they don't depend on each other, so every developer waits eight minutes for an answer that could take four. Then one night a test hangs on a dead network connection, the build holds its executor until someone notices in the morning, and every other job queues behind it. This lesson deals with both: running work side by side, and making sure a build always ends, cleans up and says what happened.
Parallel stages and post
pipeline {agent { label 'linux' }stages {stage('Checks') {parallel {stage('Lint') { steps { sh 'sleep 4; echo lint ok' } }stage('Unit') { steps { sh 'sleep 4; echo unit ok' } }}}stage('Package') { steps { echo 'packaging' } }}post {success { echo 'post: success' }failure { echo 'post: failure' }always { echo 'post: always' }}}
The output shows both sleep 4 commands starting before either echo runs, so the two branches overlapped. They share the one executor and workspace that the top-level agent allocated, as two threads in the same build, so they also share its CPU and files. A branch that declares its own agent is different: it needs a free executor of its own, and with too few executors it waits in the queue. The hands-on shows both.
The order of the last lines is worth noticing: post: always printed before post: success. Jenkins runs post conditions in a fixed order that has always first and leaves cleanup to last, regardless of the order you wrote them in. So don't put a step in always that depends on something a later condition does.
Failing fast
If lint has already failed, there's little point spending another 30 seconds on the unit tests. failFast true stops the sibling branches. This job also has a failing lint branch:
pipeline {agent { label 'linux' }stages {stage('Checks') {failFast trueparallel {stage('Lint') { steps { sh 'sleep 1; echo lint found a problem; exit 1' } }stage('Unit') { steps { sh 'sleep 30; echo unit ok' } }}}stage('Package') { steps { echo 'packaging' } }}post {success { echo 'post: success' }failure { echo 'post: failure' }always { echo 'post: always' }}}
Failed in branch Lint, then Jenkins interrupted the sleep 30 in the other branch (Sending interrupt signal to process, Terminated) and marked it failed too. The later stage was skipped due to earlier failure, the post blocks still ran, and the build ended as FAILURE.
There's a trade-off to failFast. You get the answer sooner, but only one branch's failures. For test suites where developers want to see every failure in one run, leave it off.
Retry the flaky, time out the stuck
pipeline {agent { label 'linux' }stages {stage('Flaky download') {steps {retry(3) {sh 'test -f ok || { touch ok; echo first try fails; exit 1; }; rm ok; echo second try works'}}}stage('Hung') {options { timeout(time: 5, unit: 'SECONDS') }steps { sh 'sleep 60' }}}post { aborted { echo 'post: aborted' } }}
retry(3) re-ran the step after its first failure, and the second try succeeded. Keep retries around steps that fail for outside reasons, like a download. Wrapped around tests, a retry hides the flaky test that someone ought to fix.
The Hung stage had a 5-second limit. Jenkins interrupted the sleep, ran post { aborted }, and finished the build as ABORTED, a different result from FAILURE. It means "stopped before it could finish", so an alert that fires only on failure would stay silent about a stuck build. If your notifications matter, cover aborted too.
post condition | Runs when |
|---|---|
always | Whatever the result |
success | The build is SUCCESS |
failure | The build is FAILURE |
unstable | The build is UNSTABLE, usually from failing test reports |
aborted | The build was stopped, by a timeout or a person |
cleanup | After all the others, whatever the result |
Hands-on
In a fresh lab, remove failFast true from app/Jenkinsfile.fail, push with ./commit.sh app/ "no fail fast", and run ./run-job.sh failing. How long does it take now, and does unit ok appear? Then set the agent to one executor in casc.d/00-base.yaml, apply it with docker compose --progress quiet up -d --force-recreate --wait controller and ./agent.sh, and run good. Do the two sleep lines still start together? Create a job agents from app/Jenkinsfile.agents, where each branch has its own agent, and run it on the one executor. Finish with ./down.sh.
What you should see: without failFast, unit ok appears after Failed in branch Lint, so the other branch ran to the end, and the build still ends FAILURE. In the lab run it took 55 seconds in all, because the 30-second sleep finished. With one executor, good still starts both sleep 4 lines together, because its branches share the top-level agent's executor. agents runs them one after the other: the second sleep 4 starts only after lint ok. The two runs took 20 and 21 seconds, because most of each run is overhead, so judge by the order of the lines, not by the clock.
Remember
Branches with their own agent save time only when free executors exist. Every build needs a timeout. And ABORTED is its own result, so alert on it as well as on failure.
agent none and three parallel test branches, each with its own agent { label 'linux' }. The only linux agent has one executor. The total time does not improve. Why?failFast controls what happens on failure. It has no bearing on normal timing.agent queue for executors. Branches under one top-level agent would share it and overlap.sh steps run as separate processes on the agent.timeout. An alert is configured in post { failure { ... } }. What does the on-call engineer see?ABORTED, and the failure condition does not match that.unstable is about test reports and has no effect here.aborted condition, or use one notifying step in always.retry(3) belong in a pipeline?retry(3).