How to get Git Tag in Azure Pipelines
Solution 1
To check if the commit was from a tag, use:
startsWith(variables['Build.SourceBranch'], 'refs/tags/')
From James Thurley:
Get the name of the tag with:
$tags = git tag --sort=-creatordate
$tag = $tags[0]
This sorts the tags correctly for both annotated and unannotated tags, and so the first result is the most recent tag.
I've removed the original answer and replaced it with the correct one from James Thurley. I'd delete my answer, but it appears you can't delete an accepted answer.
Solution 2
The other answers here cover the first part of the question, so as Alex Kaszynski has already pointed out, you can use a YAML condition:
startsWith(variables['Build.SourceBranch'], 'refs/tags/')
Getting the tag name is now a bit easier than it was at the time the question was asked:
Build.SourceBranchName
This variable contains the last git ref path segment, so for example if the tag was refs/tags/1.0.2
, this variable will contain 1.0.2
: the tag name.
Solution 3
git describe
can provide you with the (closest) tag name for a given git hash and Azure can give you the current hash with $(Build.SourceVersion)
.
Use the --exact-match
to limit git describe
to only use a tag from the specific commit:
git describe --exact-match $(Build.SourceVersion)
If there is a tag, it'll be returned on stdout:
$ git describe --exact-match d9df242
v1.0.0
If there is no tag, git describe --exact-match
exits with exit code 128:
$ git describe --exact-match cc1f9d2
fatal: no tag exactly matches 'cc1f9d23854c37dec000485c6c4009634516a148'
$ echo $?
128
so you can use this in a test or simply fail the task in pipelines that trigger on more than just tagged revisions.
Solution 4
The accepted answer using git tag -l v*
didn't work for me as it didn't order the tags correctly, instead giving 1.1, 1.11, 1.12, 1.2, 1.3, etc
.
I found it better to do:
$tags = git tag --sort=-creatordate
$tag = $tags[0]
This sorts the tags correctly for both annotated and unannotated tags, and so the first result is the most recent tag.
Solution 5
When you configure the pipeline to be triggers with tag the meaning that when new tag is pushed the pipeline start to run. so:
1) The pipeline will start from the git tag.
2) I don't understand the question, if you pushed tag test
so the tag name will be test
.
If you want to know programmatically if the build trigger was a tag and what is the tag name you can check the environment variable Build.SourceBranch
if the build is from a tag the value will be: refs/tags/tagName
.
So just add a PowerShell task and print the value:
Write-Host $env:Build_SourceBranch
Muhammad Rehan Saeed
▶️ YouTube: https://youtube.com/MuhammadRehanSaeed 📰 Website/Blog: https://rehansaeed.com 🐦 Twitter: https://twitter.com/RehanSaeedUK 🐙 GitHub: https://github.com/RehanSaeed 👨🏻💼 LinkedIn: https://www.linkedin.com/in/muhammad-rehan-saeed 📚 StackOverflow: https://stackoverflow.com/users/1212017/muhammad-rehan-saeed 👁️🗨️ Twitch: https://www.twitch.tv/rehansaeeduk
Updated on July 10, 2022Comments
-
Muhammad Rehan Saeed almost 2 years
In Azure Pipelines, I have enabled git tags to trigger pipelines like so:
trigger: branches: include: - '*' tags: include: - '*'
Now I want to know if there is a way to determine programmatically:
- Was the pipeline started from a git commit or git tag?
- If the pipeline was started from a git tag, what is the tag name?