Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access the last built revision according to jenkins?

Tags:

gradle

jenkins

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...

like image 502
Ray Nicholus Avatar asked Jul 17 '12 16:07

Ray Nicholus


3 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"
}
like image 143
Benjamin Muschko Avatar answered Sep 19 '22 06:09

Benjamin Muschko


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

like image 35
gMale Avatar answered Sep 22 '22 06:09

gMale


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.

like image 24
sti Avatar answered Sep 21 '22 06:09

sti