Parallel stages, post, retry and timeout

Run checks together, stop early, clean up, and never hang forever.

Intermediate13 min · lesson 5 of 15
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (19 files, 7 KB): stages.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: 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: 389df626d334acc5a5fdba522c36ef038edcf0932a19af32faa6c4ed6f96a7d3

Your 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

app/Jenkinsfile
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' }
}
}
ubuntu@secopslog-cicd:~/lab/stages · Jenkins 2.580.2
$ ./up.sh
agent lab-agent: online Jenkins 2.580.2 is up on http://127.0.0.1:8080
$ ./seed.sh app app/ && ./newjob.sh good app && ./newjob.sh failing app Jenkinsfile.fail && ./newjob.sh hung app Jenkinsfile.timeout
pushed app: 61c548e first commit job good reads Jenkinsfile from git://git/app.git job failing reads Jenkinsfile.fail from git://git/app.git job hung reads Jenkinsfile.timeout from git://git/app.git
$ ./run-job.sh good
Started by user admin Running on lab-agent in /home/jenkins/agent/workspace/good [Pipeline] parallel + sleep 4 + sleep 4 + echo unit ok unit ok + echo lint ok lint ok packaging post: always post: success Finished: SUCCESS build #1 result: SUCCESS

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:

app/Jenkinsfile.fail
pipeline {
agent { label 'linux' }
stages {
stage('Checks') {
failFast true
parallel {
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' }
}
}
ubuntu@secopslog-cicd:~/lab/stages · Jenkins 2.580.2
$ ./run-job.sh failing
Started by user admin Running on lab-agent in /home/jenkins/agent/workspace/failing [Pipeline] parallel + sleep 1 + sleep 30 + echo lint found a problem lint found a problem + exit 1 Failed in branch Lint Sending interrupt signal to process Terminated Failed in branch Unit Stage "Package" skipped due to earlier failure(s) post: always post: failure ERROR: script returned exit code 1 Finished: FAILURE build #1 result: FAILURE

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

app/Jenkinsfile.timeout
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' } }
}
ubuntu@secopslog-cicd:~/lab/stages · Jenkins 2.580.2
$ ./run-job.sh hung
Started by user admin Running on lab-agent in /home/jenkins/agent/workspace/hung [Pipeline] retry + test -f ok + touch ok + echo first try fails first try fails + exit 1 ERROR: script returned exit code 1 Retrying + test -f ok + rm ok + echo second try works second try works [Pipeline] timeout Timeout set to expire in 5 sec + sleep 60 Cancelling nested steps due to timeout Sending interrupt signal to process Terminated post: aborted Timeout has been exceeded org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: 943c4c9d-b4f5-480d-b4e8-d671ab1b33a1 Finished: ABORTED build #1 result: ABORTED
$ ./down.sh
lab stopped and its data removed

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 conditionRuns when
alwaysWhatever the result
successThe build is SUCCESS
failureThe build is FAILURE
unstableThe build is UNSTABLE, usually from failing test reports
abortedThe build was stopped, by a timeout or a person
cleanupAfter 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.

Quick check
01A pipeline has 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?
Incorrect — failFast controls what happens on failure. It has no bearing on normal timing.
Correct — Branches with their own agent queue for executors. Branches under one top-level agent would share it and overlap.
Incorrect — There is no such limit in declarative syntax.
Incorrect — The sh steps run as separate processes on the agent.
02A stage hits its timeout. An alert is configured in post { failure { ... } }. What does the on-call engineer see?
Incorrect — A timeout gives ABORTED, and the failure condition does not match that.
Incorrect — unstable is about test reports and has no effect here.
Incorrect — They can. The lab pipeline uses them together without error.
Correct — Add an aborted condition, or use one notifying step in always.
03Where does retry(3) belong in a pipeline?
Correct — A retry there is honest. Around tests, it hides flakiness someone should fix.
Incorrect — That makes a real intermittent bug look green and leaves it unfixed.
Incorrect — That repeats deterministic failures three times and hides the flaky ones.
Incorrect — It does not. In the lab, the first failure was retried only because of retry(3).

Related