E. Staal
E. Staal

Reputation: 557

Azure DevOps rest api get release definition does not include variables

I wrote a little PowerShell script that get's a release definition. On that definition I defined a few variables. Both with scope release as well as with a scope to a specific environment. When I now get the definition these variables are not included. The end goal is to be able to change a variable value using the API.

* Update *

Merlin Liang was completely right. I just didn't print the results correctly.

Here's my code for anyone else with the same :


$VariableValue = "test"
$VariableName = "test"

## Construct a basic auth head using PAT
function BasicAuthHeader()
{
    $ba = (":{0}" -f $env:SYSTEM_ACCESSTOKEN)
    $ba = [System.Text.Encoding]::UTF8.GetBytes($ba)
    $ba = [System.Convert]::ToBase64String($ba)
    $h = @{Authorization=("Basic{0}" -f $ba);ContentType="application/json"}

    return $h
}

$h = BasicAuthHeader 
$baseRMUri = $env:SYSTEM_TEAMFOUNDATIONSERVERURI + $env:SYSTEM_TEAMPROJECT
$releaseId = $env:RELEASE_RELEASEID

$getReleaseUri = $baseRMUri + "/_apis/release/definitions/30?api-version=5.1"

$release = Invoke-RestMethod -Uri $getReleaseUri -Headers $h -Method Get
# write-host "results = $($release | ConvertTo-Json -Depth 100)"

$release2 = $release | ConvertTo-Json -Depth 100 | ConvertFrom-JSON

$release2.variables.($VariableName).value = $VariableValue

$release2 = [Text.Encoding]::UTF8.GetBytes(($release2 | ConvertTo-Json -Depth 100))

$updateReleaseUri = $baseRMUri + "/_apis/release/definitions/30?api-version=5.1"

$content2 = Invoke-RestMethod -Uri $updateReleaseUri -Method Put -Headers $h -ContentType "application/json" -Body $release2 -Verbose -Debug

Upvotes: 1

Views: 988

Answers (2)

Mengdi Liang
Mengdi Liang

Reputation: 19026

Please modify the last line of your script as:

write-host "results = $($release | ConvertTo-Json -Depth 100)"

Then you would see the detailed definition from its result.


When we give the corresponding response to API, in fact, it is a nested array. This means if you just use Write-Host $result to print out the response, then it can only give you the first level.

You must use ConvertTo-Json -Depth *** to convert the response firstly(ConvertTo-Json), and then specify how many levels of contained objects in JSON you want them show out(-Depth ***).

Here, in your issue, the corresponding has been replied, but they were hide because of nested. So, just need to spread out the nested arrays/objects by using above script.

Upvotes: 1

Florian Eckert
Florian Eckert

Reputation: 311

I am using https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions/{definitionId}?api-version=5.1 and variables and variableGroups are included.

Upvotes: 0

Related Questions