Jenkinsfile get current tag

40,660

Solution 1

I'd consider returnStdout rather than writing to a file:

sh(returnStdout: true, script: "git tag --sort version:refname | tail -1").trim()

Solution 2

All the other answers yield an output in any case even if HEAD is not tagged. The question was however to return the current tag and "null" if there is nothing like that.

git tag --contains yields the tag name name if and only if HEAD is tagged.

For Jenkins Pipelines it should look like this:

sh(returnStdout: true, script: "git tag --contains").trim()

Solution 3

The TAG_NAME should work now at least in declarative pipelines.

When condition actually filters on this property. BRANCH_NAME has the same value.

stage('release') {
   when {
     tag 'release-*'
   }
   steps {
     echo "Building $BRANCH_NAME"
     echo "Building $TAG_NAME"
   }
}

See https://jenkins.io/doc/book/pipeline/syntax/#when

Solution 4

If the current build is a tag build -- as in, when { buildingTag() } was "true" -- then the (undocumented) environment variable BRANCH_NAME contains the name of the tag being build.

Solution 5

best way from my site is:

git tag --sort=-creatordate | head -n 1

with:

latestTag = sh(returnStdout:  true, script: "git tag --sort=-creatordate | head -n 1").trim()

Than you can handle with simple regex, for prefix/suffix/version_number what is to do with the tag.

other solution:

git describe --tags --abbrev=0

of course this is the current/latest tag in git. Independent from writing.

sh(returnStdout: true, script: "git describe --tags --abbrev=0").trim()
Share:
40,660

Related videos on Youtube

ligi
Author by

ligi

https://ligi.de

Updated on January 13, 2021

Comments

  • ligi
    ligi over 3 years

    Is there a way to get the current tag ( or null if there is none ) for a job in a Jenkinsfile? The background is that I only want to build some artifacts ( android APKs ) when this commit has a tag. I tried:

    env.TAG_NAME
    

    and

    binding.variables.get("TAG_NAME")
    

    both are always null - even though this ( https://issues.jenkins-ci.org/browse/JENKINS-34520 ) indicates otherwise

  • rbellamy
    rbellamy about 7 years
    Yep, this is the correct answer, and was present in the documentation at the time I gave my answer.
  • Rob Wilkinson
    Rob Wilkinson almost 7 years
    how can I set a variable equal to that result?
  • Rob Wilkinson
    Rob Wilkinson almost 7 years
    also, an easier script is git describe --tags --long --dirty --always
  • Matt
    Matt almost 6 years
    @RobWilkinson by just setting a variable i.e def foo = sh(returnStdout: true, script: "git tag --sort version:refname | tail -1").trim()
  • Raphael
    Raphael about 5 years
    What's the output if there are multiple tags on HEAD?
  • Florian
    Florian about 5 years
    Then the git command yields a multiline output. To take multiple tags on HEAD into account you could do something like git tag --contains | head -1.
  • Gerardo Figueroa
    Gerardo Figueroa over 4 years
    This command doesn't return the current tag in the current branch, but the latest tag in general.
  • Max Cascone
    Max Cascone almost 4 years
    building on @RobWilkinson 's response, git describe has some useful functionality: git describe --tags shows the tag + how many commits since that tag, and a hash id. eg on master, v1.1.0.2-0-gcf75da0 means no commits since v1.1.0.2, on another branch, v1.1.0.2-8-g47938eb means 8 commits since that tag.
  • Tom17
    Tom17 almost 4 years
    This should not be the accepted answer. It does not do what the OP asked. Instead, the answer by Florian does. git tag --contains or sh(returnStdout: true, script: "git tag --contains").trim()
  • Lester
    Lester about 3 years
    This returns the latest tag, not the current tag.
  • Kevin
    Kevin almost 3 years
    MultiBranch Pipelines need Branch Sources configured for the Behavior "Discover tags". jenkins.io/blog/2018/05/16/pipelines-with-git-tags

Related