admin管理员组

文章数量:1124387

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)
           }
Share Improve this question edited yesterday bryanbcook 17.6k2 gold badges44 silver badges73 bronze badges asked 2 days ago WinBossWinBoss 9231 gold badge23 silver badges49 bronze badges 6
  • 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? – Rui Jarimba Commented 2 days ago
  • 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. – WinBoss Commented 2 days ago
  • 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? – Rui Jarimba Commented yesterday
  • Yes, I'm sure. I can see the task gets exactly what I'm setting in the variable group when running Write-Host '(VariablesObject1)' – WinBoss Commented yesterday
  • 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 } – Jonathan Dodds Commented yesterday
 |  Show 1 more comment

1 Answer 1

Reset to default 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.

本文标签: azure devopsPass PowerShell Object from Variable GroupStack Overflow