简体   繁体   中英

Pass a Variable group name using REST API Azure devops

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.

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:

在此处输入图片说明

Hope this helps.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM