0

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)
           }
8
  • Where are you referencing the variable group containing the VariablesObject1 variable? Is that variable suppose to contain a json string like in the first code block? Commented Jan 9 at 18:10
  • I'm referencing the Variable Group earlier in the code, before the task I pasted. Yes, it is suppose to contain JSON object, just like in the first example. Commented Jan 9 at 18:19
  • And you're completely sure the variable $(VariablesObject1) is set when running the AzurePowerShell@5 task? If you add a dummy task to print that variable value is displayed correctly? Commented Jan 9 at 19:33
  • Yes, I'm sure. I can see the task gets exactly what I'm setting in the variable group when running Write-Host '(VariablesObject1)' Commented Jan 9 at 19:40
  • Is the behavior different in the second code snippet if you move the variable group reference out of the New-AzResourceGroupDeployment invocation? e.g. use $WebAppEnvironmentVariables = $(VariablesObject1) followed by New-AzResourceGroupDeployment -ResourceGroupName $ResourceGroupName ... -TemplateParameterObject @{ "EnvironmentVariables" = $WebAppEnvironmentVariables } Commented Jan 9 at 20:10

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

This didn't work and provided empty value when verifying if variables are being passed by running Write-Host $(VariablesObject1)
@WinBoss sorry I'm confused - on Jan 9 you mentioned "Yes, I'm sure. I can see the task gets exactly what I'm setting in the variable group when running Write-Host '(VariablesObject1)'" - now you're saying the opposite? My suggested solution will obviously fail if for some reason $(VariablesObject1) is not available in the task.
@WinBoss check the scope of the variable group - are you referencing it at the pipeline or job scope?
@WinBoss any update?
Decided to not spend any more time on figuring this out and just use predefined variables instead of an object via variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.