Jenkins: find build number for git commit

Each commit of my git repository starts building my Jenkins pipeline.

I want to get the buildNumber of an old assembly using commit hashing. How can i do this?

I know that this information exists because I can find it in the user interface.


Some prerequisites why I want this: When someone writes a commit, I want to create a release (by marking the docker image). Each assembly will display an image containing the assembly number. So I want to know which assembly number corresponds to this commit so that I can find the image I want to release.

+7
source share
3 answers

Install the Lucene plugin https://wiki.jenkins.io/display/JENKINS/Lucene-Search, and you can search for the hash of commits only through the default Jenkins search bar! (but read the documents on plugins, in order for the old assemblies to be searchable, you need to rebuild the database)

If you want to do this programmatically, you can use the jenkins api, e.g. http://jenkinsapi.readthedocs.io/en/latest/using_jenkinsapi.html#example-5-getting-version-information-from-a-completed-build

Just change the function in the example so as not to get the last successful build, but to get all the builds and get their git hashes, and then filter this set.

+5
source

If you want to get commit identifiers for assemblies, you can use the groovy script, for example:

def job = hudson.model.Hudson.instance.getItem("My Job Name") def builds = job.getBuilds() 

Then for each git repo you clone you can get a revision

 build.getEnvironment()["GIT_COMMIT"] build.getEnvironment()["GIT_COMMIT_4"] 
0
source

If I understand your question correctly, you first need to create a git hook to start a new build. This part is covered in the answer. How can I respond to new tags in git hooks? "though, if you use something like GitHub, BitBucket or Gitlab, then there may be other ways around this.

Then, when the assembly begins, there is an assembly number that is provided as the variable ' BUILD_NUMBER ' in Jenkins. If you want to include the git tag name, so you can use it in a script, then there seem to be several ways:

Typically, these plugins will create an environment variable that can be used by your scripts. I do not give more specific examples, since I do not know your exact tool.

-1
source

Source: https://habr.com/ru/post/1271061/


All Articles