How to get the git tag name in Jenkins in Node configuration

16,241

If you want to pick a tag when running a new job, you can use the Git Parameter Plugin. With this plugin you have new options when you add parameters to your job.

This plugin allows you to assign git tag or revision number as parameter in Parametrized builds. There is no need to set up anything special, this plugin will read your default configuration from Git Plugin.

It firstly fetching (by git fetch) current repository, and listing all the tags/sha1's to choose from.

You can select the following sorting options for tags/revision/branches/branches_or_tags

  • none
  • descending
  • ascending
  • ascending smart
  • descending smart

For the smart variants the compare treats a sequence of digits as a single character. Contributed by Graeme Hill.


There is also the Git Tag Message Plugin which, for any build, will discover if any tag is matching the current commit used for the build. If a tag is found, it's name will be set in the GIT_TAG_NAME environment variable.

Exports the name and message for a git tag as environment variables during a build.

If the revision checked out during a build has a git tag associated with it, its name will be exported during the build as the GIT_TAG_NAME environment variable. If a message was specified when creating the tag (e.g. via git tag -m "..."), then that message will also be exported during the build, as the GIT_TAG_MESSAGE > environment variable.

If the revision has more than one tag associated with it, only the most recent tag will be taken into account. However, if your refspec includes "refs/tags" — i.> e. builds are only triggered when certain tag names or patterns are matched — then the exact tag name that triggered the build will be used, even if it's not the most recent tag for this commit.

Share:
16,241
JaiK
Author by

JaiK

Updated on July 27, 2022

Comments

  • JaiK
    JaiK almost 2 years

    We re using node configuration in Jenkins.In the Post-build Actions we have Git Publisher which has tags to Push e.g 5.1.0.$BUILD_NUMBER.This now becomes the new build number and is used as the release number for testing

    I wanted to access this tag for all the remaining jobs so that I can create the Test report XMLs something with 5.1.0.$BUILD_NUMBER+myjob.xml so that its related to that unique build number.

    However I am not sure how can I retrieve the Git tags in all the individual jobs.I am not too sure that I want to use Global variable string property since its a node configuration where on one node/box there a different build and on other there is a different one.The Global property could overwrite the build versions on each other ?

  • JaiK
    JaiK over 8 years
    Tried GIT_TAG_NAME it does not o/p anything in following command.The Git branch and build number works echo Git branch is %GIT_BRANCH% echo Build number is %BUILD_NUMBER% echo Git Revision is %GIT_COMMIT% echo Git Tag is %GIT_TAG_MESSAGE%
  • JaiK
    JaiK over 8 years
    we are already generating the Tag name with 5.1.0.$BUILD_NUMBER in the first job in Jenkins.Its more about how do I make it available to other jobs as well.I tried global variable,however at Global level the BUILD_NUMBER is not available so I am bit stuck.
  • Thomasleveil
    Thomasleveil over 8 years
    with the Git Tag Message plugin, you can pass to other jobs just the commit hash that matches the tag, and the plugin will figure out which tag matches.
  • JaiK
    JaiK over 8 years
    .Not too sure how I can get the commit hash.So everyday there are many commits to a branch.We then push a tag E.g.5.1.0.$BUILD_NUMBER which is now our revision number.Now say if there are 10 jobs which are triggered after the build there is possibility at say job nos 3-4 to get the wrong tag(as developers would be commiting and pushing stuff and therefore the tag can change from job1 to job3-4). I want the revision number with tag 5.1.0.$BUILD_NUMBER to be pretty much consistent till the next build is trgiggered.Cant I write this to file and read it later on.But not sure how?