Milestone step for concurrency in Jenkins - Time & Space Complexity
When using the milestone step in Jenkins pipelines, it helps control how concurrent builds proceed.
We want to understand how the time to process builds changes as more builds run at once.
Analyze the time complexity of the following Jenkins pipeline snippet using milestone.
pipeline {
agent any
stages {
stage('Build') {
steps {
milestone 1
echo 'Building...'
}
}
stage('Test') {
steps {
milestone 2
echo 'Testing...'
}
}
}
}
This pipeline uses milestone steps to ensure only the latest build passes each milestone, cancelling older ones.
Look for repeated or concurrent operations that affect execution time.
- Primary operation: Multiple builds running concurrently, each hitting milestone steps.
- How many times: Number of concurrent builds trying to pass milestones.
As more builds run at the same time, the milestone step cancels older builds to keep only the latest.
| Concurrent Builds (n) | Approx. Active Builds After Milestone |
|---|---|
| 10 | 1 (latest build continues) |
| 100 | 1 (only newest build proceeds) |
| 1000 | 1 (milestone cancels all but latest) |
Pattern observation: Regardless of how many builds start, only one build passes each milestone, so execution time stays stable.
Time Complexity: O(1)
This means the time to pass a milestone does not grow with more concurrent builds because older builds are cancelled.
[X] Wrong: "More concurrent builds mean longer wait times at milestones because all builds wait their turn."
[OK] Correct: The milestone step cancels older builds, so only the newest build continues, keeping wait time constant.
Understanding how concurrency controls like milestone affect build time helps you design efficient pipelines and explain your reasoning clearly.
"What if we removed the milestone steps? How would the time complexity change with many concurrent builds?"
Practice
milestone step in a Jenkins pipeline?Solution
Step 1: Understand the role of the milestone step
The milestone step is designed to control concurrency by ensuring only the newest pipeline run proceeds beyond the milestone point.Step 2: Compare with other pipeline steps
Other steps like triggering builds or sending notifications do not control concurrency like milestone does.Final Answer:
To allow only the latest pipeline run to continue past a certain point -> Option AQuick Check:
Milestone controls concurrency = B [OK]
- Confusing milestone with notification steps
- Thinking milestone triggers builds
- Assuming milestone archives artifacts
Solution
Step 1: Recall the correct milestone syntax
The milestone step uses named parameters with the syntax: milestone label: 'LabelName'.Step 2: Identify syntax errors in other options
Options B, C, and D have incorrect syntax such as missing named parameter format or wrong punctuation.Final Answer:
milestone label: 'BuildCheckpoint' -> Option AQuick Check:
Correct milestone syntax uses label: 'name' [OK]
- Using parentheses instead of named parameters
- Using equal sign instead of colon
- Omitting the label keyword
stage('Test') {
steps {
milestone label: 'TestStart'
echo 'Running tests'
}
}
// Two pipeline runs start quickly one after another
What happens to the first pipeline run when the second run reaches the same milestone?Solution
Step 1: Understand milestone concurrency behavior
When multiple runs reach the same milestone label, Jenkins allows only the latest run to proceed and aborts older runs at that point.Step 2: Apply this to the scenario
The second run reaching 'TestStart' milestone causes the first run to abort at that milestone, so only the second run continues.Final Answer:
The first run is aborted and only the second run continues past the milestone -> Option CQuick Check:
Milestone aborts older runs = D [OK]
- Thinking both runs continue
- Assuming second run waits
- Believing both runs abort
Solution
Step 1: Check milestone placement
The milestone step is most likely placed after all stages have completed. This makes it ineffective because older runs finish before a newer run can reach the milestone to trigger aborts.Step 2: Evaluate other options
Label reuse affects log clarity but not concurrency control (ordinals matter). Missing agent prevents runs entirely. Server offline stops everything.Final Answer:
The milestone step is placed after all stages have completed -> Option BQuick Check:
Milestone placement before completion critical = A [OK]
- Placing milestone after all stages
- Blaming unrelated pipeline syntax
- Assuming server offline causes milestone issues
Solution
Step 1: Identify where to place milestone for concurrency control
To abort older runs before deploying, place the milestone step at the start of the Deploy stage so only the newest run proceeds.Step 2: Understand why other placements fail
Placing milestone at the end or in other stages won't stop older runs before deployment. Using it only in the first stage doesn't control concurrency at Deploy.Final Answer:
Placemilestone label: 'Deploy'at the start of the Deploy stage -> Option DQuick Check:
Milestone at stage start controls concurrency there [OK]
- Placing milestone too late in the stage
- Using milestone in unrelated stages
- Not placing milestone at all
Ready to prove it?
Pass the test and this pattern moves unknown
