Jenkins/Hudson - accessing the current build number?

Continuous IntegrationHudsonJenkinsHudson Plugins

Continuous Integration Problem Overview


I have a report file I'm generating, and I would like to be able to add the current build number to that file within a Jenkins job. Is there an environment variable or plugin I can use to get at the current build number?

Continuous Integration Solutions


Solution 1 - Continuous Integration

BUILD_NUMBER is the current build number. You can use it in the command you execute for the job, or just use it in the script your job executes.

See the Jenkins documentation for the full list of available environment variables. The list is also available from within your Jenkins instance at http://hostname/jenkins/env-vars.html.

Solution 2 - Continuous Integration

I've just come across this question too and found out that if anytime the build number gets corrupt because of any error-triggered hard shutdown of the jenkins instance you can set back the build number manually by just editing the file nextBuildNumber (pathToJenkins\jobs\jobxyz\nextBuildNumber) and then make a reload by using the option Reload Configuration from Disk from the Manage Jenkins View.

Solution 3 - Continuous Integration

Jenkins Pipeline also provides the current build number as the property number of the currentBuild. It can be read as currentBuild.number.

For example:

// Scripted pipeline
def buildNumber = currentBuild.number
// Declarative pipeline
echo "Build number is ${currentBuild.number}"

Other properties of currentBuild are described in the Pipeline Syntax: Global Variables page that is included on each Pipeline job page. That page describes the global variables available in the Jenkins instance based on the current plugins.

Solution 4 - Continuous Integration

As per Jenkins Documentation,

> BUILD_NUMBER

is used. This number is identify how many times jenkins run this build process $BUILD_NUMBER is general syntax for it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStefan KendallView Question on Stackoverflow
Solution 1 - Continuous IntegrationRichard FearnView Answer on Stackoverflow
Solution 2 - Continuous IntegrationHannes KoglerView Answer on Stackoverflow
Solution 3 - Continuous IntegrationMark WaiteView Answer on Stackoverflow
Solution 4 - Continuous Integrationmuhammad shahanView Answer on Stackoverflow