Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Git Tag in Azure Pipelines

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:

  1. Was the pipeline started from a git commit or git tag?
  2. If the pipeline was started from a git tag, what is the tag name?
like image 645
Muhammad Rehan Saeed Avatar asked May 27 '19 13:05

Muhammad Rehan Saeed


People also ask

How do I get git tags?

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.

How do I view Azure DevOps tags?

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} .


2 Answers

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.

like image 75
Alex Kaszynski Avatar answered Oct 06 '22 00:10

Alex Kaszynski


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.

like image 32
Mark Bell Avatar answered Oct 05 '22 23:10

Mark Bell