Azure DevOps 管道 - 读取管道内的构建标签?

Azure DevOps Pipeline - read a build tag within the pipeline?

我在 ADO 管道中有一个脚本任务,它设置管道构建标记:

echo "##vso[build.addbuildtag]MyTag foo"

现在我正在寻找是否有一种方法可以从同一管道中的其他任务(在不同的 jobs/stages 中)读取该标签?

您可以使用变量和 logging commands。 PowerShell 示例:

pool:
  name: Azure Pipelines
variables:
  my.tag: ''

steps:
- powershell: |
   Write-Host "##vso[task.setvariable variable=my.tag;]build_1"       
  displayName: 'Set var value'

- powershell: |
   Write-Host "##vso[build.addbuildtag]$(my.tag)"       
  displayName: 'Set tag'

- powershell: |
   Write-Host "$(my.tag)"       
  displayName: 'View tag'

您可以使用 rest api to get build info:

pool:
  name: Azure Pipelines
steps:
- powershell: 'Write-Host "##vso[build.addbuildtag]build_1"'
  displayName: 'Set tag'

- powershell: |
   $token = "$(System.AccessToken)" 
   
   $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
   $orgUrl = "$(System.CollectionUri)"
   $teamProject = "$(System.TeamProject)"
   
   $buildId = '$(Build.BuildId)'
   $buildDefId = '$(System.DefinitionId)'
   
   $restGetBuildLogs = "$orgUrl/$teamProject/_apis/build/builds/$buildId" + "?api-version=6.0"
   
   function InvokeGetRequest ($GetUrl)
   {    
       return Invoke-RestMethod -Uri $GetUrl -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}    
   }
   
   
   $build = InvokeGetRequest $restGetBuildLogs
   
   Write-Host $build.tags
  displayName: 'Read tag'