使用REST API设置Azure devops Release管道变量 [英] Set Azure devops Release pipeline variable using REST API

本文介绍了使用REST API设置Azure devops Release管道变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用下面的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设置Azure devops Release管道变量

Set Azure devops Release pipeline variable using REST API

我们可以使用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:

更新:

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

But I want to achieve it without updating\changing the definition

答案是肯定的.您可以使用

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管道变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆