Near the top of my build console I see a "Last Built Revision:" w/ a revision #. How can I access this last built rev # in my build script? I'm using Gradle, but I don't think that matters here. Does Jenkins provide the last built rev # in a system property? Surely this must be trivial to access from my build script...
5 Answers
You can directly access the Jenkins BUILD_NUMBER
as system environment variable.
task getBuildNumber << {
ext.env = System.getenv()
ext.buildNumber = env.BUILD_NUMBER?.toInteger()
println "Build Number: $buildNumber"
}

- 32,442
- 9
- 61
- 82
-
`env.BUILD_NUMBER` does not contain the VCS revision, but Jenkins' build's number. That's useful in some cases, but as far as I can tell it's not what the OP is asking for – David Pärsson Dec 15 '17 at 13:37
-
The build number is also not unique across branches in a multibranch pipeline. This means that if you want to use it to create unique variable names for your builds, they will only be unique within a branch. Not across multiple branches. – jrial Dec 17 '19 at 14:02
Turns out, the Git plugin DOES export the last build revision as an environment variable. So instead of using the accepted answer:
curl -sf "$BUILD_URL/api/xml?xpath=//lastBuiltRevision/SHA1/text()"
you can just use this instead:
GIT_PREVIOUS_COMMIT
One failproof way to see exactly what's available to your build script is to choose Add Build Step > Execute Shell
then simply add the following:
export
view your console (for the build) and you should see lots of great environment variables available to you. The git-related variables that were available to me (using the git plugin) were:
GIT_AUTHOR_EMAIL
GIT_AUTHOR_NAME
GIT_BRANCH
GIT_COMMIT
GIT_COMMITTER_EMAIL
GIT_COMMITTER_NAME
GIT_PREVIOUS_COMMIT
GIT_URL
Lastly, to see a less comprehensive list of available environment variables, you can also just go to this url: http://[your-jenkins-domain-and-port]/env-vars.html

- 17,147
- 17
- 91
- 116
-
Note that these variables are not available (yet) under the pipeline plugin, awaiting resolution of [JENKINS-35230 The environment variables of git plugin not working in pipeline script](https://issues.jenkins-ci.org/browse/JENKINS-35230) – Douglas Royds Oct 14 '16 at 04:48
The current build-number is provided as the Jenkins-variable BUILD_NUMBER
- In Unix it is set for you as ${BUILD_NUMBER}
- In Windows it is available as %BUILD_NUMBER%
The complete list of variables is available on your Jenkins server, at:
http://[your-jenkins-server]/env-vars.html

- 4,005
- 1
- 31
- 39
-
Commenting here since this is the correct answer for getting the Jenkins build number. If however your question was about the Revision number of a Mercurial repository, then you'll want to execute the following command from within a clone repository "hg id -n" and then direct the output appropriately (ie, read it into a variable or log file. – Peter Bernier Jul 18 '12 at 13:15
I do not think the git plugin exports the last built revision as an environment variable, but the information is easily available using a simple shell command like:
curl -sf "$BUILD_URL/api/xml?xpath=//lastBuiltRevision/SHA1/text()"
BUILD_URL always points to the build's own page and the rest of the information seems to be available using the xml api.

- 11,047
- 1
- 27
- 27
The Git plugin returns information from the checkout()
command. In Pipeline script, this can be used to get the previous commit:
def scmVars = checkout scm
scmVars.GIT_PREVIOUS_COMMIT

- 5,662
- 1
- 15
- 21