Pipeline stages and steps in Jenkins - Time & Space Complexity
We want to understand how the time to run a Jenkins pipeline changes as we add more stages and steps.
How does the total work grow when the pipeline gets bigger?
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
}
}
This pipeline has two stages, each with one step that prints a message.
Look for repeated actions in the pipeline.
- Primary operation: Executing each step inside every stage.
- How many times: Once per step, repeated for all stages.
As we add more stages and steps, the total work grows.
| Input Size (number of stages) | Approx. Operations (steps executed) |
|---|---|
| 10 | 10 steps (if 1 step per stage) |
| 100 | 100 steps |
| 1000 | 1000 steps |
Pattern observation: The total steps grow directly with the number of stages.
Time Complexity: O(n)
This means the time to run the pipeline grows in a straight line as you add more stages.
[X] Wrong: "Adding more stages won't affect total time much because they run fast."
[OK] Correct: Even if each step is quick, the total time adds up because all steps run one after another.
Understanding how pipeline time grows helps you design efficient build processes and explain your reasoning clearly in discussions.
"What if we run some stages in parallel? How would the time complexity change?"
Practice
stages in a Jenkins pipeline?Solution
Step 1: Understand the role of stages
Stages are used to split the pipeline into clear sections, making it easier to read and manage.Step 2: Differentiate stages from steps
Steps are the actual commands or actions inside stages, not the division itself.Final Answer:
To divide the pipeline into distinct parts for clarity -> Option CQuick Check:
Stages divide pipeline parts = C [OK]
- Confusing stages with steps
- Thinking stages run commands directly
- Assuming stages store logs
Build in a Jenkins declarative pipeline?Solution
Step 1: Recall declarative pipeline stage syntax
The correct syntax usesstage('Name') { ... }to define a stage.Step 2: Check the options for correct structure
stage('Build') { steps { echo 'Building...' } } matches the correct syntax withstage('Build') { steps { ... } }.Final Answer:
stage('Build') { steps { echo 'Building...' } } -> Option DQuick Check:
Stage syntax uses stage('name') { steps { ... } } = A [OK]
- Using colon instead of parentheses for stage name
- Placing stage inside stages incorrectly
- Missing quotes around stage name
pipeline {
agent any
stages {
stage('Test') {
steps {
echo 'Running tests'
}
}
}
}Solution
Step 1: Identify the stage and steps
The pipeline has one stage named 'Test' with a step that echoes 'Running tests'.Step 2: Understand echo step output
The echo step prints the given string to the console output when the pipeline runs.Final Answer:
Running tests -> Option AQuick Check:
Echo prints 'Running tests' = A [OK]
- Thinking stage name prints output
- Assuming syntax error without checking carefully
- Expecting output from missing steps
pipeline {
agent any
stages {
stage('Deploy')
steps {
echo 'Deploying app'
}
}
}Solution
Step 1: Check stage syntax
The stage block must have curly braces enclosing its content:stage('Deploy') { ... }.Step 2: Identify missing braces
In the snippet, the stage declaration lacks opening and closing braces around steps.Final Answer:
Missing curly braces after stage declaration -> Option BQuick Check:
Stage needs braces { } = D [OK]
- Forgetting braces after stage name
- Placing steps outside stages block
- Misunderstanding agent syntax
Build and Test. The Test stage should only run if the Build stage succeeds. Which pipeline snippet correctly implements this logic?Solution
Step 1: Understand conditional stage execution
Jenkins declarative pipelines run stages sequentially by default. If the Build stage fails, the Test stage is skipped.Step 2: Check the snippet with sequential stages
pipeline { agent any stages { stage('Build') { steps { echo 'Building...' } } stage('Test') { steps { echo 'Testing...' } } } }Step 3: Verify distractors
The snippet withwhen { expression { currentBuild.result == 'SUCCESS' } }uses the correct propertyresultinstead ofcurrentResult.
The snippet withwhen { branch 'main' }conditions on Git branch.
The snippet withpost { success { ... } }adds an unrelated post-action block.Final Answer:
pipeline { agent any stages { stage('Build') { steps { echo 'Building...' } } stage('Test') { steps { echo 'Testing...' } } } } -> Option AQuick Check:
Sequential stages run conditionally by default = A [OK]
- Using invalid when expression syntax
- Conditioning on branch instead of stage success
- Adding unnecessary post blocks
Ready to prove it?
Pass the test and this pattern moves unknown
