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:
Find Latest Git Tag Available In order to find the latest Git tag available on your repository, you have to use the “git describe” command with the “–tags” option. This way, you will be presented with the tag that is associated with the latest commit of your current checked out branch.
You can view tags in the Tags view and in the Commits view in the web portal. With Azure DevOps Services, the format for the project URL is dev.azure.com/{your organization}/{your project} .
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.
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.
Full docs are now here.
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