简体   繁体   English

如何在 Azure 管道中获取 Git 标签

[英]How to get Git Tag in Azure Pipelines

In Azure Pipelines, I have enabled git tags to trigger pipelines like so:在 Azure 管道中,我启用了 git 标签来触发管道,如下所示:

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?管道是从 git 提交还是 git 标记开始的?
  2. If the pipeline was started from a git tag, what is the tag name?如果管道是从 git 标签启动的,标签名称是什么?

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.我已经删除了原始答案,并用 James Thurley 的正确答案替换了它。 I'd delete my answer, but it appears you can't delete an accepted answer.我会删除我的答案,但您似乎无法删除已接受的答案。

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) . git describe可以为你提供给定 git hash 的(最接近的)标签名称,Azure 可以通过$(Build.SourceVersion)为你提供当前的 hash。

Use the --exact-match to limit git describe to only use a tag from the specific commit:使用--exact-matchgit describe限制为仅使用来自特定提交的标签:

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以退出代码 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.因此,您可以在测试中使用它,或者只是让管道中的任务失败,而不仅仅是在标记的修订上触发。

The accepted answer using git tag -lv* 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 .使用git tag -lv*接受的答案对我不起作用,因为它没有正确排序标签,而是给出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.这会为带注释和未注释的标签正确排序标签,因此第一个结果是最近的标签。

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:此处的其他答案涵盖了问题的第一部分,因此正如 Alex Kaszynski 已经指出的那样,您可以使用 YAML 条件:

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.此变量包含最后一个 git ref 路径段,因此例如,如果标签是refs/tags/1.0.2 ,则此变量将包含1.0.2 :标签名称。

Full docs are now here . 完整的文档现在在这里

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. 1) 管道将从 git 标签开始。

2) I don't understand the question, if you pushed tag test so the tag name will be test . 2)我不明白这个问题,如果你推送标签test那么标签名称将是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 .如果您想以编程方式知道构建触发器是否是标签以及标签名称是什么,您可以检查环境变量Build.SourceBranch如果构建来自标签,则该值将是: refs/tags/tagName

So just add a PowerShell task and print the value:所以只需添加一个 PowerShell 任务并打印值:

Write-Host $env:Build_SourceBranch

This need to consider different situations.这需要考虑不同的情况。 If you just push tag or create it with UI, the pipeline are started from git tag.如果您只是推送标签或使用 UI 创建它,则管道将从 git 标签启动。 Just commit without any tag, it will started from git commit.只需提交不带任何标签,它将从 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.为了获得更直观的视图,您可以在内部版本号中添加变量' $(Build.SourceBranch)' Here is my code about how to configure build number in YAML file:这是我关于如何在 YAML 文件中配置内部版本号的代码:

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} .如果标记,它会显示refs_tags_{tagname} ,如果它是提交,它会显示refs_heads_{branchname}

在此处输入图片说明

According the this doc the Tag which started the build can be found in BUILD_SOURCEBRANCH .根据这个文档,可以在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.对于 Azure Pipelines,BUILD_SOURCEBRANCH 将设置为完整的 Git 引用名称,例如 refs/tags/tag_name。

To answer your second question.回答你的第二个问题。 If you don't mind having a separate pipeline for triggering through tags, then you can enable continuous integration and override the YAML trigger like shown below.如果您不介意使用单独的管道来触发标签,那么您可以启用持续集成并覆盖 YAML 触发器,如下所示。 This example will trigger builds with tags having the pattern 'test-*' (regardless of branch).此示例将使用具有模式“test-*”(无论分支如何)的标签触发构建。

在此处输入图片说明

By doing this, you can just execute git describe in your pipeline and it will output the name of the tag that triggered the build.通过这样做,您只需在管道中执行git describe ,它就会输出触发构建的标签的名称。

Here you can see the result:在这里你可以看到结果:

在此处输入图片说明

I realize my answer may not apply to everyone.我意识到我的回答可能不适用于所有人。 However, I wanted to provide it as an alternative for those using an on prem install of Azure DevOps that does not have the replace function yet (2019).但是,我想为那些使用 Azure DevOps 预安装但还没有replace功能的人提供它作为替代(2019 年)。

steps:
- powershell: |
    $tag = "$(Build.SourceBranch)"
    $tag = $tag -replace "refs/tags/", ""
    echo "##vso[task.setvariable variable=tag;isOutput=true]$tag"
  name: createTagVariableStep

This step uses the special syntax to call the setvariable logging command to setup a variable by the name of tag for use within the current job.此步骤使用特殊语法调用setvariable日志记录命令,通过tag名称设置变量以在当前作业中使用。 It can be accessed as $(createTagVariableStep.tag) .它可以作为$(createTagVariableStep.tag)访问。

If you are using a version of Azure DevOps that has the replace function (2020 on prem or Azure DevOps Services) then you can use something like the following:如果您使用的是具有替换功能的 Azure DevOps 版本(2020 on prem 或 Azure DevOps Services),则可以使用以下内容:

variables:      
  tag: $[replace(variables['Build.SourceBranch'], 'refs/tags/', '')]

For either of these options I would use them in combination with a condition对于这些选项中的任何一个,我都会将它们与条件结合使用

job: SomeAwesomeJob
condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/') # only run when there is a tag

Some good answers already for your first question (determine if the build was triggered from a tag).您的第一个问题已经有了一些很好的答案(确定构建是否是从标签触发的)。

To get the name of the tag, you can use the predefined variable Build.SourceBranchName .要获取标签的名称,您可以使用预定义变量Build.SourceBranchName

Check it out here: https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml在这里查看: https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml

The predefined variable Build.SourceBranchName contains alternatively tag name or branch name.预定义变量Build.SourceBranchName包含可选的标记名称或分支名称。 Here is how to read it being sure the value it is always a tag.以下是如何阅读它,确保它的值始终是一个标签。

variables:
- name: gitTag
  ${{ if startsWith(variables['Build.SourceBranch'], 'refs/tags/') }}:
    value: $[variables['Build.SourceBranchName']]
  ${{ else }}:
    value: ''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM