2
votes

To make it clear: This is not about Git tags. I'm talking about the tags one can add to individual pipeline builds. For example when using the logging command ##vso[build.addbuildtag].

The build pipeline adds a number of tags to its builds. For example the release number, whether the build is for a release candidate, etc...

My question is how I can get these tags in a release pipeline based on the tagged pipeline build.

[EDIT] The solution applied

The way I implement is in a Command task, using Bash. The only dependency is a (secret) variable named "myvars.pat". Which is the personal access token created for this particular case. I've used a variable group for sharing the token with different release pipelines.

# 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    
1
did you examine automatic variables?4c74356b41
If you mean the build-in variables, then yes.JG801
@JG801 Not get your response for several days, would you please share your latest information about this issue?If you have any concern, feel free to share it here.Hugh Lin
@HughLin-MSFT I'm being sidetracked at work currently. So, it's not that I don't want to find the answer. :-)JG801

1 Answers

1
votes

how I can get these tags in a release pipeline based on the tagged pipeline build

For this issue, you can use Tags - Get Build Tags rest api to achieve it. You can add a powershell task to the release pipeline, and then call this rest api through a script to output build tags in the release .

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

Sample script:

$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)"

Get in release:

enter image description here