Jenkins failure propagation and retries: preserve the reason a release must stop
Explain sh returnStatus, catchError and retry scope in Jenkins Pipelines. Keep failed verification from becoming a successful promotion and reconcile ambiguous side effects before retrying.
TL;DR: Jenkins can record a failure and continue executing Pipeline code. Decide which failures must abort promotion, then test that path deliberately. Retry a bounded recoverable operation; do not repeat an external mutation merely because its acknowledgement was lost.
Command status and control flow are separate
An ordinary sh step fails when its command exits unsuccessfully. With returnStatus: true, it returns the status as a value, so the Pipeline must evaluate that value. Logging tests returned 1 and moving on is successful execution of faulty release logic.
catchError can mark a build or stage result while continuing after its block. That is useful for collecting reports or running independent checks, but the later promotion decision still needs an explicit requirement. Jenkins documents these behaviors in using a Jenkinsfile and the basic steps.
Prove the failure path with a harmless marker
This Scripted Pipeline fixture deliberately uses a failing command. The final marker must never appear. It needs a Linux agent and does not perform a deployment.
node('linux') {
def status = sh(script: 'exit 7', returnStatus: true)
if (status != 0) {
error("Verification failed with status ${status}")
}
echo 'PROMOTION_MARKER'
}
Change exit 7 to exit 0 for the positive case. Then, in a disposable copy, remove the status guard: the failing command now reaches the marker and exposes the defect the guard prevents. Retain test reports in an appropriate post or finally path, while keeping the release decision blocked.
| Mechanism | Behavior to account for | Appropriate use |
|---|---|---|
Ordinary failing sh | Throws unless handled | Stop dependent work |
returnStatus: true | Returns an integer | Branch explicitly on expected outcomes |
catchError | Can record failure and continue | Collect further evidence with a separate release gate |
retry | Repeats its block under configured conditions | Bounded recovery of an understood failure |
Choose the smallest safe retry scope
An agent connection failure during a clean checkout can justify allocating another worker and retrying. A deterministic unit-test assertion usually does not; repeated retries can turn flakiness into a hidden release criterion. Record each attempt and decide which class is recoverable.
The retry step supports conditions including agent-related failures and nonresumable steps interrupted by a controller restart. Availability and details depend on installed Pipeline plugins. A conditional retry is more precise than wrapping the entire delivery process in unconditional retries.
Now consider a deployment API accepting an operation while the client times out before receiving its response. Repeating the whole block can create another operation. Query by a stable operation or idempotency identifier first, or use an API contract that makes retries safe. Agent replacement cannot resolve uncertainty about a remote effect by itself.
Self-check: catchError marks the build failed, but the next sh step publishes the image. Did the red build prevent the release? No. The effect already happened. Put the decision before publishing and test the downstream effect, not just the final build color.
Practice tests that fail while publishing continues and connect retry design to retry budgets.