Post section (success, failure, always) in Jenkins - Time & Space Complexity
We want to understand how the time taken by a Jenkins pipeline changes when using the post section with success, failure, and always blocks.
Specifically, how does adding these blocks affect the overall execution time as the pipeline grows?
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
}
post {
success {
echo 'Success!'
}
failure {
echo 'Failure!'
}
always {
echo 'Always runs'
}
}
}
This pipeline runs a build stage and then runs different post blocks depending on the build result.
Look for repeated actions or loops in the post section.
- Primary operation: The post section runs exactly one of the success or failure blocks, plus the always block.
- How many times: Each post block runs once per pipeline execution, no loops or recursion.
The post section runs a fixed number of steps regardless of pipeline size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 (success/failure + always) |
| 100 | 3 |
| 1000 | 3 |
Pattern observation: The number of post steps does not grow with input size; it stays constant.
Time Complexity: O(1)
This means the post section runs a fixed number of steps no matter how big the pipeline or input is.
[X] Wrong: "The post section runs multiple times and grows with the number of stages or steps."
[OK] Correct: The post section runs only once after the entire pipeline finishes, so its execution time does not increase with pipeline size.
Understanding fixed versus growing execution parts in pipelines helps you design efficient CI/CD workflows and explain your reasoning clearly in interviews.
"What if the post section contained loops or calls to other jobs? How would that affect the time complexity?"