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
Under the Source Control Management section in your job configuration, if you have selected "Git", then there should be a section labelled "Additional Behaviours". Click "Add" and select "Export git tag and message as environment variables".
In order to list Git tags, you have to use the “git tag” command with no arguments. You can also execute “git tag” with the “-n” option in order to have an extensive description of your tag list. Optionally, you can choose to specify a tag pattern with the “-l” option followed by the tag pattern.
In the "Git Repository" section of your job, under the "Source Code Management" heading, click "Advanced". Under "Branches to build", "Branch specifier", put */tags/<TAG_TO_BUILD> (replacing <TAG_TO_BUILD> with your actual tag name).
Job tag plugin is one simple plugin to help user to add multiple tags to job configuration, and display in list view.
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()
I'd consider returnStdout
rather than writing to a file:
sh(returnStdout: true, script: "git tag --sort version:refname | tail -1").trim()
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
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