Yet more Azure DevOps pipeline variable expansion strangeness
The Issue
Yesterday I posted about converting ARM output variables to Azure DevOps pipeline variables
Whilst using the pattern I discussed we hit an interesting problem. On my test pipeline I had the following YAML and it was working as expected.
- task: PowerShell@2
displayName: Obtain Azure Deployment outputs
inputs:
targetType: 'inline'
script: |
if (![string]::IsNullOrEmpty( $env:deploymentOutputs )) {
$DeploymentOutputs = convertfrom-json $env:deploymentOutputs
$DeploymentOutputs.PSObject.Properties | ForEach-Object {
$keyname = $_.Name
$value = $_.Value.value
Write-Host "The value of [$keyName] is [$value]"
Write-Host "##vso[task.setvariable variable=$keyname]$value"
}
}
However, on the first production project I tried this on, the script ran but did not create the expected variables. The issue was that the variable $env:deploymentOutputs
was empty, even though the ARM deployment had completed successfully and the outputs were available in the pipeline debug logs.
The Solution
Turns out the problem was the type of pipeline agent being used. The test pipeline was using a Windows agent, the production pipeline an Ubuntu agent.
The fix was to swap to use the $(deploymentOutputs)
syntax, as opposed to the $env:deploymentOutputs
syntax, as shown below
- task: PowerShell@2
displayName: Obtain Azure Deployment outputs
inputs:
targetType: 'inline'
script: |
if (![string]::IsNullOrEmpty( '$(deploymentOutputs)' )) {
$DeploymentOutputs = convertfrom-json '$(deploymentOutputs)'
$DeploymentOutputs.PSObject.Properties | ForEach-Object {
$keyname = $_.Name
$value = $_.Value.value
Write-Host "The value of [$keyName] is [$value]"
Write-Host "##vso[task.setvariable variable=$keyname]$value"
}
}
Interestingly, I had assumed the issue was actually whether PowerShell or PowerShell Core was being used, but this was not the case. Both versions of PowerShell correctly resolved the $env:deploymentOutputs
syntax on a Windows agent.
So it was the agent OS type that was the issue. Yet another strange quirk of Azure DevOps pipeline variable expansion.
For the original version of this post see Richard Fennell's personal blog at Yet more Azure DevOps pipeline variable expansion strangeness