Jenkinsfile get current tag
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()
Related videos on Youtube
Comments
-
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 about 7 yearsYep, this is the correct answer, and was present in the documentation at the time I gave my answer.
-
Rob Wilkinson almost 7 yearshow can I set a variable equal to that result?
-
Rob Wilkinson almost 7 yearsalso, an easier script is
git describe --tags --long --dirty --always
-
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 about 5 yearsWhat's the output if there are multiple tags on HEAD?
-
Florian about 5 yearsThen 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 over 4 yearsThis command doesn't return the current tag in the current branch, but the latest tag in general.
-
Max Cascone almost 4 yearsbuilding 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 almost 4 yearsThis should not be the accepted answer. It does not do what the OP asked. Instead, the answer by Florian does.
git tag --contains
orsh(returnStdout: true, script: "git tag --contains").trim()
-
Lester about 3 yearsThis returns the latest tag, not the current tag.
-
Kevin almost 3 yearsMultiBranch Pipelines need Branch Sources configured for the Behavior "Discover tags". jenkins.io/blog/2018/05/16/pipelines-with-git-tags