1

Is there any way to pass a variable group name to a release pipeline using REST API without editing the release definition.

I am able to do it using the following

   $defurl = "https://vsrm.dev.azure.com/org/proj/_apis/release/definitions/13?api-version=5.1" 
   $def = Invoke-RestMethod -Uri $defurl -Method Get -Headers $header
   $def.variableGroups="VariableGroupName"
   $json = @($def) | ConvertTo-Json -Depth 99 
   $udef = Invoke-RestMethod  -Uri $defurl  -Method Put -Body $json -ContentType "application/json" -Headers $header

But the problem is "Put" request updating the Original definition. Is there any way to pass the Variablegroup without editing the release definition. Is this a good practice to edit the Release defnition on the fly to pass the variable group.

mystack
  • 4,910
  • 10
  • 44
  • 75
  • Can you tell us what `$definition` looks like? – Theo Dec 04 '19 at 11:02
  • $definition is a Json Object – mystack Dec 04 '19 at 14:50
  • I see, but what I meant was: you are updating the definition with `$def.variableGroups="VariableGroupName"`. Shouldn't you convert `$def` to json and use that in your next `Invoke-RestMethod` call rather than `$definition` ? – Theo Dec 04 '19 at 14:53
  • yes i do , Sorry for the typo My problem is I want to pass the variable without updating\editing the definition – mystack Dec 04 '19 at 15:12
  • I have found that most(all?) of the actions in the web gui call the REST api, so you can use fiddler to capture the calls your browser makes when you perform the action manually in the web gui, and inspect what the json looks like there, which may give you some insight. – Brandon McClure Dec 05 '19 at 16:49
  • It sounds like you want to create a Release from a Release Definition and then make a change to that Release. A Release is a different type of resource to a Release Definition. It’s not possible to add a Variable Group to a Release through the GUI and if you look at the API docs it implies you can only Get and not Set the Variable Groups so I don’t think it’s possible to do what you want without editing the Release Definition - https://learn.microsoft.com/en-us/rest/api/azure/devops/release/releases/update%20release?view=azure-devops-rest-5.1 – Nick Graham Dec 05 '19 at 19:50

2 Answers2

1

Is there any way to pass the Variablegroup without editing the release definition

I am afraid there is no such way to pass the Variablegroup without editing the release definition.

To pass the Variablegroup name to the release definition, we have to use the Put request to update the definition. Since there is no option/REST API we could use to update the definition when we run the release pipeline.

If you do not want to modify the original definition, you could get the Variablegroup name in the original definition, then use above REST API to add/update the Variablegroup name. At the end of the release pipeline, we could invoke again above REST API to restore the Variablegroup name in the original definition.

Besides, if there are not many variables in the variable group you added, you could use the the Logging Command during the release pipeline to overwrite the variables, which would not change the original definition.

Write-Host "##vso[task.setvariable variable=testvar;]testvalue"

Update:

How to use this logging command outside the release pipeline to modify the Variable group???

The answer is NO. That because we could not update the the variable group when we create the release pipeline, it only shows the release variable:

enter image description here

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • 1
    How to use this logging command outside the release pipeline to modify the Variable group??? – mystack Dec 09 '19 at 11:04
  • @tjdoubts, If you want to use this logging command outside the release pipeline to modify the Variable group, I am afraid the answer is NO, you could check my updated answer for some more details. – Leo Liu Dec 10 '19 at 09:33
0

You are using the same $defurl with a get, then a post. The post is trying to perform an update on the definition api doc src so this code will always update the release definition.

I think the endpoint you are looking for is release -> Create which will start a new deployment. I have not modified Variable Groups using this endpoint, but have overrode specific variables and will try to add some code to my answer if a better answer does not pop up.

I found that using fiddler to inspect the REST calls that the web gui sends helped me figure out exactly how my json body needed to look like.

Brandon McClure
  • 1,329
  • 1
  • 11
  • 32