Options directive (timeout, retry) in Jenkins - Time & Space Complexity
When using Jenkins options like timeout and retry, it's important to understand how they affect the total time your pipeline runs.
We want to see how the execution time grows as retries increase or timeout limits change.
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
options {
timeout(time: 5, unit: 'MINUTES')
retry(3)
}
stages {
stage('Example') {
steps {
sh 'echo Hello World'
}
}
}
}
This pipeline runs a simple shell command with a timeout of 5 minutes and retries up to 3 times on failure.
Look for repeated attempts and time limits that affect execution.
- Primary operation: The retry block repeats the stage steps on failure.
- How many times: Up to 3 retries, so the steps can run up to 4 times total (1 initial + 3 retries).
The total execution time can grow with the number of retries allowed.
| Retries Allowed (n) | Max Attempts |
|---|---|
| 1 | 2 (1 initial + 1 retry) |
| 3 | 4 (1 initial + 3 retries) |
| 5 | 6 (1 initial + 5 retries) |
Pattern observation: Each retry adds one more possible full execution of the steps, increasing total time linearly.
Time Complexity: O(n)
This means the total execution time grows linearly with the number of retries allowed.
[X] Wrong: "Timeout limits the total pipeline time regardless of retries."
[OK] Correct: Timeout applies per attempt, so retries can multiply total time beyond the single timeout limit.
Understanding how retry and timeout options affect pipeline duration shows you can reason about pipeline behavior and resource use, a key skill in DevOps roles.
"What if we changed retry(3) to retry(0)? How would the time complexity change?"
Practice
timeout option in a Jenkins pipeline?Solution
Step 1: Understand the role of timeout
Thetimeoutoption is used to limit how long a build or step can run.Step 2: Identify the effect of timeout
If the build runs longer than the set time, Jenkins stops it to save resources.Final Answer:
To stop a build if it runs longer than a specified time -> Option AQuick Check:
Timeout = stop long builds [OK]
- Confusing timeout with retry
- Thinking timeout schedules builds
- Assuming timeout increases priority
Solution
Step 1: Recall Jenkins options syntax
The correct syntax usesretry(3)inside theoptionsblock without extra braces.Step 2: Identify the valid option
options { retry(3) } matches the correct syntax exactly:options { retry(3) }.Final Answer:
options { retry(3) } -> Option DQuick Check:
Retry syntax = retry(3) inside options [OK]
- Adding extra braces after retry(3)
- Using retry without parentheses
- Placing retry outside options block
options {
timeout(time: 5, unit: 'MINUTES')
retry(2)
}
stage('Test') {
steps {
script {
def count = 0
retry(2) {
count++
if (count < 3) {
error('Fail')
}
}
echo "Count is ${count}"
}
}
}What will be the output of the
echo command?Solution
Step 1: Understand retry behavior in script
The innerretry(2)block retries the enclosed code up to 2 times on failure.Step 2: Trace the count increments and errors
First run: count=1, error thrown (count<3). Retry 1: count=2, error thrown. Retry 2: count=3, no error, exits retry.Step 3: Determine echo output
After retries, count is 3, soecho "Count is 3"runs.Final Answer:
Count is 3 -> Option CQuick Check:
Retry runs until count=3, then echo prints count [OK]
- Assuming retry runs only once
- Thinking count resets on retry
- Ignoring retry inside script block
options {
timeout(10)
retry(3)
}But the build fails with a syntax error. What is the problem?
Solution
Step 1: Check timeout syntax requirements
Thetimeoutoption requires named parameters:timeandunit.Step 2: Identify missing parameters
Usingtimeout(10)alone is invalid; it must betimeout(time: 10, unit: 'MINUTES').Final Answer:
timeout requires both time and unit parameters -> Option AQuick Check:
Timeout needs time and unit named params [OK]
- Passing only a number to timeout
- Thinking retry needs a block
- Confusing timeout value types
Solution
Step 1: Set timeout correctly
Timeout must use named parameters:time: 2, unit: 'MINUTES'.Step 2: Add retry correctly
Retry is a separate option inside theoptionsblock:retry(2).Step 3: Validate options block structure
options { timeout(time: 2, unit: 'MINUTES') retry(2) } correctly lists both options without nesting or syntax errors.Final Answer:
options { timeout(time: 2, unit: 'MINUTES') retry(2) } -> Option BQuick Check:
Timeout and retry as separate options inside options block [OK]
- Nesting retry inside timeout block
- Using timeout without named params
- Wrong parameter order or types
Ready to prove it?
Pass the test and this pattern moves unknown
