0
0
Jenkinsdevops~10 mins

Options directive (timeout, retry) in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Options directive (timeout, retry)
Start Pipeline
Apply Options Directive
Execute Stage
Check Timeout
Abort Stage
Check Retry Count
Retry Stage
End Pipeline
The pipeline starts, applies options like timeout and retry, runs the stage, checks if timeout occurred to abort, and retries the stage if it fails, until success or retry limit.
Execution Sample
Jenkins
pipeline {
  options {
    timeout(time: 1, unit: 'MINUTES')
    retry(2)
  }
  stages {
    stage('Example') { steps { sh 'exit 1' } }
  }
}
This Jenkins pipeline runs a stage with a 1-minute timeout and retries the stage up to 2 times if it fails.
Process Table
StepActionTimeout CheckRetry CountStage ResultNext Step
1Start pipeline and apply optionsNo timeout0Not startedExecute stage
2Execute stage first timeNo timeout0Fail (exit 1)Retry stage (retry=1)
3Execute stage retry 1No timeout1Fail (exit 1)Retry stage (retry=2)
4Execute stage retry 2No timeout2Fail (exit 1)No retries left, abort
5Pipeline aborts due to failure after retriesNo timeout2AbortedEnd pipeline
💡 Retries exhausted (2 retries done), stage failed, pipeline aborts
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
retryCount01222
stageResultNot startedFailFailFailAborted
timeoutReachedNoNoNoNoNo
Key Moments - 2 Insights
Why does the pipeline retry the stage after failure?
Because the retry option is set to 2, the pipeline retries the stage up to 2 times after failure, as shown in steps 2, 3, and 4 in the execution table.
What happens if the stage takes longer than the timeout?
If the stage exceeds the timeout, the pipeline aborts the stage immediately. In this example, no timeout occurred, but if it did, step 4 would show 'Timeout reached' and abort.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the retryCount after the first retry?
A1
B0
C2
D3
💡 Hint
Check the 'retryCount' variable in the variable_tracker after Step 2 and Step 3.
At which step does the pipeline stop retrying the stage?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Next Step' column in the execution_table where retries are exhausted.
If the timeout was set to 0.5 minutes, how would the execution table change?
ATimeout Check would be 'Yes' and pipeline aborts earlier
BRetry count would increase
CStage result would always be success
DNo change at all
💡 Hint
Timeout affects the 'Timeout Check' column and can cause early abort as per concept_flow.
Concept Snapshot
Jenkins options directive sets pipeline-wide behaviors.
Use timeout(time, unit) to limit stage duration.
Use retry(count) to rerun failed stages.
If timeout triggers, stage aborts immediately.
Retries stop after count is reached.
Combine for robust pipeline control.
Full Transcript
This visual execution shows how Jenkins pipeline options directive works with timeout and retry. The pipeline starts and applies options. It runs the stage and checks if the timeout is reached. If not, it checks the stage result. On failure, it retries the stage up to the retry count. If retries are exhausted, the pipeline aborts. Variables like retryCount and stageResult track progress. This helps beginners see how timeout stops long stages and retry reruns failed stages automatically.