Build steps execution in Jenkins - Time & Space Complexity
We want to understand how the time to run build steps changes as we add more steps.
How does the total work grow when the number of build steps increases?
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
for (int i = 0; i < stepsList.size(); i++) {
stepsList[i].execute()
}
}
}
}
}
}
This code runs each build step one after another from a list of steps.
Look for loops or repeated actions.
- Primary operation: Loop over all build steps to execute each one.
- How many times: Once for each build step in the list.
As the number of build steps grows, the total time grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 executions |
| 100 | 100 executions |
| 1000 | 1000 executions |
Pattern observation: The total work grows directly with the number of steps.
Time Complexity: O(n)
This means the time to finish all build steps grows in a straight line as you add more steps.
[X] Wrong: "Running more build steps won't affect total time much because they run fast."
[OK] Correct: Even if each step is fast, doing many steps adds up and total time grows with the number of steps.
Understanding how build steps add up helps you explain pipeline performance clearly and shows you can reason about process scaling.
"What if the build steps could run in parallel? How would the time complexity change?"
Practice
Solution
Step 1: Understand what build steps do
Build steps are commands executed during a Jenkins pipeline to automate tasks.Step 2: Identify the correct purpose
They help compile, test, and package software automatically as part of the build process.Final Answer:
To run commands that compile, test, and package software automatically -> Option DQuick Check:
Build steps = automate build tasks [OK]
- Confusing build steps with server management
- Thinking build steps create users
- Assuming build steps monitor hardware
Solution
Step 1: Recall Jenkins pipeline shell step syntax
In Jenkins scripted or declarative pipelines, shell commands run with theshstep and a string argument.Step 2: Match the correct syntax
The correct syntax issh 'command', sosh 'echo Hello World'is valid.Final Answer:
sh 'echo Hello World' -> Option AQuick Check:
Shell step uses sh 'command' [OK]
- Using incorrect keywords like shell: or execute shell
- Omitting quotes around the command
- Writing commands without the sh step
stage('Build') {
steps {
sh 'echo Start Build'
sh 'echo Build Complete'
}
}Solution
Step 1: Understand step execution order
Jenkins runs build steps in the order they appear inside the stage.Step 2: Trace the commands
First,echo Start Buildruns, printing "Start Build". Thenecho Build Completeruns, printing "Build Complete".Final Answer:
Start Build\nBuild Complete -> Option CQuick Check:
Steps run in order, output matches [OK]
- Assuming steps run in reverse order
- Thinking only first step outputs
- Believing steps might be skipped without error
stage('Test') {
steps {
sh 'echo Running tests'
sh 'exit 1'
sh 'echo Tests done'
}
}
What will happen when this pipeline runs?Solution
Step 1: Understand shell step failure behavior
In Jenkins, if a shell command returns a non-zero exit code (like exit 1), the pipeline fails and stops executing further steps.Step 2: Analyze the commands
Aftersh 'exit 1'fails, the next stepsh 'echo Tests done'will not run.Final Answer:
Pipeline stops at 'exit 1' and 'echo Tests done' is not executed -> Option BQuick Check:
Non-zero exit stops pipeline [OK]
- Assuming pipeline continues after failure
- Thinking all steps always run
- Ignoring exit codes in shell steps
Solution
Step 1: Understand conditional execution in Jenkins pipeline
To run tests only if compile succeeds, you need to check the build status before running tests.Step 2: Analyze options
stage('Build') { steps { sh 'compile.sh' script { if (currentBuild.result != 'FAILURE') { sh 'test.sh' } } } } uses ascriptblock to check if the build is not failed before running tests, ensuring tests run only after successful compile.Final Answer:
sh 'compile.sh'; script { if (currentBuild.result != 'FAILURE') { sh 'test.sh' } } -> Option AQuick Check:
Conditional test run uses script and build status check [OK]
- Running all steps unconditionally
- Using shell || true which ignores compile failure
- Splitting stages without controlling flow
Ready to prove it?
Pass the test and this pattern moves unknown
