Bird
Raised Fist0
Jenkinsdevops~10 mins

currentBuild variables in Jenkins - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. What does the currentBuild.number variable represent in a Jenkins Pipeline?
easy
A. The number of failed tests in the build
B. The unique build number of the current running job
C. The number of parallel stages in the pipeline
D. The total number of builds ever run on Jenkins

Solution

  1. Step 1: Understand the role of currentBuild

    The currentBuild variable holds information about the current Jenkins job execution.
  2. Step 2: Identify what number means

    The number property specifically gives the unique build number assigned to this run.
  3. Final Answer:

    The unique build number of the current running job -> Option B
  4. Quick Check:

    currentBuild.number = build number [OK]
Hint: Remember: currentBuild.number is the build's unique ID [OK]
Common Mistakes:
  • Confusing build number with total builds
  • Thinking it counts test failures
  • Mixing it with stage counts
2. Which of the following is the correct way to access the current build's result status in a Jenkins Pipeline script?
easy
A. currentBuild.result
B. currentBuild.status
C. currentBuild.state
D. currentBuild.outcome

Solution

  1. Step 1: Recall the property for build result

    The Jenkins currentBuild object uses result to store the build status like SUCCESS or FAILURE.
  2. Step 2: Verify other options

    status, state, and outcome are not valid properties for currentBuild.
  3. Final Answer:

    currentBuild.result -> Option A
  4. Quick Check:

    Build status = currentBuild.result [OK]
Hint: Use currentBuild.result for build status, not status or state [OK]
Common Mistakes:
  • Using currentBuild.status which is undefined
  • Confusing result with outcome
  • Trying to access non-existent properties
3. Given this Jenkins Pipeline snippet:
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?
medium
A. Build number: 42 Build status: SUCCESS
B. Build number: 42 Build status: null
C. Build number: null Build status: SUCCESS
D. Build number: 0 Build status: FAILURE

Solution

  1. Step 1: Understand variable values after build

    After a successful build, currentBuild.number is the build number (42), and currentBuild.result is "SUCCESS".
  2. Step 2: Substitute variables in echo statements

    Replacing variables gives:
    Build number: 42
    Build status: SUCCESS
  3. Final Answer:

    Build number: 42 Build status: SUCCESS -> Option A
  4. Quick Check:

    Number=42, Result=SUCCESS [OK]
Hint: After success, currentBuild.result = SUCCESS, number = build ID [OK]
Common Mistakes:
  • Expecting result to be null before build ends
  • Confusing build number with zero
  • Assuming result is FAILURE by default
4. You wrote this Jenkins Pipeline snippet:
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?
medium
A. The string comparison should use == instead of =
B. The if statement needs parentheses around the condition
C. The echo command is incorrect syntax
D. The property should be currentBuild.result, not currentBuild.status

Solution

  1. Step 1: Identify the incorrect property usage

    The currentBuild object does not have a status property; the correct property for build result is result.
  2. Step 2: Understand why the condition fails

    Since currentBuild.status is undefined, the condition never matches 'SUCCESS', so it always goes to else.
  3. Final Answer:

    The property should be currentBuild.result, not currentBuild.status -> Option D
  4. Quick Check:

    Use currentBuild.result for status check [OK]
Hint: Use currentBuild.result, not currentBuild.status, for build status [OK]
Common Mistakes:
  • Using currentBuild.status instead of currentBuild.result
  • Confusing assignment (=) with comparison (==)
  • Incorrect echo syntax in Jenkinsfile
5. In a Jenkins Pipeline, you want to set the display name of the current build to include the build number and status, like "Build #42 - SUCCESS". Which snippet correctly does this?
hard
A. currentBuild.displayName = "Build #${build.number} - ${build.result}"
B. currentBuild.name = "Build #${currentBuild.number} - ${currentBuild.status}"
C. currentBuild.displayName = "Build #${currentBuild.number} - ${currentBuild.result}"
D. currentBuild.setDisplayName("Build #${currentBuild.number} - ${currentBuild.result}")

Solution

  1. Step 1: Identify the correct property to set display name

    The property to set the build's display name is currentBuild.displayName.
  2. Step 2: Use correct variable names and syntax

    Use currentBuild.number and currentBuild.result inside the string with Groovy string interpolation.
  3. Final Answer:

    currentBuild.displayName = "Build #${currentBuild.number} - ${currentBuild.result}" -> Option C
  4. Quick Check:

    Set displayName with currentBuild.displayName and correct variables [OK]
Hint: Set displayName with currentBuild.displayName and correct variables [OK]
Common Mistakes:
  • Using currentBuild.name instead of displayName
  • Using undefined variable 'build'
  • Trying to call setDisplayName() which doesn't exist
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard