How can I access the last built revision according to jenkins?
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"
}
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
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