CI/CD pipeline mental model in Jenkins - Time & Space Complexity
When we run a CI/CD pipeline in Jenkins, we want to know how the time it takes grows as we add more steps or projects.
We ask: How does the pipeline's work increase when we add more tasks?
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
for (int i = 0; i < projects.size(); i++) {
build job: projects[i]
}
}
}
}
}
}
This pipeline runs a build job for each project in a list one after another.
Look for repeated actions in the code.
- Primary operation: Running a build job for each project.
- How many times: Once per project, repeated for all projects in the list.
As the number of projects grows, the total build time grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 build jobs |
| 100 | 100 build jobs |
| 1000 | 1000 build jobs |
Pattern observation: The work grows directly with the number of projects; doubling projects doubles the work.
Time Complexity: O(n)
This means the total time grows in a straight line as you add more projects to build.
[X] Wrong: "Running multiple build jobs in a loop happens all at once, so time stays the same no matter how many projects."
[OK] Correct: In this code, builds run one after another, so more projects mean more total time.
Understanding how pipeline steps add up helps you explain how your automation scales as projects grow. This shows you can think about efficiency and planning.
"What if we changed the loop to run all build jobs in parallel? How would the time complexity change?"