Shell steps (sh, bat) in Jenkins - Time & Space Complexity
We want to understand how the time taken by shell steps in Jenkins changes as the commands run more or process more data.
Specifically, how does running shell commands inside Jenkins scale with input size?
Analyze the time complexity of the following Jenkins pipeline snippet using shell steps.
pipeline {
agent any
stages {
stage('Run Shell Commands') {
steps {
sh 'for i in $(seq 1 100); do echo $i; done'
}
}
}
}
This code runs a shell loop printing numbers from 1 to 100 inside a Jenkins pipeline.
Look for repeated actions inside the shell step.
- Primary operation: The shell loop runs the echo command multiple times.
- How many times: The loop runs 100 times, once for each number.
The number of echo commands grows directly with the loop count.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 echo commands |
| 100 | 100 echo commands |
| 1000 | 1000 echo commands |
Pattern observation: The total work grows linearly as the number of loop iterations increases.
Time Complexity: O(n)
This means the time taken grows in direct proportion to the number of commands run inside the shell loop.
[X] Wrong: "Running shell steps inside Jenkins always takes constant time regardless of commands."
[OK] Correct: The time depends on what the shell commands do and how many times they run, so more commands mean more time.
Understanding how shell steps scale helps you explain pipeline performance and troubleshoot slow builds confidently.
"What if the shell loop runs nested loops inside? How would the time complexity change?"