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

3
我可以使用以下JSON格式的数据更新构建管道中的变量。
        $body = '
{ 
    "definition": {
        "id": 25
    },
    "parameters": "{\"var\":\"value\"}"

}
'

在发布管道中,相同的JSON文件无法工作。是否有任何方法可以通过相同方式通过发布管道传递变量?

2个回答

7

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

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

    }
   ],
}

更多信息请参考此处的帖子

希望这可以帮到你。


但是我想在不更新/更改定义的情况下实现它。 - mystack
2
@tjdoubts,这完全是另一个问题,你在原始问题中根本没有提到它,请查看我的更新答案。 - Leo Liu
@LeoLiu-MSFT,这太棒了。完美地运行!但是为什么封装REST API的程序集有这么多限制呢?例如变量操作(这也是我来这里的原因!)。不管怎样,谢谢你。 - gsscoder
@LeoLiu-MSFT 请更新您的问题并删除与问题无关的部分,或者至少改变顺序。这样会更有意义。 - Eugen Mayer

1

虽然这个话题有些老套,但现在有更好的方法,我认为它值得一个新答案(也许从一开始就可用,不确定)。

与其更新管道的定义,因为这只适用于未来版本,你现在可以仅更新当前正在运行的版本,以解决你的问题。

以下是我如何设置管道中的任务:

enter image description here

这里是 Powershell 任务的一段代码:

(它基于 deploy_time 变量更新 delay_minutes 发布变量,deploy_time 指定 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%可复制粘贴的。 另外,请确保在第一个代理作业中设置以下内容: enter image description here 以便在脚本中可用SYSTEM_TOKEN变量。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接