0
0
Jenkinsdevops~5 mins

currentBuild variables in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
When running a Jenkins pipeline, you often want to know details about the current build like its status or number. currentBuild variables give you easy access to this information inside your pipeline scripts.
When you want to check if the current build succeeded or failed to decide next steps.
When you need to get the build number to tag artifacts or logs.
When you want to print the build URL for notifications or reports.
When you want to mark the build as unstable or aborted based on conditions.
When you want to access the current build's duration or result for analytics.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Example') {
      steps {
        script {
          echo "Build number: ${currentBuild.number}"
          echo "Build status: ${currentBuild.currentResult}"
          echo "Build URL: ${currentBuild.absoluteUrl}"
        }
      }
    }
  }
}

This Jenkinsfile defines a simple pipeline with one stage. Inside the stage, it uses currentBuild variables to print the build number, status, and URL. This shows how to access build details during the pipeline run.

Commands
Check that Jenkins Job Builder CLI is installed and working before running pipelines.
Terminal
jenkins-jobs --version
Expected OutputExpected
3.10.0
Trigger the Jenkins pipeline named 'example-pipeline' and wait for it to finish to see the output.
Terminal
jenkins-cli build example-pipeline -s
Expected OutputExpected
[Pipeline] echo Build number: 15 [Pipeline] echo Build status: SUCCESS [Pipeline] echo Build URL: http://jenkins.example.com/job/example-pipeline/15/ [Pipeline] End of Pipeline Finished: SUCCESS
-s - Waits for the build to complete before returning output
View the console output of build number 15 of the 'example-pipeline' to verify currentBuild variables output.
Terminal
jenkins-cli console example-pipeline 15
Expected OutputExpected
[Pipeline] echo Build number: 15 [Pipeline] echo Build status: SUCCESS [Pipeline] echo Build URL: http://jenkins.example.com/job/example-pipeline/15/ [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

If you remember nothing else from currentBuild variables, remember: they let you access live details about the running Jenkins build inside your pipeline script.

Common Mistakes
Trying to use currentBuild variables outside a pipeline script or outside a script block.
currentBuild is only available inside pipeline scripts and must be used inside a script block for Groovy code.
Always use currentBuild inside a 'script { }' block within a pipeline stage.
Assuming currentBuild.result updates immediately after a step.
currentBuild.result reflects the overall build status and may not update until the build finishes.
Use currentBuild.currentResult to get the current status during the build.
Summary
Use currentBuild variables inside Jenkins pipeline scripts to get info about the running build.
Access build number, status, and URL to make decisions or print useful info.
Always use currentBuild inside a script block within pipeline stages.