1

有没有办法使用 REST API 将变量组名称传递给发布管道,而无需编辑发布定义。

我可以使用以下方法来做到这一点

   $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

但问题是“ Put ”请求更新原始定义。有没有办法在不编辑发布定义的情况下传递变量组。这是动态编辑发布定义以传递变量组的好习惯吗?

4

2 回答 2

1

有没有办法在不编辑发布定义的情况下传递变量组

恐怕没有这种方法可以在Variablegroup 编辑发布定义的情况下通过。

要将Variablegroup名称传递给发布定义,我们必须使用Put请求来更新定义。由于没有选项/REST API,我们可以在运行发布管道时使用它来更新定义。

如果您不想修改原始定义,您可以获取Variablegroup原始定义中的名称,然后使用上面的 REST API 来添加/更新Variablegroup名称。在发布管道结束时,我们可以再次调用以上 REST API 来恢复Variablegroup原始定义中的名称。

此外,如果您添加的变量组中的变量不多,您可以在发布管道中使用Logging 命令覆盖变量,这不会改变原始定义。

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

更新:

如何在发布管道之外使用此日志记录命令来修改变量组???

答案是不。那是因为我们在创建发布管道时无法更新变量组,它只显示发布变量:

在此处输入图像描述

希望这可以帮助。

于 2019-12-06T03:15:35.580 回答
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.

于 2019-12-05T17:05:48.263 回答