【问题标题】:How to get build pipeline tags from Azure release pipeline?如何从 Azure 发布管道获取构建管道标签?
【发布时间】:2019-12-10 15:09:12
【问题描述】:

明确一点:这不是关于 Git 标签的。我说的是可以添加到单个管道构建中的标签。例如,使用日志记录命令 ##vso[build.addbuildtag].

构建管道在其构建中添加了许多标签。例如版本号、构建是否适用于候选版本等...

我的问题是如何根据标记的管道构建在发布管道中获取这些标签。

[编辑]应用的解决方案

我实现的方式是在命令任务中,使用 Bash。唯一的依赖是一个名为“myvars.pat”的(秘密)变量。这是为这种特殊情况创建的个人访问令牌。我使用了一个变量组来与不同的发布管道共享令牌。

# Construct the url based on the environment
ORG=$(basename $(System.CollectionUri))
URL=https://dev.azure.com/$ORG/$(Build.ProjectName)/_apis/build/builds/$(Build.BuildId)/tags?api-version=5.1

# Base64 the PAT token. Mind the ':' prefix !
PAT=$(echo -n ":$(myvars.pat)" | base64)

# Make the GET request. "-L" is needed to follow redirects.
curl -L -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Basic $PAT" -s -o ./tags.json $URL

# Using default tools to extract the array. No doubt there is a cleaner way.
tags=$(cat ./tags.json | cut -d "[" -f 2 | cut -d "]" -f 1 | tr -d '"' | tr ',' ' ')

# There are the tags
for tag in $tags; do
  echo $tag
done

# Set them as a variable to be used by the subsequent tasks
echo "##vso[task.setvariable variable=Build.Tags;]$tags"

# Clean up
rm ./tags.json    

【问题讨论】:

  • 你检查过自动变量吗?
  • 如果你的意思是内置变量,那么是的。
  • @JG801 好几天没有收到您的回复,能否分享一下您对此问题的最新信息?如果您有任何问题,请随时在这里分享。
  • @HughLin-MSFT 我目前在工作中分心。所以,并不是我不想找到答案。 :-)

标签: azure-devops azure-pipelines azure-pipelines-release-pipeline


【解决方案1】:

如何根据标记在发布管道中获取这些标记 管道构建

对于这个问题,可以使用Tags - Get Build Tagsrest api 来实现。你可以在发布管道中添加一个powershell任务,然后通过脚本调用这个rest api来输出发布中的构建标签。

 GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/tags?api-version=5.1

示例脚本:

$personalAccessToken="{yourPAT}"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))
$header = @{Authorization=("Basic {0}" -f $token)}
$Url = "https://dev.azure.com/{org}/{pro}/_apis/build/builds/{buildId}/tags?api-version=5.1"
$tags = Invoke-RestMethod -Uri $Url -Method Get  -Headers $header
Write-Host  "Tags = $($tags.value| ConvertTo-Json -Depth 1)"

进入发布:

【讨论】:

  • 我认为这是最后的措施。没有别的办法吗?
  • 目前我能想到的唯一办法就是通过rest API。
猜你喜欢
  • 1970-01-01
  • 2019-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-11
  • 2020-05-03
  • 1970-01-01
相关资源
最近更新 更多