Stage block structure in Jenkins - Time & Space Complexity
We want to understand how the time to run a Jenkins pipeline changes as we add more stages.
How does the number of stages affect the total execution time?
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}
This pipeline runs three stages sequentially: Build, Test, and Deploy.
Look at what repeats as the pipeline runs.
- Primary operation: Each stage runs its steps one after another.
- How many times: Once per stage, in order.
As you add more stages, the total time grows by adding each stage's time.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 stages | 3 sets of steps run |
| 10 stages | 10 sets of steps run |
| 100 stages | 100 sets of steps run |
Pattern observation: The total work grows directly with the number of stages.
Time Complexity: O(n)
This means if you double the number of stages, the total execution time roughly doubles.
[X] Wrong: "Adding more stages won't affect total time because they run fast."
[OK] Correct: Each stage adds its own steps to run, so more stages mean more total work and longer time.
Understanding how pipeline stages add up helps you design efficient pipelines and explain your reasoning clearly in discussions.
"What if stages ran in parallel instead of sequentially? How would the time complexity change?"