Jenkins/Hudson - accessing the current build number?
Asked Answered
C

4

91

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?

Cassandra answered 23/8, 2011 at 21:15 Comment(0)
M
128

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.

Malvie answered 23/8, 2011 at 21:26 Comment(1)
Richard Fearn can you please help me to be more specific abt your ans. i need to get buildnumber of my jenkins job . i'm using Java + maven+ jenkinsAccrescent
Q
21

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.

Quartermaster answered 8/9, 2020 at 6:11 Comment(1)
thank you for short and to the point answerEmulation
M
12

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.

Martijn answered 29/11, 2012 at 12:24 Comment(0)
D
6

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.

Diva answered 25/9, 2020 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.