currentBuild variables in Jenkins - Time & Space Complexity
We want to understand how using currentBuild variables affects the time it takes for a Jenkins pipeline to run.
Specifically, how does accessing or updating these variables change the work Jenkins does as the pipeline grows?
Analyze the time complexity of the following Jenkins pipeline snippet.
pipeline {
agent any
stages {
stage('Example') {
steps {
script {
for (int i = 0; i < n; i++) {
currentBuild.description = "Build step " + i
}
}
}
}
}
}
This code updates the currentBuild.description variable inside a loop that runs n times.
Look for repeated actions in the code.
- Primary operation: Updating
currentBuild.descriptioninside the loop. - How many times: The loop runs
ntimes, so the update happensntimes.
As n grows, the number of updates grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 updates to currentBuild.description |
| 100 | 100 updates to currentBuild.description |
| 1000 | 1000 updates to currentBuild.description |
Pattern observation: The work grows directly with the number of loop iterations.
Time Complexity: O(n)
This means the time to update currentBuild.description grows linearly as the number of updates increases.
[X] Wrong: "Updating currentBuild variables inside a loop is instant and does not add time."
[OK] Correct: Each update triggers Jenkins to process and save the change, so doing it many times adds up and takes longer.
Understanding how repeated updates to build variables affect pipeline time helps you write efficient Jenkins pipelines and shows you think about scaling your automation.
What if we updated currentBuild.description only once after the loop instead of inside it? How would the time complexity change?
Practice
currentBuild.number variable represent in a Jenkins Pipeline?Solution
Step 1: Understand the role of
ThecurrentBuildcurrentBuildvariable holds information about the current Jenkins job execution.Step 2: Identify what
Thenumbermeansnumberproperty specifically gives the unique build number assigned to this run.Final Answer:
The unique build number of the current running job -> Option BQuick Check:
currentBuild.number= build number [OK]
- Confusing build number with total builds
- Thinking it counts test failures
- Mixing it with stage counts
Solution
Step 1: Recall the property for build result
The JenkinscurrentBuildobject usesresultto store the build status like SUCCESS or FAILURE.Step 2: Verify other options
status,state, andoutcomeare not valid properties forcurrentBuild.Final Answer:
currentBuild.result -> Option AQuick Check:
Build status =currentBuild.result[OK]
- Using currentBuild.status which is undefined
- Confusing result with outcome
- Trying to access non-existent properties
echo "Build number: ${currentBuild.number}"
echo "Build status: ${currentBuild.result}"
If the build number is 42 and the build just finished successfully, what will be the output?
Solution
Step 1: Understand variable values after build
After a successful build,currentBuild.numberis the build number (42), andcurrentBuild.resultis "SUCCESS".Step 2: Substitute variables in echo statements
Replacing variables gives:
Build number: 42
Build status: SUCCESSFinal Answer:
Build number: 42 Build status: SUCCESS -> Option AQuick Check:
Number=42, Result=SUCCESS [OK]
- Expecting result to be null before build ends
- Confusing build number with zero
- Assuming result is FAILURE by default
if (currentBuild.status == 'SUCCESS') {
echo 'Build succeeded'
} else {
echo 'Build failed'
}But it always prints 'Build failed' even when the build succeeds. What is the issue?
Solution
Step 1: Identify the incorrect property usage
ThecurrentBuildobject does not have astatusproperty; the correct property for build result isresult.Step 2: Understand why the condition fails
SincecurrentBuild.statusis undefined, the condition never matches 'SUCCESS', so it always goes to else.Final Answer:
The property should be currentBuild.result, not currentBuild.status -> Option DQuick Check:
Use currentBuild.result for status check [OK]
- Using currentBuild.status instead of currentBuild.result
- Confusing assignment (=) with comparison (==)
- Incorrect echo syntax in Jenkinsfile
Solution
Step 1: Identify the correct property to set display name
The property to set the build's display name iscurrentBuild.displayName.Step 2: Use correct variable names and syntax
UsecurrentBuild.numberandcurrentBuild.resultinside the string with Groovy string interpolation.Final Answer:
currentBuild.displayName = "Build #${currentBuild.number} - ${currentBuild.result}" -> Option CQuick Check:
Set displayName with currentBuild.displayName and correct variables [OK]
- Using currentBuild.name instead of displayName
- Using undefined variable 'build'
- Trying to call setDisplayName() which doesn't exist
Ready to prove it?
Pass the test and this pattern moves unknown
