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)
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:
Check for release pipeline value:
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 "=========================================================="