I have set up a powershell script which runs a build pipeline within Azure Devops via the REST API, this works but always specifies the branch as master, even when I specify a different branch.
I have the code set up as per the below (with the sensitive details omitted):
$body = @{
stagesToSkip = @{}
resources =
@{
repositories =
@{
"self" = @{
"refName" = 'refs/heads/test123'}
}
}
templateParameters = @{}
variables = @{
'variable1' = @{
value = $textBox1.Text
}
'variable2' = @{
value = $textBox2.Text
}
'variable3' = @{
value = $textBox3.Text
}
'variable4' = @{
value = $textBox4.Text
}
'variable5' = @{
value = $textBox5.Text
}
'variable6'= @{
value = $textBox6.Text
}
'variable7'= @{
value = $textBox7.Text
}
'variable8'= @{
value = $textBox8.Text
}
'variable9'= @{
value = $textBox9.Text
}
'variable10'= @{
value = $textBox10.Text
}
'variable11'= @{
value = $textBox11.Text
}
'variable12'= @{
value = $textBox12.Text
}
'variable13'= @{
value = $textBox13.Text
}
}
} | ConvertTo-Json
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", "Basic [Omitted PAT]")
$response = Invoke-RestMethod 'https://dev.azure.com/[OrgName]/[ProjectName]/_apis/pipelines/22/runs?api-version=7.0' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json
You can see from this that within the request body, the 'refname' value is set to the branch test123, which is a test branch I created (though the same issue occurs regardless of which branch I try).
As per the output from running this script, it sets the branch to Master and ignores what I have specified:
"_links": {
"self": {
"href": "https://dev.azure.com/[OrgName]/68babb1f-0ed8-4b26-8f68-a3c492c31279/_apis/pipelines/22/runs/1371"
},
"web": {
"href": "https://dev.azure.com/[OrgName]/68babb1f-0ed8-4b26-8f68-a3c492c31279/_build/results?buildId=1371"
},
"pipeline.web": {
"href": "https://dev.azure.com/[OrgName]/68babb1f-0ed8-4b26-8f68-a3c492c31279/_build/definition?definitionId=22"
},
"pipeline": {
"href": "https://dev.azure.com/[OrgName]/68babb1f-0ed8-4b26-8f68-a3c492c31279/_apis/pipelines/22?revision=152"
}
},
"variables": {
"variable1": {
"value": ""
},
"variable2": {
"value": ""
},
"variable3": {
"value": "Yes"
},
"variable4": {
"value": ""
},
"variable5": {
"value": ""
},
"variable6": {
"value": ""
},
"variable7": {
"value": ""
},
"variable8": {
"value": ""
},
"variable9": {
"value": ""
},
"variable10": {
"value": ""
},
"variable11": {
"value": ""
},
"variable12": {
"value": ""
},
"variable13": {
"value": ""
}
},
"templateParameters": {
},
"pipeline": {
"url": "https://dev.azure.com/[OrgName]/68babb1f-0ed8-4b26-8f68-a3c492c31279/_apis/pipelines/22?revision=152",
"id": 22,
"revision": 152,
"name": "Container Build",
"folder": "\\"
},
"state": "inProgress",
"createdDate": "2024-01-08T11:25:06.1776032Z",
"url": "https://dev.azure.com/[OrgName]/68babb1f-0ed8-4b26-8f68-a3c492c31279/_apis/pipelines/22/runs/1371",
"resources": {
"repositories": {
"self": "@{repository=; refName=refs/heads/master}"
}
},
"id": 1371,
"name": "1371"
I need to be able to swap out the branch in this script, as it is a major aspect of the work I am involved in. How do I set this up correctly? As an FYI, I am using a classic Azure Devops pipeline as opposed to YAML-based.
I have tried various formats and syntaxes for the body of the request, which makes no difference.
I expect that when I specify a particular branch in the body of the request, it checks out that branch when the build pipeline runs, but instead it always uses Master.
Based on your description, you are using the Classic Azure DevOps Pipeline.
The Rest API you are currently using applies to YAML Pipeline change branches. Runs - Run Pipeline
To set the Classic Pipeline Run Source Branch, you need to change to use the Rest API: Builds - Queue
For example:
Rest API:
POST https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=7.1-preview.7
Request Body:
{
"definition":{"id":buildefinitionid},
"sourceBranch":"refs/heads/branchname",
"reason":1,
"demands":[],
"parameters":"{\"system.debug\":\"false\",\"variablename1\":\"value\",\"variablename2\":\"value\"}"
}
Powershell example:
$body= @'
{
"definition":{"id":22},
"sourceBranch":"refs/heads/main",
"reason":1,
"demands":[],
"parameters":"{\"system.debug\":\"false\",\"variablename1\":\"value\",\"variablename2\":\"value\"}"
}
'@
$token = "PAT"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", "Basic $token")
$response = Invoke-RestMethod 'https://dev.azure.com/xx/xx/_apis/build/builds?api-version=7.1-preview.7' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json
You can set the variables in the parameters field of the request body. And you can set Pipeline Run source branch via sourceBranch field.
Result: