Build, test, deploy stages concept in Jenkins - Time & Space Complexity
We want to understand how the time taken by a Jenkins pipeline grows as we add more build, test, and deploy steps.
How does the total work change when we increase the number of tasks in each stage?
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
for (int i = 0; i < buildTasks.size(); i++) {
echo "Building task ${i}"
}
}
}
}
stage('Test') {
steps {
script {
for (int j = 0; j < testTasks.size(); j++) {
echo "Testing task ${j}"
}
}
}
}
stage('Deploy') {
steps {
script {
for (int k = 0; k < deployTasks.size(); k++) {
echo "Deploying task ${k}"
}
}
}
}
}
}
This pipeline runs three stages: build, test, and deploy. Each stage loops over a list of tasks and performs an action for each.
Look at the loops that repeat work in each stage.
- Primary operation: Looping over tasks in build, test, and deploy stages.
- How many times: Each loop runs once per task in its list (buildTasks, testTasks, deployTasks).
As the number of tasks grows, the total work grows by adding the work of each stage.
| Input Size (tasks per stage) | Approx. Operations (total loops) |
|---|---|
| 10 | 10 + 10 + 10 = 30 |
| 100 | 100 + 100 + 100 = 300 |
| 1000 | 1000 + 1000 + 1000 = 3000 |
Pattern observation: The total work grows linearly with the number of tasks in each stage combined.
Time Complexity: O(n)
This means the total time grows in a straight line as we add more tasks to build, test, and deploy.
[X] Wrong: "Because there are three stages, the time grows like n cubed (n³)."
[OK] Correct: The stages run one after another, so their times add up, not multiply. Each stage loops over tasks once, so total time grows linearly, not exponentially.
Understanding how pipeline stages add up in time helps you design efficient workflows and explain your choices clearly in interviews.
What if the test stage ran tests in parallel instead of one by one? How would the time complexity change?