1
votes

For the dynamics 365 hotfix build and release, I have created a build pipeline variable to get the patch name.

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. All the blogs and answers are related to classic pipelines and related to Rest.

I thought of using Azure CLI script to update the release pipeline variable as a build pipeline step - as a workaround.

az pipelines variable list --org $orgname --project $projectname --pipeline-name $pipelinename

The above script works perfectly fine and lists the variables in the BUILD pipeline but it is not retrieving the release pipeline variables and the below error is shown.

ERROR: There were no build definitions matching the name "WM.CRM.Rls.DeploySITHotfix" in the project "Mustaque-ADO".

can you please advise if this is intended to work only with Build variables and not the Release variables ?

Is there a way I can pass via the ADO pipelines the dynamics build variable to the release and use it in the task steps?

enter image description here

1
I think you would need to output the build variable as a build artifact. Build artifacts are used as input by release pipelines.Henk van Boeijen

1 Answers

1
votes

The Azure Cli az pipelines variable list only works for build/yaml pipeline but not for classsic release pipeline.

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.

You can use rest api to get release definition(Definitions - Get) and then update variable(Definitions - Update).

Please grant Project Collection Build Service (xxx) account the edit release pipeline permission. (Select the release pipeline --> ... --> Security --> Edit release definition set to Allow) enter image description here

Sample as below:

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)

Pipeline result: enter image description here

Check for release pipeline value: enter image description here

To directly edit on local 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 "=========================================================="

enter image description here

enter image description here