azure-devops - 如何从发布任务修改 Azure DevOps 发布定义变量?

标签 azure-devops azure-storage azure-pipelines-release-pipeline

从 AzureDevOps 发布任务让 key 轮换适用于 Azure 存储帐户的最简单方法是什么?当前的计划是在发布后重新生成旧 key 以使其失效,并拥有一个可以在下次部署时使用的新 key 。但是为了让它工作,似乎我至少需要存储 key 的名称以在发布变量中使用。

我查看了他的日志记录任务( https://github.com/Microsoft/azure-pipelines-tasks/blob/master/docs/authoring/commands.md ),但这只会更改当前版本中的值,而不会修改版本定义。

最佳答案

您可以使用 REST API ( Definitions - Update ) 从发布任务更新发布定义变量的值。

  • 转至 Agent Phase并选择 Allow Scripts to Access OAuth Token .见 Use the OAuth token to access the REST API
  • 授予 Project Collection Build Service (xxx)帐户编辑
    释放管道权限。 (选择发布管道 --> ... --> Security --> Edit release definition 设置为 Allow )
  • 在发布管道中添加 PowerShell 任务
  • 运行内联脚本:(在下面的示例中更新变量 v1030 的值)
    $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 "=========================================================="
    

  • enter image description here

    关于azure-devops - 如何从发布任务修改 Azure DevOps 发布定义变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53042605/

    相关文章:

    npm - 已将 NPM 包发布到 Azure Artifacts,但在安装时出现此错误

    azure-devops - 探索托管代理上的文件系统

    azure-devops - 如何获取 Visual Studio Team Services 中工件的下载 url?

    .net - 如何停止 dotnet pack 在 azure devops 中构建项目(--no-build 没有效果)

    Azure QueueClient 与 CloudQueue

    azure-pipelines - 具有多个存储库的 VSTS 构建

    TFS 查询没有父任务的任务

    Azure 使用 Terraform 将安全组与多个网络接口(interface)和负载均衡器与多个子网关联

    c# - 从 Blob Azure 下载重定向错误

    azure - 如何使用 PowerShell 或 Azure CLI 检索 Azure 存储表中的所有记录