0
0
Jenkinsdevops~15 mins

currentBuild variables in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Using currentBuild Variables in Jenkins Pipeline
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

The first build usually has number 1 and result is null before completion.