Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins: find build number for git commit

Each commit to my git repo triggers a build of my Jenkins pipeline.

I want to retrieve the buildNumber of an old build by commit hash. How can I do that?

I know this information exists because I can find it in the UI.


Some background as to why I want this: When someone tags a commit I want to create a release (by tagging a docker image). Each build will push an image containing the build number. So I want to find out which build number corresponds to that commit so that I can find the image that I want to release.

like image 202
herm Avatar asked Aug 21 '17 15:08

herm


People also ask

How do I find my Jenkins build number?

Navigate to the Jenkins dashboard and select Build History. This timeline displays your build history.

What is build number in Git?

The build number is the number of commits between the current commit and the tag. To guarantee an increasing build number, all these conditions must be satisfied: Make all releases from the same Git repository. Make all releases from the same branch.

How do I build on specific commit in Git on Jenkins?

Pass the commit sha1 to a predefined parameter and in Build-Execute shell run git checkout <commit> before the build process starts. Some extra work is needed to make sure the check-out goes well. Check the box This project is parameterized and then you can add predefined parameters.

What are Git commit numbers?

Commit IDs are unique SHA-1 hashes that are created whenever a new commit is recorded. If you specify a commit ID when you add a repository, Domino will always pull the state of the repository specified by that commit in a detached HEAD state.


2 Answers

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.

like image 118
ka4eli Avatar answered Oct 18 '22 03:10

ka4eli


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
...
...
...
like image 27
Peter Moore Avatar answered Oct 18 '22 03:10

Peter Moore