问题描述
我可以使用下面的json主体来更新构建管道中的变量
I am able to update the variable in the build pipeline using the below json body
$body = '
{
"definition": {
"id": 25
},
"parameters": "{\"var\":\"value\"}"
}
'
相同的json无法与Release管道一起使用.有什么办法可以通过发布管道以相同的方式传递变量
The same json is not working with Release pipeline . Is there any way to pass the variable through same way through release pipeline
推荐答案
我们可以使用REST API 定义-获取以在主体中获取有关此定义的所有信息,然后我们可以更新主体并使用(定义-更新)以更新值发布管道中的发布定义变量的说明:
We could use the REST API Definitions - Get to get all the info about this definition in the body, then we could update the body and use the (Definitions - Update) to update the value of the release definition variable from a release pipeline:
PUT https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions/{definitionId}?api-version=5.0
以下是我的测试内联Powershell脚本:
Following is my test inline powershell scripts:
$url = "https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions/{definitionId}?api-version=5.1"
Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Method Get -Headers @{
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"
# Update an existing variable named TestVar to its new value 2
$pipeline.variables.TestVar.value = "789"
####****************** update the modified object **************************
$json = @($pipeline) | ConvertTo-Json -Depth 99
$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
write-host "=========================================================="
Write-host "The value of Varialbe 'TestVar' is updated to" $updatedef.variables.TestVar.value
作为测试结果,变量TestVar
更新为789
:
As test result, the variable TestVar
updated to 789
:
更新:
答案是肯定的.您可以使用发布-使用请求正文创建:
The answer is yes. You could use the Releases - Create with request body:
{
"definitionId": Id,
"environments": [
{
"variables": {
"TestVar": {
"value": "xxxx"
},
"TestVar2": {
"value": "xxxx"
}
},
}
],
}
有关更多信息,请参见在此处发布.
For more information refer the post here.
希望这会有所帮助.
这篇关于使用REST API设置Azure devops Release管道变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!