How to get Git Tag in Azure Pipelines

≯℡__Kan透↙ 提交于 2020-04-09 07:46:41

问题


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?

回答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.




回答2:


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



回答3:


This need to consider different situations. If you just push tag or create it with UI, the pipeline are started from git tag. Just commit without any tag, it will started from git commit. No doubt, the build will be triggered just once.

But if you push commit with tag, the build will be triggered twice. First is triggered by commit, and second is by tag. Check this pic.

These means the pipeline started from a commit instead of tag.

All in all, no matter which is first, the tag which trigger the build are all you pushed or created.

For getting more intuitive view about this, you can add variable ' $(Build.SourceBranch)' in your build number. Here is my code about how to configure build number in YAML file:

name: $(Build.SourceBranch)-$(date:yyyyMMdd)$(rev:.r)
trigger:
  branches:
    include:
    - '*'
  tags:
    include:
    - '*'

Here is the result of what triggered the build. If tag, it will shows refs_tags_{tagname}, if it's commit, it will shows refs_heads_{branchname}.




回答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.




回答5:


According the this doc the Tag which started the build can be found in BUILD_SOURCEBRANCH.

If this build was queued by the creation of a tag then this is the name of that tag. For Azure Pipelines, the BUILD_SOURCEBRANCH will be set to the full Git reference name, eg refs/tags/tag_name.




回答6:


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.



来源:https://stackoverflow.com/questions/56326940/how-to-get-git-tag-in-azure-pipelines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!