Discover how a simple variable can save you hours of manual build tracking!
Why currentBuild variables in Jenkins? - Purpose & Use Cases
Imagine you are running a Jenkins pipeline and want to track the status, duration, or result of your current build manually by checking logs or external tools.
Manually tracking build details is slow and error-prone because you have to dig through logs or use separate monitoring tools, which can lead to missed failures or delays in reacting to issues.
The currentBuild variable in Jenkins pipelines gives you instant access to build details like status, duration, and result right inside your script, making it easy to automate decisions and notifications.
echo 'Check logs manually for build status'echo "Build status is ${currentBuild.currentResult}"You can automatically respond to build outcomes and customize pipeline behavior based on real-time build information.
Sending a notification email only if the current build fails, using currentBuild.currentResult to decide when to alert the team.
Access build info instantly: status, duration, result.
Automate reactions: trigger actions based on build state.
Save time and reduce errors: no manual log checking needed.
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
