This is the same as an answered question from over a year ago: Jenkinsfile get current tag
The accepted answer there is not working for me and I'm thinking the behavior must have changed within the pipeline since then.
My repo has tags (pushed to origin!), and I've added the Jenkins option to discover tags, however I cannot harvest or reference them from the pipeline steps. It just shows up null.
In my MultiBranch Pipeline job I have added the "Discover tags" step.

I have a git repo with a release tag set:
myhost$ git fetch
myhost$ git tag
0.0.42
myhost$ git tag --sort version:refname
0.0.42
In the Jenkinsfile I've tried:
sh "git tag --sort version:refname | tail -1 > version.tmp"
sh "cat version.tmp"
and:
sh(returnStdout: true, script: "git tag --sort version:refname | tail -1").trim()
Both of them return with nothing (null)
You must make sure Jenkins fetches your repo with tags. You can see whether this is the case in the Jenkins build console. There must be a line like git fetch --no-tags ... or git fetch --tags ....
My Jenkins installation seems to change this behavior sometimes without obvious reason. To make sure it fetches tags add Advanced clone behaviors to your Pipeline job with the checkbox Do not fetch tags not marked.

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With