While you are using Azure DevOps pipelines to perform build and deployment, you can select Azure DevOps agent or Deployment group as a build and deployment agent. If we consider about the Deployment group, there can be one or multiple deployments targets. While you are working with deployment group with multiple targets, there can be situations where you need to do a specific changes only for the selected deployment target. You achieve that via adding a new tag to deployment target. This blog explains how to add new tag to deployment target via a script.
prerequisite:
- Azure DevOps deployment group with a deployment target
- Azure DevOps personal access token (PAT)
Let's get started.
First we need to create an authentication header using an Azure DevOps PAT.
$AzureDevOpsPAT = "your PAT"
$User="";
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User,$AzureDevOpsPAT)));
$header = @{Authorization=("Basic {0}" -f $base64AuthInfo)};
Next, we need to identify the relevant Azure DevOps REST API call. As initial step, create the url which is going to be triggered using a web request.
You need deployment group id and target id to create the url. So, go to Deployment group page of Azure DevOps and get deployment group id and deployment target id from the page url as shown in the following picture.
$deployMentGroupId = 639;
$uri = 'https://dev.azure.com/nilminih/Project%20X/_apis/distributedtask/deploymentgroups/'+ $deployMentGroupId +'/targets?api-version=6.0-preview.1';
Next, step is to create a body of the web request. Inside body section, put the new deployment target tag along with all the old tags. Otherwise you would lose the old tags.
$machineId = '33';
$body = '[
{
"tags": [
"db",
"web",
"Test Script"
],
"id": '+ $machineId + '
}
]';
Finally, Invoke the rest method to add a new tag to deployment target. You can add this script to Azure DevOps pipeline and add deployment target tag on demand.
$result = Invoke-RestMethod -Uri $uri -Method Patch -ContentType application/json -Body $body -Headers $header
Check the relevant deployment target of the deployment group. You would be able to find deployment target with newly added tag.
This blog explained how to add a new tag to a deployment target on demand via Azure DevOps REST API script.
No comments:
Post a Comment