Idempotent pipeline steps in Jenkins - Time & Space Complexity
We want to understand how the time taken by idempotent steps in a Jenkins pipeline changes as we run the pipeline multiple times or with different inputs.
Specifically, we ask: How does repeating idempotent steps affect execution time?
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
if (!fileExists('build/output.jar')) {
sh 'make build'
}
}
}
}
}
}
This pipeline runs a build step only if the output file does not exist, making the step idempotent.
Look for repeated actions or checks in the pipeline.
- Primary operation: Checking if the build output file exists.
- How many times: Once per pipeline run.
As the pipeline runs multiple times, the build step runs only if the output is missing.
| Input Size (pipeline runs) | Approx. Operations |
|---|---|
| 1 | Check file + run build (if missing) |
| 10 | 10 file checks + build runs only if output missing |
| 100 | 100 file checks + build runs only if output missing |
Pattern observation: The file check happens every time, but the build command runs only when needed, so total work grows slowly.
Time Complexity: O(n)
This means the time grows linearly with the number of pipeline runs, mostly due to the file existence check each time.
[X] Wrong: "The build step runs every time, so time grows much faster."
[OK] Correct: The pipeline skips the build if the output exists, so the expensive build command does not run every time.
Understanding idempotent steps helps you design pipelines that save time and resources, a valuable skill in real projects and interviews.
"What if the pipeline checked multiple files before building? How would the time complexity change?"