Jenkins: find build number for git commit
Install Lucene plugin https://wiki.jenkins.io/display/JENKINS/Lucene-Search and you will be able to search by commit hash just via default Jenkins search bar! (but read plugin docs, for old builds to be searchable you need to rebuild database)
If you want to do it programatically you can use jenkins api, for example http://jenkinsapi.readthedocs.io/en/latest/using_jenkinsapi.html#example-5-getting-version-information-from-a-completed-build
Just modify function in example not to get latest successful build, but to get all builds and get their git hashes then filter this set.
Based on @akostadinov bit I poked around and found build number and other goodies but not GIT_COMMIT.
Maybe this would be useful to someone else so thought I would share what I found.
Open your admin script console with http://yourjenkins:8080/script
and check it out for yourself.
def job = hudson.model.Hudson.instance.getItem("Foo Project")
def builds = job.getBuilds()
def thisBuild = builds[0]
def fourBuildsAgo = builds[4]
println('env' + builds[0].getEnvironment().keySet() )
println('each job has previous job e.g "' + thisBuild.getPreviousBuild() + '"')
fourBuildsAgo.getChangeSets().each {
println('Num of commits in this build ' + (it.getLogs()).size() )
it.getLogs().each {
println('commit data : ' + it.getRevision() + ' ' + it.getAuthor() + ' ' + it.getMsg())
}
}
I used this GitChangeSet API to poke around at the methods in groovy.
This code will fetch and display the commit hashes of each commit 4 builds ago. you can format your currentBuild.description with this text if you want and it will show on your status page.
This resulted in output ( real commit details hidden )
each job has previous job e.g "Foo Project #191"
Num of commits in this build 8
commit data : 288f0e7d3664045bcd0618aacf32841416519d92 user1 fixing the build
commit data : b752ee12b3d804f9a674314bef4de5942d9e02f5 user2 Fix handling to the library foo
commit data : 9067fd040199abe32d75467734a7a4d0d9b6e8b2 user2 Implemented Foo Class
...
...
...