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

tjd*_*bts 2 azure-devops azure-pipelines azure-pipelines-release-pipeline azure-devops-rest-api

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

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

}
'
Run Code Online (Sandbox Code Playgroud)

相同的 json 不适用于 Release pipeline 。有什么方法可以通过发布管道以相同的方式传递变量

Leo*_*SFT 5

使用 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
Run Code Online (Sandbox Code Playgroud)

以下是我的测试内联 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
Run Code Online (Sandbox Code Playgroud)

作为测试结果,变量TestVar更新为789

在此处输入图片说明

更新:

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

答案是肯定的。您可以使用Releases - Create with request body:

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

    }
   ],
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅此处帖子

希望这可以帮助。

  • @tjdoubts,这完全是另一个问题,您在原始问题中根本没有提到它,请检查我更新的答案。 (2认同)