divyanshm
divyanshm

Reputation: 6810

Error while updating VSTS release definition from powershell

I use the APIs listed in the VSTS API documentation here. On modifying a variable and saving the definition the error I get from the server is VS402982: Retention policy is not set for the environment 'environmentName'.

The portion of the PS script that performs the update is -

$c = Invoke-WebRequest 'https://accountname.vsrm.visualstudio.com/projectname/_apis/release/definitions/definitionId' -Method Get -Headers @{Authorization = 'Bearer ' + $authtoken} 
$jsonObj = $c | ConvertFrom-Json
$url3 = "https://accountname.vsrm.visualstudio.com/projectname/_apis/release/definitions/definitionId?api-version=4.1-preview.3";

$contentType3 = "application/json"      
$headers3 = @{
    Authorization = 'Bearer ' + $authtoken
};

$d = $jsonObj | ConvertTo-Json;
Invoke-RestMethod -Method PUT -Uri $url3 -ContentType $contentType3 -Headers $headers3 -Body $d;

What could be wrong here?

Upvotes: 0

Views: 581

Answers (2)

Sven
Sven

Reputation: 11878

Additionally to divyanshm's solution make sure encoding is correct:

$d = [Text.Encoding]::UTF8.GetBytes($d)

Upvotes: 0

divyanshm
divyanshm

Reputation: 6810

This problem has been reported a couple of times in different forms, and is mostly related to a small problem with the powershell code in the question.

If you see error like the one mentioned in the question or this - VS402903: The specified value is not convertible to type ReleaseDefinition. Make sure it is convertible to type ReleaseDefinition and try again it means that there is a problem in the JSON object that you are posting to the server. Easiest problem will be to capture the request payload and analyse it for issues.

However, in the code mentioned in the question, the problem lies with powershell's ConvertTo-JSON method. Do note, the release definition has multiple layers of nested objects, definition -> environment -> steps/approvals etc, and ConvertTo-JSON by default goes only 2 levels deep to form a JSON object, which means you are missing some vital properties while calling the VSTS APIs. The fix would be to specify a large value for the -Depth parameter so that you do not miss any properties while calling the service.

eg. ConvertTo-Json -Depth 100

More details on the problem and how it's fixed in the script can be seen here.

Upvotes: 1

Related Questions