Skip to content

Instantly share code, notes, and snippets.

@GustavoAmerico
Last active October 22, 2022 18:02
Show Gist options
  • Save GustavoAmerico/5f6f73322f29914631282554273df22c to your computer and use it in GitHub Desktop.
Save GustavoAmerico/5f6f73322f29914631282554273df22c to your computer and use it in GitHub Desktop.
Script to copy Azure DevOps pullrequest tags to AzureDevOps Pipeline Build
$tagNameExpression = "target-*"
# Define the PR URI based in azure devops pipeline runtime variables
$prUri = "$(System.CollectionUri)/$(System.TeamProject)/_apis/git/repositories/$(Build.Repository.ID)/pullRequests/$(System.PullRequest.PullRequestId)/labels?api-version=6.0-preview.1";
$buildUri= "$(System.CollectionUri)/$(System.TeamProject)/_apis/build/builds/$(Build.BuildId)/tags";
# Get PullRequest Details
$FoundTags = Invoke-WebRequest `
-Uri $prUri `
-Authentication Bearer `
-Token (ConvertTo-SecureString "$(System.AccessToken)" -AsPlainText -force) `
ConvertFrom-Json #Convert result in JSON | `
Select-Object -ExpandProperty Value | ` #Select the property Value of object returned
Where-Object { $_.active -eq $True -and $_.name -like $tagNameExpression } | ` #Filter active tags by name with match tagNameExpression
Select-Object -Property Id, Name;
$FoundTags | ForEach-Object -Process {
$tag=[System.Web.HTTPUtility]::UrlEncode($_.name);
$tagUri = ("$buildUri/$tag" + "?api-version=6.0");
Write-Host "Adicionando tag $tagUri";
Invoke-WebRequest -Method PUT `
-Uri $tagUri `
-Authentication Bearer `
-Token (ConvertTo-SecureString "$(System.AccessToken)" -AsPlainText -force); }; `
if($FoundTags) {
echo "##vso[task.setvariable variable=FoundTags;isOutput=true] $FoundTags"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment