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"

Subscribe

Other Recent Blogs

Using Helm as a configuration manager for AKS

Helm is a package manager for Kubernetes that allows easier packaging, configuring and deployment of applications and services on your Kubernetes cluster, however it is not “only” a tool for application deployment. In this post I will discuss using it as a configuration and compliance management tool for AKS.

Read More »

Our Award Winning AI-Infused Solutions

Learn about the AI-infused solution we developed for Maurice Blackburn Lawyers, Australia’s leading social justice law firm. Leveraging Azure Cognitive Search and Services, the solution uses AI to review life insurance policies to assess rapidly whether a claim has merit and should be pursued. Our solution has helped the firm cut the time taken to assess a client’s entitlement to a superannuation disability insurance claim from months down to the span of the client’s initial phone. Our work with Maurice Blackburn Lawyers was recently featured across a range of Australian publications.

Read More »

Arinco PTY LTD
All Rights Reserved

Level 17, 303 Collins Street
Melbourne VIC 3000