Pass a Variable group name using REST API Azure devops

ぃ、小莉子 提交于 2021-02-10 18:26:42

问题


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.


回答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:

Hope this helps.




回答2:


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.



来源:https://stackoverflow.com/questions/59173157/pass-a-variable-group-name-using-rest-api-azure-devops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!