Failing fast principle in Jenkins - Time & Space Complexity
When using the failing fast principle in Jenkins pipelines, we want to know how quickly the system stops when an error occurs.
We ask: how does the time spent change as the number of steps grows?
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
for (int i = 0; i < stepsList.size(); i++) {
if (!stepsList[i].run()) {
error('Step failed, stopping pipeline')
}
}
}
}
}
}
}
This code runs a list of steps one by one and stops immediately if any step fails.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over stepsList to run each step.
- How many times: Up to the number of steps in stepsList, but may stop early on failure.
The pipeline runs steps one by one until a failure or all steps complete.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 steps run, but may stop sooner if a failure occurs. |
| 100 | Up to 100 steps run, stopping early on failure. |
| 1000 | Up to 1000 steps run, stopping early on failure. |
Pattern observation: The time grows linearly with the number of steps but can be less if a failure happens early.
Time Complexity: O(n)
This means the time to run grows roughly in direct proportion to the number of steps, but can be shorter if a failure stops the process early.
[X] Wrong: "The pipeline always runs all steps no matter what."
[OK] Correct: Because of failing fast, the pipeline stops as soon as a step fails, so it may run fewer steps than the total.
Understanding how failing fast affects time helps you explain efficient pipeline design and error handling in real projects.
"What if we changed the pipeline to run all steps even if some fail? How would the time complexity change?"
Practice
Solution
Step 1: Understand the failing fast principle
The principle means to stop work quickly when an error happens to save time and resources.Step 2: Apply to Jenkins pipelines
In Jenkins, this means the pipeline should stop immediately on error instead of continuing.Final Answer:
Stop the pipeline immediately when an error occurs -> Option BQuick Check:
Fail fast = stop immediately on error [OK]
- Thinking pipeline should continue despite errors
- Confusing fail fast with retrying steps
- Assuming errors are handled at the end
Solution
Step 1: Identify the Jenkins command to fail fast
Theerror()step immediately stops the pipeline with a failure.Step 2: Check the condition usage
If the condition is false, callingerror()triggers fail fast behavior.Final Answer:
if (!condition) { error('Fail fast') } -> Option DQuick Check:
Use error() to fail fast [OK]
- Using echo() which only prints message
- Using retry() which retries steps instead of failing
- Using sleep() which delays but does not fail
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
if (false) {
error('Fail fast triggered')
}
echo('Step completed')
}
}
}
}
}Solution
Step 1: Analyze the condition in the script block
The condition isfalse, soerror()is not called.Step 2: Check what happens next
The pipeline executesecho('Step completed')and continues successfully.Final Answer:
Pipeline prints 'Step completed' and succeeds -> Option AQuick Check:
False condition skips error(), so pipeline succeeds [OK]
- Assuming error() runs even if condition is false
- Thinking pipeline fails without error() call
- Confusing echo() with error()
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
if (buildStatus == 'FAILURE')
error('Build failed')
}
}
}
}
}Solution
Step 1: Check variable definitions
ThebuildStatusvariable is not defined in the pipeline context.Step 2: Verify standard Jenkins variables
In Jenkins, usecurrentBuild.resultor definebuildStatusfrom previous steps to check status.Final Answer:
buildStatus variable is undefined -> Option AQuick Check:
Undefined variables cause runtime errors [OK]
- Assuming error() usage is wrong
- Ignoring undefined variables without context
- Misplacing steps and script blocks
Solution
Step 1: Understand fail fast requirement
Fail fast means stop pipeline immediately on test failure, so no further stages run.Step 2: Evaluate options
Usingerror()inside each test stage stops pipeline immediately on failure, matching fail fast.Final Answer:
Use error() inside each test stage to stop pipeline on failure -> Option CQuick Check:
Fail fast = error() stops pipeline immediately [OK]
- Using catchError which allows pipeline to continue
- Using try-catch which delays failure
- Using sleep() which only delays, not fails
Ready to prove it?
Pass the test and this pattern moves unknown
