0
0
Jenkinsdevops~10 mins

currentBuild variables in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - currentBuild variables
Start Build
Initialize currentBuild
Set Variables: result, duration, displayName, etc.
Pipeline Steps Execute
Update currentBuild Variables
Access currentBuild Variables in Script
Use Variables for Logic or Reporting
Build Ends
The currentBuild object is created at build start, holds info like status and duration, updates during the build, and can be accessed anytime in the pipeline.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Example') {
      steps {
        script {
          echo "Build status: ${currentBuild.currentResult}"
        }
      }
    }
  }
}
This pipeline prints the current build status using currentBuild.currentResult.
Process Table
StepActioncurrentBuild VariableValueNotes
1Build startsresultnullNo result yet
2Stage 'Example' startsresultnullBuild running
3Inside script blockcurrentResultSUCCESSDefault before failure
4Stage completesresultSUCCESSStage passed
5Build completesresultSUCCESSBuild finished successfully
6Access displayNamedisplayName#1Build number shown
7Access durationduration5000Duration in ms (example)
8Build endsresultSUCCESSPipeline finished
9Exit--Build complete, no more updates
💡 Build finishes, currentBuild variables hold final build info.
Status Tracker
VariableStartAfter Step 3After Step 5After Step 8
resultnullSUCCESSSUCCESSSUCCESS
currentResultnullSUCCESSSUCCESSSUCCESS
displayNamenull#1#1#1
durationnullnull50005000
Key Moments - 3 Insights
Why is currentBuild.result null at the start but currentBuild.currentResult shows SUCCESS inside the script?
At build start, result is not set yet (null). currentResult reflects the current known status during execution, often defaulting to SUCCESS until a failure occurs, as shown in execution_table row 3.
Can currentBuild variables be changed manually in the pipeline?
Yes, some variables like currentBuild.displayName can be set manually to customize build names, but others like duration are updated automatically by Jenkins, as seen in execution_table rows 6 and 7.
What happens to currentBuild variables after the build finishes?
They hold the final build state and info for reporting or downstream steps, as shown in execution_table rows 8 and 9.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of currentBuild.result at step 2?
Anull
BSUCCESS
CFAILURE
DUNKNOWN
💡 Hint
Check the 'currentBuild Variable' and 'Value' columns at step 2 in the execution_table.
At which step does currentBuild.duration get a value?
AStep 5
BStep 7
CStep 3
DStep 9
💡 Hint
Look for 'duration' variable updates in the execution_table rows.
If the build failed at step 5, what would currentBuild.result likely be?
ASUCCESS
Bnull
CFAILURE
DABORTED
💡 Hint
Consider what currentBuild.result represents after stage completion in the execution_table.
Concept Snapshot
currentBuild is a Jenkins pipeline object holding build info.
Key variables: result (final status), currentResult (live status), displayName (build label), duration (build time).
result starts null, updates as build progresses.
Use currentBuild variables in scripts to check or set build info.
Useful for conditional logic and reporting in pipelines.
Full Transcript
The currentBuild object in Jenkins pipelines tracks the build's status and details. It starts empty at build start, then updates variables like result, currentResult, displayName, and duration as the build runs. For example, currentBuild.result is null initially but becomes SUCCESS if the build passes. You can access these variables inside script blocks to make decisions or print info. Some variables like displayName can be set manually to customize the build label. After the build finishes, currentBuild variables hold the final state for reporting or further steps. This visual trace shows how these variables change step-by-step during a simple pipeline.