我是Azure DevOps的新手。我需要向存储库添加标签,并且在执行构建时,它应该从txt文件中读取标签名称并进行相应的创建。
答案 0 :(得分:0)
During the build successful completion, it should create the tag for that successful build.
我认为自动创建标签的最简单方法是使用Tags REST API。
其背后的想法是在成功构建的末尾添加一个附加脚本,该脚本将调用API并创建必要的标签。
该脚本可能类似于以下内容:
- script: |
export TAG_NAME="$(cat tag-file.txt)" ##Somehow export the tag name as env variable
export ORGANIZATION="organization_name" ##If you use variable groups, you can add it there and skip this line.
curl -d "" -H "Content-Type: application/json" -H "Authorization: Basic $(PERONSAL_ACCESS_TOKEN)" \
-X PUT https://dev.azure.com/$(ORGANIZATION)/$(System.TeamProject)/_apis/build/builds/$(Build.BuildId)/tags/$(TAG_NAME)?api-version=6.0
condition: succeeded()
您可以按照THIS指令创建个人访问令牌。使用此令牌调用REST API之前,必须将其转换为Base64
字符串。为此,您可以使用THIS脚本。
答案 1 :(得分:0)
LJ。共享了使用YAML管道的解决方案。您可以将该部分直接添加到xx.yml
文件中。
根据您的描述,似乎您正在使用经典UI编辑器。
然后,您可以使用Bash task或PowerShell task来调用脚本。
此外,Rest API正在向构建中添加标签。
由于您只想标记存储库,因此可以直接使用git命令。
然后,您需要授予Project Collection Build Service 创建标签和贡献(添加/删除文件)权限。
有关详细示例,请参阅git commit and git tag in azure devops yml based pipeline
答案 2 :(得分:0)
这是一个示例 YAML 管道任务,用于创建带注释的 Git 标签。
与调用 git tag
命令相比,我看到的主要好处是您的管道不需要 Contribute
权限。拥有 Create Tag
权限就足够了。
- task: PowerShell@2
displayName: Create Git Tag using inline script
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken) # enables you to use the $(System.AccessToken) to authenticate the REST call
inputs:
targetType: 'inline'
script: |
$rootUrl = "$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI$env:SYSTEM_TEAMPROJECTID/_apis"
$authorizationHeader = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" }
$message = "YOUR GIT TAG MESSAGE" # TODO: set your message
$body = "{ `"name`": `"$Env:BUILD_BUILDNUMBER`", `"taggedObject`": { `"objectId`": `"$Env:BUILD_SOURCEVERSION`" }, `"message`": `"$message`" }"
$createTagUrl = "$rootUrl/git/repositories/$($env:Build_Repository_Name)/annotatedtags?api-version=6.0" # api-version=6.0 may change in the future!
$createTagResponse = Invoke-RestMethod -Uri $createTagUrl -Method POST -Headers $authorizationHeader -Body $Body -ContentType application/json
if ($createTagResponse -ne $Null)
{
Write-Host $createTagResponse
Write-Host "Git Tag Created!"
}
failOnStderr: true