Azure CLI - Azure Devops - 发布管道 - 变量更新

Azure CLI - Azure Devops - Release pipline - Variable updates

对于 dynamics 365 修补程序的构建和发布,我创建了一个构建管道变量来获取补丁名称。

目前,我正在检查传递管道中的变量以发布并使用它在不同的实例中进行部署,但不确定如何执行此操作。所有的博客和答案都与经典流水线相关,与Rest相关。

我考虑使用 Azure CLI 脚本将发布管道变量更新为构建管道步骤 - 作为一种解决方法。

az 管道变量列表 --org $orgname --project $projectname --pipeline-name $pipelinename

上面的脚本工作得很好,列出了 BUILD 管道中的变量,但它没有检索发布管道变量,并显示了以下错误。

错误:在项目“Mustaque-ADO”中没有与名称“WM.CRM.Rls.DeploySITHotfix”匹配的构建定义。

能否请您告知这是否仅适用于构建变量而不适用于发布变量?

有没有一种方法可以通过 ADO 管道将动态构建变量传递给发布并在任务步骤中使用它?

Azure Cli az pipelines variable list 仅适用于 build/yaml 管道但不适用于经典发布管道。

Currently, I am checking to pass the variable in the pipeline to release and use it to deploy in the different instances, but not sure how to do that.

您可以使用 rest api 获取发布定义(Definitions - Get) and then update variable(Definitions - Update)。

请授予项目集合生成服务 (xxx) 帐户编辑​​发布管道权限。 (Select 发布管道 --> ... --> 安全 --> 编辑发布定义设置为允许)

示例如下:

trigger: none

pool:
  vmImage: Windows-latest

steps:
- script: echo $(var1)         # This is variable in build pipeline.

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $url = "https://vsrm.dev.azure.com/{yourorg}/$(System.TeamProject)/_apis/Release/definitions/3?api-version=5.0"
      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 var3 to its new value $(var1)
      $pipeline.variables.var3.value = "$(var1)"
      ####****************** 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 'var3' is updated to" $updatedef.variables.var3.value
      write-host "=========================================================="
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)

流水线结果:

检查发布管道值:

直接在本地编辑PS:

Param(
   [string]$org = "yourorgname",
   [string]$projectName = "yourprojectname",
   [string]$releasedefinitionid = "3",
   [string]$user = "",
   [string]$token = "yourPAT", 
   [string]$buildvar = "testvar2"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))


$url = "https://vsrm.dev.azure.com/$org/$projectName/_apis/Release/definitions/$releasedefinitionid"+"?api-version=5.0"
Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{
  Authorization=("Basic {0}" -f $base64AuthInfo)
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"
# Update an existing variable named var3 to its new value $buildvar
$pipeline.variables.var3.value = $buildvar
####****************** update the modified object **************************
$json = @($pipeline) | ConvertTo-Json -Depth 99
$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
write-host "==========================================================" 
Write-host "The value of Varialbe 'var3' is updated to" $updatedef.variables.var3.value
write-host "=========================================================="