Node and stage blocks in Jenkins - Time & Space Complexity
We want to understand how the time to run Jenkins pipelines changes as we add more stages or nodes.
How does the pipeline's execution time grow when we increase the number of stages or nodes?
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent none
stages {
stage('Build') {
agent { label 'linux' }
steps {
echo 'Building...'
}
}
stage('Test') {
agent { label 'linux' }
steps {
echo 'Testing...'
}
}
}
}
This pipeline runs two stages, each on a node labeled 'linux', executing simple steps.
Look for repeated parts that affect execution time.
- Primary operation: Running each stage on a node.
- How many times: Once per stage, so the number of stages determines repetitions.
As we add more stages, the pipeline runs more node blocks sequentially.
| Input Size (number of stages) | Approx. Operations (node runs) |
|---|---|
| 10 | 10 node runs |
| 100 | 100 node runs |
| 1000 | 1000 node runs |
Pattern observation: The total execution time grows roughly in direct proportion to the number of stages.
Time Complexity: O(n)
This means the time to run the pipeline grows linearly with the number of stages.
[X] Wrong: "Adding more stages does not increase total time because they run in parallel automatically."
[OK] Correct: By default, stages run one after another unless explicitly set to run in parallel, so more stages usually mean more total time.
Understanding how Jenkins stages and nodes affect pipeline time helps you design efficient pipelines and explain your choices clearly in discussions.
"What if we changed the stages to run in parallel? How would the time complexity change?"