1

I have a script task in an ADO Pipeline which sets a pipeline build tag:

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

Now I am looking if there is a way to read that tag in other task (in different jobs/stages) from within that same pipeline?

silent
  • 14,494
  • 4
  • 46
  • 86

2 Answers2

2

You can use 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'
Shamrai Aleksander
  • 13,096
  • 3
  • 24
  • 31
0

You can use variables and logging commands. Example with 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'
Shamrai Aleksander
  • 13,096
  • 3
  • 24
  • 31
  • Are you sure this will work across stages and jobs? You are basically using variables, I was looking for a way to use tags – silent Mar 21 '22 at 13:01
  • @silent, you can use variables across jobs: https://stackoverflow.com/questions/71419827/how-to-pass-secret-variable-from-one-stage-to-another-azuredevops-pipeline/71420742#71420742 – Shamrai Aleksander Mar 21 '22 at 13:19