使用REST API设置Azure devops Release管道变量

时间:2019-12-09 12:59:10

标签: azure-devops azure-pipelines azure-pipelines-release-pipeline azure-devops-rest-api

我可以使用下面的json主体更新构建管道中的变量

        $body = '
{ 
    "definition": {
        "id": 25
    },
    "parameters": "{\"var\":\"value\"}"

}
'

同一json无法与Release管道一起使用。有什么办法可以通过发布管道以相同的方式传递变量

1 个答案:

答案 0 :(得分:1)

  

使用REST API设置Azure devops Release管道变量

我们可以使用REST API Definitions - Get在主体中获取有关此定义的所有信息,然后我们可以更新主体并使用(Definitions - Update)更新发布定义的值发布管道中的变量:

PUT https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions/{definitionId}?api-version=5.0

以下是我测试的内联Powershell脚本:

$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

enter image description here

更新

  

但是我想在不更新\更改定义的情况下实现它

答案是肯定的。您可以将Releases - Create与请求正文一起使用:

{
  "definitionId": Id,
  "environments": [
    {
      "variables": {
        "TestVar": {
          "value": "xxxx"
        },
        "TestVar2": {
          "value": "xxxx"
        }
      },

    }
   ],
}

有关更多信息,请参见post here

希望这会有所帮助。