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

Set Azure devops Release pipeline variable using REST API

我可以使用下面的 json body

更新构建管道中的变量
        $body = '
{ 
    "definition": {
        "id": 25
    },
    "parameters": "{\"var\":\"value\"}"

}
'

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

Set Azure devops Release pipeline variable using REST API

我们可以使用 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) 从发布管道更新发布定义变量的值:

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

更新:

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

希望对您有所帮助。

老话题,但现在有更好的方法,我相信它值得一个新的答案(也许它从一开始就可用,不知道。)

您现在可以只更新当前 运行 版本,而不是更新仅适用于未来版本的管道定义,这可以解决您的问题。

这是我在管道中设置任务的方式:

下面是 Powershell 任务的一个片段: (它根据 deploy_time 变量更新 delay_minutes 释放变量,该变量以 HH:mm 格式指定时间)

if(!"$(deploy_time)") {
  Write-Host "deploy_time empty, won't delay deployment"
  return
}

$url = "$(System.TeamFoundationServerUri)/$(System.TeamProjectId)/_apis/release/releases/$(Release.ReleaseId)?api-version=5.0"

# Uncomment for debugging
# Write-Host "URL: $url"

$delayMinutes = [int](New-TimeSpan -start (Get-Date) -end "$(deploy_time)").TotalMinutes

if($delayMinutes -lt 0) { $delayMinutes = 0 }

$pipeline = Invoke-RestMethod -Uri $url -Method Get -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}


# Uncomment for debugging
# Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"

$pipeline.variables.delay_minutes.value = $delayMinutes

$json = @($pipeline) | ConvertTo-Json -Depth 99

$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}

代码段中的 URL 仅使用始终可用的预定义变量,因此它应该是 100% copy-pastable。 还要确保在第一个代理作业上进行设置:

以便 SYSTEM_TOKEN 变量在脚本中可用。