I have an Azure PowerShell task in an Azure DevOps Pipeline that creates Web Apps by deploying an ARM Template.
I'm trying to deploy a Web App with the specific environment variables and while they work fine when I define the variables in the YAML, they are not being passed when I define the Environment Variables via Variable Groups.
This is the task that works fine:
- checkout: self
- task: AzurePowerShell@5
displayName: "Create Web Apps"
inputs:
azureSubscription: 'XXX'
ScriptType: 'InlineScript'
azurePowerShellVersion: 'LatestVersion'
errorActionPreference: 'silentlyContinue'
Inline: |
# Define variables
$ARMTemplateFile = "$(Build.Repository.LocalPath)\ARM-Templates\WebApps.json"
$ResourceGroupName = "XXX"
$WebAppEnvironmentVariables = @{
"Name1" = "Value1"
"Name2" = "Value2"
}
# Use Azure PowerShell to deploy the ARM template
New-AzResourceGroupDeployment `
-ResourceGroupName $ResourceGroupName `
-TemplateFile $ARMTemplateFile `
-TemplateParameterObject @{
"EnvironmentVariables" = $WebAppEnvironmentVariables
}
I've tried every possible combination that I could think of and none of them work when referencing object from the Variable Groups. The template gets deployed, but the Environment Variables are empty:
- checkout: self
- task: AzurePowerShell@5
displayName: "Create Web Apps"
inputs:
azureSubscription: 'XXX'
ScriptType: 'InlineScript'
azurePowerShellVersion: 'LatestVersion'
pwsh: true
Inline: |
# Define variables
$ARMTemplateFile = "$(Build.Repository.LocalPath)\ARM-Templates\WebApps.json"
$ResourceGroupName = "XXX"
# Use Azure PowerShell to deploy the ARM template
New-AzResourceGroupDeployment `
-ResourceGroupName $ResourceGroupName `
-TemplateFile $ARMTemplateFile `
-TemplateParameterObject @{
"EnvironmentVariables" = $(VariablesObject1)
}
I have an Azure PowerShell task in an Azure DevOps Pipeline that creates Web Apps by deploying an ARM Template.
I'm trying to deploy a Web App with the specific environment variables and while they work fine when I define the variables in the YAML, they are not being passed when I define the Environment Variables via Variable Groups.
This is the task that works fine:
- checkout: self
- task: AzurePowerShell@5
displayName: "Create Web Apps"
inputs:
azureSubscription: 'XXX'
ScriptType: 'InlineScript'
azurePowerShellVersion: 'LatestVersion'
errorActionPreference: 'silentlyContinue'
Inline: |
# Define variables
$ARMTemplateFile = "$(Build.Repository.LocalPath)\ARM-Templates\WebApps.json"
$ResourceGroupName = "XXX"
$WebAppEnvironmentVariables = @{
"Name1" = "Value1"
"Name2" = "Value2"
}
# Use Azure PowerShell to deploy the ARM template
New-AzResourceGroupDeployment `
-ResourceGroupName $ResourceGroupName `
-TemplateFile $ARMTemplateFile `
-TemplateParameterObject @{
"EnvironmentVariables" = $WebAppEnvironmentVariables
}
I've tried every possible combination that I could think of and none of them work when referencing object from the Variable Groups. The template gets deployed, but the Environment Variables are empty:
- checkout: self
- task: AzurePowerShell@5
displayName: "Create Web Apps"
inputs:
azureSubscription: 'XXX'
ScriptType: 'InlineScript'
azurePowerShellVersion: 'LatestVersion'
pwsh: true
Inline: |
# Define variables
$ARMTemplateFile = "$(Build.Repository.LocalPath)\ARM-Templates\WebApps.json"
$ResourceGroupName = "XXX"
# Use Azure PowerShell to deploy the ARM template
New-AzResourceGroupDeployment `
-ResourceGroupName $ResourceGroupName `
-TemplateFile $ARMTemplateFile `
-TemplateParameterObject @{
"EnvironmentVariables" = $(VariablesObject1)
}
According to New-AzResourceGroupDeployment's documentation, -TemplateParameterObject
must be an Hashtable
:
New-AzResourceGroupDeployment
# ...
-TemplateParameterObject <Hashtable>
So instead of:
- task: AzurePowerShell@5
displayName: "Create Web Apps"
inputs:
azureSubscription: 'XXX'
ScriptType: 'InlineScript'
azurePowerShellVersion: 'LatestVersion'
pwsh: true
Inline: |
# Define variables
$ARMTemplateFile = "$(Build.Repository.LocalPath)\ARM-Templates\WebApps.json"
$ResourceGroupName = "XXX"
# Use Azure PowerShell to deploy the ARM template
New-AzResourceGroupDeployment -ResourceGroupName $ResourceGroupName `
-TemplateFile $ARMTemplateFile `
-TemplateParameterObject @{
"EnvironmentVariables" = $(VariablesObject1)
}
Try converting the JSON string to a hashtable:
- task: AzurePowerShell@5
displayName: "Create Web Apps"
inputs:
azureSubscription: 'XXX'
ScriptType: 'InlineScript'
azurePowerShellVersion: 'LatestVersion'
errorActionPreference: 'silentlyContinue'
Inline: |
# Define variables
$ARMTemplateFile = "$(Build.Repository.LocalPath)\ARM-Templates\WebApps.json"
$ResourceGroupName = "XXX"
$WebAppEnvironmentVariables = $env:MyVariables | ConvertFrom-Json -AsHashtable
# Use Azure PowerShell to deploy the ARM template
New-AzResourceGroupDeployment -ResourceGroupName $ResourceGroupName `
-TemplateFile $ARMTemplateFile `
-TemplateParameterObject @{
"EnvironmentVariables" = $WebAppEnvironmentVariables
}
env:
MyVariables: $(VariablesObject1) # <-------------------- from variable group
Note: I'm setting a task-level environment variable to avoid escaping issues.
VariablesObject1
variable? Is that variable suppose to contain a json string like in the first code block? – Rui Jarimba Commented Jan 9 at 18:10$(VariablesObject1)
is set when running theAzurePowerShell@5
task? If you add a dummy task to print that variable value is displayed correctly? – Rui Jarimba Commented Jan 9 at 19:33Write-Host '(VariablesObject1)'
– WinBoss Commented Jan 9 at 19:40New-AzResourceGroupDeployment
invocation? e.g. use$WebAppEnvironmentVariables = $(VariablesObject1)
followed byNew-AzResourceGroupDeployment -ResourceGroupName $ResourceGroupName ... -TemplateParameterObject @{ "EnvironmentVariables" = $WebAppEnvironmentVariables }
– Jonathan Dodds Commented Jan 9 at 20:10