0

I have an Azure Devops YAML based Build (Not Release) Pipeline. I have defined a variable called Department. My Requirement is that this variable to be updated at the end of the build using the rest API. I'm using this code.

How to modify Azure DevOps release definition variable from a release task?

The API call works fine. But I'm not sure whether this is the correct API to call. The Department will change for each build. According to the output HTTP method put is not supported.

Note: I have actually defined 5 variables including Department, Department being the last one. When the API is called it only outputs first 3 variables only.


$Department = getDepartment.ps1

$url = "https://dev.azure.com/xxx/xxx/_apis/pipelines/$(System.DefinitionId)/runs?api-version=6.0-preview.1"

$pipeline = Invoke-RestMethod -Uri $url -Headers @{
          Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
      }
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"


$pipeline.variables.Department.value = $Department
      

$json = @($pipeline) | ConvertTo-Json -Depth 99


$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}

Write-host "==========================================================" 
Write-host "The value of Variable 'Department' is updated to $( $updatedef.variables.Department.value)"
write-host "=========================================================="
bryanbcook
  • 16,210
  • 2
  • 40
  • 69
  • 1
    Hi Prageeth; I'm not sure I understand your requirement. What are you trying to achieve, by changing a pipeline variable after the pipeline has completed? Pipeline variables only exist for the duration of the pipeline run, so changing it at the end won't affect anything? – Vince Bowdren Mar 07 '22 at 15:06
  • Is it that you want the next run of this pipeline to use a different department? – Vince Bowdren Mar 07 '22 at 15:07
  • @VinceBowdren Yes i want the next Pipeline Run to use the updated Variable...Thanks – Prageeth Athulathmudali Mar 08 '22 at 03:24

2 Answers2

0

The Department will change for each build. According to the output HTTP method put is not supported.

According to the document Pipelines:

enter image description here

It does not provide a method for update pipeline with PUT.

To resolve this issue, you still need use the REST API Build Definitions - Update:

PUT https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=6.0

The code sample:

$url = "https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=6.0"

Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"

$pipeline.variables.Test.value = "$Department"

####****************** update the modified object **************************
$json = @($pipeline) | ConvertTo-Json -Depth 99


$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}

write-host "==========================================================" 
Write-host "The value of Varialbe 'Test' is updated to" $updatedef.variables.Test.value

The test result:

enter image description here

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
0

When you start the pipeline using the API, you can add the values of the parameters that should be different from the default values, to the payload of the call. So you're not updating the definition, but just the value of the variables for that instance of the pipeline run.

Payload

$JSON = @"
    {
        "definition": {
        "id":"207"
    },
    "parameters": "{ \"ENVIRONMENT\":\"$($config.testEnvironmentName)\", \"API_CLIENT_SECRET\":\"$($config.testEnvironmentApiClientSecret)\" }"
    }
"@

Note that the 'parameters' property has a value as an escaped json string.

This is the API that I use to start the pipeline. /_apis/build/builds?api-version=7.1-preview

Use a 'POST' method and send the json as the body of the request (content-type application/json).

Björn Boxstart
  • 1,098
  • 1
  • 12
  • 25