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
  • 15,127
  • 2
  • 37
  • 66
  • 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

1 Answers1

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-MSFT
  • 62,538
  • 8
  • 91
  • 107