如何通过发布任务修改Azure DevOps发布定义变量? [英] How to modify Azure DevOps release definition variable from a release task?

查看:52
本文介绍了如何通过发布任务修改Azure DevOps发布定义变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从AzureDevOps释放任务获取密钥轮换以用于Azure存储帐户的最简单方法是什么?当前的计划是在发行后重新生成旧密钥以使其失效,并具有可在下一次部署中使用的新密钥.但是要使其正常工作,看来我至少需要存储要在发布变量中使用的键的名称.

What is the easiest way to get key rotation to work for azure storage accounts from a AzureDevOps relase task? The current plan is to re-generate the old key after release to invalidate it, and have a fresh key that can be used on next deployment. But to get that to work it seems like I at least need to store the name of the key to use in a release variable.

我看过他记录任务的情况(

I had a look at he logging tasks (https://github.com/Microsoft/azure-pipelines-tasks/blob/master/docs/authoring/commands.md), but that only changes the value in the current release and does not modify the release definition.

推荐答案

您可以使用REST API(

You can use the REST API (Definitions - Update) to update the value of the release definition variable from a release task.

  1. 转到Agent Phase并选择Allow Scripts to Access OAuth Token.请参阅
  1. Go to the Agent Phase and select Allow Scripts to Access OAuth Token. See Use the OAuth token to access the REST API
  2. Grant Project Collection Build Service (xxx) account the edit release pipeline permission. (Select the release pipeline --> ... --> Security --> Edit release definition set to Allow)
  3. Add a PowerShell task in your release pipeline
  4. Run inline script: (Update the value of variable v1030 in below sample)

$url = "$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)$env:SYSTEM_TEAMPROJECTID/_apis/Release/definitions/$($env:RELEASE_DEFINITIONID)?api-version=5.0-preview.3"
Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"

# Update an existing variable named v1030 to its new value 1035
$pipeline.variables.v1030.value = "1035"

####****************** 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 'v1030' is updated to" $updatedef.variables.v1030.value
write-host "=========================================================="

这篇关于如何通过发布任务修改Azure DevOps发布定义变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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