Blogs

Share on facebook
Share on twitter
Share on linkedin
Share on email

Azure Pro Tip Series: How to use New-AzResourceGroupDeployment -WhatIf in Azure DevOps pipeline templates with override parameters

Today we will be walking through how to setup an Azure DevOps pipeline template to execute the New-AzResourceGroupDeployment cmdlet with the -WhatIf argument when you have an ARM template that requires override parameters.

Before we continue this article assumes knowledge in the following areas.

A common use case for running the command with the -WhatIf argument is to validate if our ARM template deployment will be successful and to display the changes it is going to make as part of the deployment. This is perfect for utilising inside an Azure DevOps pipeline template as it allows us to validate our ARM templates before we deploy them.

So how do we setup our Azure DevOps pipeline template to run the New-AzResourceGroupDeployment cmdlet when we have ARM template that has override parameters?

  1. Update the format of our override parameters in our pipeline.

Typically when we parse override parameters to a pipeline template it looks something like this.

- template: /pipelines/templates/MyDemoTemplate.yml
  parameters:
    serviceConnection: ServiceConnectionName
    location: AustraliaSoutheast
    templateFile: armtemplates/demo/azuredeploy.json
    templateParametersFile: armtemplates/demo/azuredeploy.parameters.json
    overrideParameters: –storageName fabrikam -storageTier hot

We need to update the format of the override parameters so it now looks like this.

- template: /pipelines/templates/MyDemoTemplate.yml
  parameters:
    serviceConnection: ServiceConnectionName
    location: AustraliaSoutheast
    templateFile: armtemplates/demo/azuredeploy.json
    templateParametersFile: armtemplates/demo/azuredeploy.parameters.json    
    overrideParameters: "storageName = fabrikam; storageTier = hot" 

2. Configure our pipeline template to generate a new ARM template parameter file with the override values.

To do that we create an Azure PowerShell task in our pipeline template that does the following.

  • Import the content from the ARM template parameter file
  • Create a temporary ARM parameter file with the content from ARM template parameter file and with the overrides
  • Execute the New-AzResourceGroupDeployment cmdlet with the -WhatIf argument specified using the temporary ARM template parameter file
 

Example below:

- task: AzurePowerShell@5
  displayName: Simulate Template Deployment
  inputs:
    azureSubscription: ${{parameters.serviceConnection}}
    ScriptType: InlineScript
    Inline: |
      $template = "${{parameters.templateFile}}"
      $templateParametersFile = "${{parameters.templateParametersFile}}"
      $overrideParameters = "${{parameters.overrideParameters}}"
      foreach ($override in $overrideParameters.Split(';')){
        $overrideObject = $override.Split('=').Trim()
        $overrideParam = $overrideObject[0]
        $overrideValue = $overrideObject[1]
        
        # Updates param value with override value
        if($json.parameters.$overrideParam){              
          $json.parameters.$overrideParam.value = $overrideValue
        }
        else{
          # Adds override param as it does not exist in param file
          [array]$addtionalParams += ('"' + $override.Split(';').trim().Replace("=",'":{"value":"') + '"},').Replace(" ","")
        }            
      }
      
      # Creates param object with updated override values and missing override params if they were found
      if($addtionalParams -ne $null){
        $paramFile = $json | ConvertTo-Json -Depth 15
        $newParamFile = $paramFile.Replace('"parameters": {', '"parameters": {' + $addtionalParams)             
      }
      
      # Creates param object with updated override values
      else{
        $newParamFile = $json | ConvertTo-Json -Depth 15
      }
      
      # Creates new param file with overrides
      New-Item $env:BUILD_ARTIFACTSTAGINGDIRECTORY/tempParamFile.json
      Set-Content -Value $newParamFile -Path $env:BUILD_ARTIFACTSTAGINGDIRECTORY/tempParamFile.json          
      New-AzResourceGroupDeployment -ResourceGroupName ${{parameters.ResourceGroup}} -TemplateFile $template -TemplateParameterFile $env:BUILD_ARTIFACTSTAGINGDIRECTORY/tempParamFile.json -Location ${{parameters.Location}} -WhatIf      
    FailOnStandardError: true
    azurePowerShellVersion: LatestVersion
    pwsh: true

The complete code is below.

Pipeline template

parameters:
  serviceConnection: ""
  location: ""
  resourceGroup: ""
  templateFile: ""
  templateParametersFile: ""
  overrideParameters: ""

steps:
  - task: AzurePowerShell@5  
    displayName: Simulate Template Deployment
    inputs:
      azureSubscription: ${{parameters.serviceConnection}}
      ScriptType: InlineScript
      Inline: |
        $template = "${{parameters.templateFile}}"
        $templateParametersFile = "${{parameters.templateParametersFile}}"
        $overrideParameters = "${{parameters.overrideParameters}}"
        foreach ($override in $overrideParameters.Split(';')){
          $overrideObject = $override.Split('=').Trim()
          $overrideParam = $overrideObject[0]
          $overrideValue = $overrideObject[1]
          
          # Updates param value with override value
          if($json.parameters.$overrideParam){              
            $json.parameters.$overrideParam.value = $overrideValue
          }
          else{
            # Adds override param as it does not exist in param file
            [array]$addtionalParams += ('"' + $override.Split(';').trim().Replace("=",'":{"value":"') + '"},').Replace(" ","")
          }            
        }
        
        # Creates param object with updated override values and missing override params if they were found
        if($addtionalParams -ne $null){
          $paramFile = $json | ConvertTo-Json -Depth 15
          $newParamFile = $paramFile.Replace('"parameters": {', '"parameters": {' + $addtionalParams)             
        }
        
        # Creates param object with updated override values
        else{
          $newParamFile = $json | ConvertTo-Json -Depth 15
        }
        
        # Creates new param file with overrides
        New-Item $env:BUILD_ARTIFACTSTAGINGDIRECTORY/tempParamFile.json
        Set-Content -Value $newParamFile -Path $env:BUILD_ARTIFACTSTAGINGDIRECTORY/tempParamFile.json          
        New-AzResourceGroupDeployment -ResourceGroupName ${{parameters.ResourceGroup}} -TemplateFile $template -TemplateParameterFile $env:BUILD_ARTIFACTSTAGINGDIRECTORY/tempParamFile.json -Location ${{parameters.Location}} -WhatIf      
      FailOnStandardError: true
      azurePowerShellVersion: LatestVersion
      pwsh: true

Pipeline

stages:
  - stage: Test
    jobs:
    - job: Test
      displayName: Test
      pool:
        vmImage: windows-latest
      steps:
        - checkout: self

        - template: /pipelines/templates/MyDemoTemplate.yml
          parameters:
            serviceConnection: ServiceConnectionName
            location: AustraliaSoutheast
            templateFile: armtemplates/demo/azuredeploy.json
            templateParametersFile: armtemplates/demo/azuredeploy.parameters.json
            overrideParameters: "storageName = fabrikam; storageTier = hot"
[mailpoet_form id="1"]

Other Recent Blogs

Arinco trades as Arinco (VIC) Pty Ltd
and Arinco (NSW) Pty Ltd

All Rights Reserved

 

Level 9, 360 Collins Street, 
Melbourne VIC 3000

Level 3, 19 Bridge Street
Sydney NSW 2000

Get started on the right path to cloud success today. Our Crew are standing by to answer your questions and get you up and running.