在完成时从Azure Devops构建管道设置Git标记 [英] Setting Git Tag from Azure Devops Build Pipeline on Complete

查看:145
本文介绍了在完成时从Azure Devops构建管道设置Git标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用成功构建结束时在GIT提交上使用GitVersion确定的当前版本号设置标签.感觉我不能成为第一个这样做的人,但是我正在努力寻找可行的方法.

Azure Devops Pipeline在获取源"中具有标记源"功能.成功.我已经将此设置并设置为由我拥有的代理任务之一(GitVersion)设置的变量



我在调试日志中看到,该变量是由我添加到管道中的GitVersion组件设置的.

2019-12-06T20:54:20.2390794Z ##[debug]Processed: ##vso[task.setvariable variable=GitVersion.MajorMinorPatch;]2.98.0

但是,如果我只保留原样,则会得到一个标记为"v $(GitVersion.MajorMinorPatch)"的标签.这意味着在创建标签时,该变量不再存在.

标签格式帮助工具提示

标签格式可以是范围为全部"的用户定义或预定义变量的组合.例如:'$(Build.DefinitionName) $(Build.DefinitionVersion) $(Build.BuildId) $(Build.BuildNumber) $(My.Variable)'" ;

所以我想问题是在管道中创建的此变量没有All范围.

然后,我尝试将管道变量添加到"GitVersion.MajorMinorPatch"管道中.希望这是在正确的范围内,并希望当"task.setvariable"命令运行,这将设置此更高范围变量的变量值.

但是在这种情况下,我只是获得了一个标记"v",已创建.

所以我有点卡住了.我需要以某种方式能够在作用域ALL上动态创建或设置一个变量,该变量具有我要在此处标记的值.

我真的很感谢任何想法.

解决方案

如果您正在执行yaml管道,则可以添加以下步骤

- checkout: self
  persistCredentials: true

## Rest of pipeline ##

- script: |
     git tag $(GitVersion.NugetVersionV2)
     git push origin $(GitVersion.NugetVersionV2)
  workingDirectory: $(Build.SourcesDirectory)

persistCredentials允许令牌自动传递给其他git命令.请注意workingDirectory的分配,否则我将收到一个错误消息,该位置不是git存储库.

对于带注释的标记而不是轻量级标签,语法看起来像这样……

- script: |
     git tag -a <tagname> -m <message>

要针对该用户/日期,您需要

要使用此功能,请使用 Project Collection Build Server 帐户(而不是 Project Build Service Accounts )需要分配存储库的贡献"权限

I'm trying to set a tag with the current version number determined by GitVersion on the GIT commit at the end of a successful build. Feels like I can't be the first one to be doing this, but I'm struggling to find something that works.

Azure Devops Pipeline has a feature in Get Sources to "Tag sources" On Success. I've set this and set to a variable that is set by one of the Agent Tasks I have (GitVersion)



I can see in the debug logs that this variable is getting set by the GitVersion component that I've added to the pipeline.

2019-12-06T20:54:20.2390794Z ##[debug]Processed: ##vso[task.setvariable variable=GitVersion.MajorMinorPatch;]2.98.0

However if I leave it just as this, I get a tag created as "v$(GitVersion.MajorMinorPatch)" which means that at the time that the tag is being created that that variable no longer exists.

The Tag Format help tooltip says

"Tag format could be a combination of user-defined or pre-defined variables that have a scope of "All". For example: '$(Build.DefinitionName)$(Build.DefinitionVersion)$(Build.BuildId)$(Build.BuildNumber)$(My.Variable)'"

So I guess the problem is that this variable created during the pipeline does not have a scope of All.

I then tried adding a pipeline variable to the pipeline of "GitVersion.MajorMinorPatch" with the hope that this was at the right scope and hoping that when the "task.setvariable" command is run, that this will set the variable value of this higher scoped variable.

However in this case I just got a tag "v" created.

So I am a bit stuck. Somehow I need to be able to dynamically create or set a variable at scope ALL with the value I want to tag here.

I'd be really grateful for any ideas on this.

解决方案

If you are doing a yaml pipeline, you can add the following steps

- checkout: self
  persistCredentials: true

## Rest of pipeline ##

- script: |
     git tag $(GitVersion.NugetVersionV2)
     git push origin $(GitVersion.NugetVersionV2)
  workingDirectory: $(Build.SourcesDirectory)

The persistCredentials allows the token to be automatically passed to other git commands. Note the assignment of workingDirectory, otherwise I had an error that the location was not a git repository.

For an annotated tag rather than lightweight tag, the syntax would look like this...

- script: |
     git tag -a <tagname> -m <message>

To get a user/date against it you need to set the user name/email as well e.g.

- script: |
    git config --global user.name "BuildService"
    git config --global user.email "autobuild@fabrikam.com"
    git tag -a <tagname> -m <message>

For this to work, the Project Collection Build Server account (not the Project Build Service Accounts group) needs to be allocated the Contribute permission for the Repositories

这篇关于在完成时从Azure Devops构建管道设置Git标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆