📖 Scenario: You are working on a Jenkins pipeline to automate your software build process. You want to track and display information about the current build, such as its number and result status, to help your team monitor the build progress easily.
🎯 Goal: Build a simple Jenkins pipeline script that uses currentBuild variables to access and display the build number and build result.
📋 What You'll Learn
Create a variable to store the current build number using currentBuild.number
Create a variable to store the current build result using currentBuild.result
Print both the build number and build result in the pipeline script
💡 Why This Matters
🌍 Real World
Jenkins pipelines often need to report build status and details to users or other systems for monitoring and alerting.
💼 Career
Knowing how to use <code>currentBuild</code> variables is essential for DevOps engineers to create informative and responsive CI/CD pipelines.
Progress0 / 4 steps
1
Create a variable for the current build number
Create a variable called buildNumber and set it to currentBuild.number to get the current build number.
Jenkins
Hint
The currentBuild.number gives you the number of the current build in Jenkins.
2
Create a variable for the current build result
Create a variable called buildResult and set it to currentBuild.result to get the current build result status.
Jenkins
Hint
The currentBuild.result holds the status of the build like SUCCESS, FAILURE, or null if not finished.
3
Use the variables in a print statement
Write a println statement that prints the text: "Build number: " followed by buildNumber and " with result: " followed by buildResult.
Jenkins
Hint
Use Groovy string interpolation with ${variable} inside double quotes to print variables.
4
Display the build information
Run the pipeline script and ensure the output shows the build number and build result in the format: Build number: X with result: Y where X is the build number and Y is the build result.
Jenkins
Hint
The first build usually has number 1 and result is null before completion.
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
Step 1: Understand the role of currentBuild
The currentBuild variable holds information about the current Jenkins job execution.
Step 2: Identify what number means
The number property specifically gives the unique build number assigned to this run.
Final Answer:
The unique build number of the current running job -> Option B
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
Step 1: Recall the property for build result
The Jenkins currentBuild object uses result to store the build status like SUCCESS or FAILURE.
Step 2: Verify other options
status, state, and outcome are not valid properties for currentBuild.
Final Answer:
currentBuild.result -> Option A
Quick Check:
Build status = currentBuild.result [OK]
Hint: Use currentBuild.result for build status, not status or state [OK]
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
Step 1: Identify the incorrect property usage
The currentBuild object does not have a status property; the correct property for build result is result.
Step 2: Understand why the condition fails
Since currentBuild.status is undefined, the condition never matches 'SUCCESS', so it always goes to else.
Final Answer:
The property should be currentBuild.result, not currentBuild.status -> Option D
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
Step 1: Identify the correct property to set display name
The property to set the build's display name is currentBuild.displayName.
Step 2: Use correct variable names and syntax
Use currentBuild.number and currentBuild.result inside the string with Groovy string interpolation.
Final Answer:
currentBuild.displayName = "Build #${currentBuild.number} - ${currentBuild.result}" -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.