azure-devops - 使用 REST API 设置 Azure devops 发布管道变量

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

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

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

}
'

相同的 json 不适用于发布管道。有没有办法通过发布管道以相同的方式传递变量

最佳答案

Set Azure devops Release pipeline variable using REST API

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

更新:

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

答案是肯定的。你可以使用 Releases - Create带有请求正文:

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

    }
   ],
}

有关更多信息,请参阅 post here .

希望这对您有所帮助。

关于azure-devops - 使用 REST API 设置 Azure devops 发布管道变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59249440/

相关文章:

MSBUILD : Logger error MSB4104: Failed to write to log file "XYZ.sln.log"

azure - 在 Azure 管道中;在哪里存储数组?

tfs - 从 TFS 2017 构建定义中的 'Copy Files' 步骤中排除整个文件夹/内容

asp.net-mvc - Azure Web App VSTS 预编译 View (不可更新)

azure - 如何将软件版本从 Azure DevOps 部署到内部服务器?

linux - 使用 VSTS Release 在 Linux VM 上解压文件

tfs - 防止 TFS 在构建/发布时删除节点模块

azure - 使用 Azure PowerShell 将 key 保管库 secret 分配给 Azure 函数

azure-devops - Azure DevOps API 用于访问构建的多 repo 源

Azure DevOps Server 无法使用作为订阅贡献者的服务主体创建 Azure 资源