mystack
mystack

Reputation: 5532

Set Azure devops Release pipeline variable using REST API

I am able to update the variable in the build pipeline using the below json body

        $body = '
{ 
    "definition": {
        "id": 25
    },
    "parameters": "{\"var\":\"value\"}"

}
'

The same json is not working with Release pipeline . Is there any way to pass the variable through same way through release pipeline

Upvotes: 3

Views: 7471

Answers (2)

Michael K. Sondej
Michael K. Sondej

Reputation: 963

Old topic, but there is a better way now and I believe it deserves a new answer (maybe it was even available since the very beginning, don't know.)

Instead of updating the very definition of the pipeline which only works for future releases, you can now update the currently running release only and that solves your problem.

This is how I set up the tasks in the pipeline:

enter image description here

And here's a snippet from the Powershell task: (it updates delay_minutes release variable based on deploy_time variable which specifies time in HH:mm format)

if(!"$(deploy_time)") {
  Write-Host "deploy_time empty, won't delay deployment"
  return
}

$url = "$(System.TeamFoundationServerUri)/$(System.TeamProjectId)/_apis/release/releases/$(Release.ReleaseId)?api-version=5.0"

# Uncomment for debugging
# Write-Host "URL: $url"

$delayMinutes = [int](New-TimeSpan -start (Get-Date) -end "$(deploy_time)").TotalMinutes

if($delayMinutes -lt 0) { $delayMinutes = 0 }

$pipeline = Invoke-RestMethod -Uri $url -Method Get -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}


# Uncomment for debugging
# Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"

$pipeline.variables.delay_minutes.value = $delayMinutes

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

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

The URL in the snippet uses only always available predefined variables so it should be 100% copy-pastable. Also make sure to set this on the first agent job:

enter image description here

So that the SYSTEM_TOKEN variable is available in the script.

Upvotes: 1

Leo Liu
Leo Liu

Reputation: 76928

Set Azure devops Release pipeline variable using REST API

We could use the REST API Definitions - Get to get all the info about this definition in the body, then we could update the body and use the (Definitions - Update) to update the value of the release definition variable from a release pipeline:

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

Following is my test inline powershell scripts:

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

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

# Update an existing variable named TestVar to its new value 2
$pipeline.variables.TestVar.value = "789"

####****************** 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 'TestVar' is updated to" $updatedef.variables.TestVar.value

As test result, the variable TestVar updated to 789:

enter image description here

Update:

But I want to achieve it without updating\changing the definition

The answer is yes. You could use the Releases - Create with request body:

{
  "definitionId": Id,
  "environments": [
    {
      "variables": {
        "TestVar": {
          "value": "xxxx"
        },
        "TestVar2": {
          "value": "xxxx"
        }
      },

    }
   ],
}

For more information refer the post here.

Hope this helps.

Upvotes: 7

Related Questions