Creating a new ARM template pipeline in 2020

by MikeHogg20. May 2020 16:57
##BackgroundTwo different approaches to have pipeline automated ARM deployments
- AzureResourceManagerTemplateDeployment and 
- Powershell task where the ps1 script runs what you were typing in the Azure Powershell manually
(from - https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/add-template-to-azure-pipelines)
(note: Azure cli is separate from Azure Powershell and offers a non template alternative and AzDO has a task for it
    https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest)
(also there is Azure Resource Manager REST api. so many options.)
AzureResourceManagerTemplateDeployment copies your templates to a StagingDirectory and then runs the 
AzureResourceManagerTemplateDeployment tasks that run those templates.  It needs powershell to use Outputs anyway.  
The Powershell approach seems simpler.
Powershell way uses Deploy-AzureResourceGroup.ps1, this is the same one that Visual Studio uses when you 
right click->Deploy your Azure Resource Manager Project. 
(Azure Resource Manager Project is a new type of Visual Studio project, 
get latest templates to see it and get some helpful tooling)
for pipeline, add this task (note the @3 version)
```
pool:
  name: Hosted Windows 2019 with VS2019
  demands: azureps
steps:
- task: AzurePowerShell@3
  inputs:
    azureSubscription: '<service connection name>'
    ScriptPath: './Deploy-AzureResourceGroup.ps1'
    ScriptArguments: -ResourceGroupName 'mike-test' -ResourceGroupLocation 'eastus2'
    azurePowerShellVersion: LatestVersion
```
In the Powershell approach, there is now a new way.  the new way uses Powershell@4 Tasks
The new script is Deploy-AzTemplate.ps1, it must run in powershell 
(not Visual Studio, and it requires powershell 5.1 on windows but 
 there are some conflicts using it on 5.1 and some templates use features
 from later versions.  latest today is 7.0.1. Best to upgrade)
> .\Deploy-AzTemplate.ps1 -ArtifactStagingDirectory . -Location centralus -TemplateFile WebSiteSQLDatabase.json -TemplateParametersFile WebSiteSQLDatabase.parameters.json##TLDRor for pipeline use AzurePowerShell@4
```
pool:
  name: Hosted Windows 2019 with VS2019
  demands: azureps
steps:
- task: AzurePowerShell@4
  inputs:
    azureSubscription: '<service connection name>'
    ScriptPath: './Deploy-AzTemplate.ps1'
    ScriptArguments: -ResourceGroupName 'mike-test' -ResourceGroupLocation 'eastus2'
    azurePowerShellVersion: LatestVersion
```
notes: 
- resource group name- task creates if not exists already
- I decided not to use a single template with multiple parameter files for example for the two appServices. 
    This  added complexity for no good reason, so everything is just repeated if necessary and straightforward.
- If the new azModule supported linked templates it also added complexity, when we could just add a step 
    in our pipeline to call each additional template.  no SAS token, so artifact staging directory needed. 
    (notice we set ArtifactStagingDirectory to the SourceCodeDirectory in yaml which is kind of a quick hack)
- search for New-AzResourceGroupDeployment - that is the new azModule module that powershell ultimately calls.
phase 2:
- key vault.  Use parameter file
```
"<second-parameter-name>": {
      "reference": {
        "keyVault": {
          "id": "<resource-id-key-vault>"
        },
        "secretName": "<secret-name>"
      }
    }
```
this would be for prod.parameters.json of course, the lower dev.parameters.json and qa.parameters.json just have values## Further Reference:best summary to date i've found is Mark Heath https://markheath.net/post/deploying-arm-templates-azure-cli
tied with https://clearmeasure.com/az-cli-vs-azure-powershell-vs-arm-templates/
he also links to the quickstart template repo where you get the azTemplate.ps1 used above in powershell@4
https://github.com/Azure/azure-quickstart-templates
Best Practices: https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-best-practices
100s pages of docs for the new azModule: https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-4.1.0

About Mike Hogg

Mike Hogg is a c# developer in Brooklyn.

More Here

Favorite Books

This book had the most influence on my coding style. It drastically changed the way I write code and turned me on to test driven development even if I don't always use it. It made me write clearer, functional-style code using more principles such as DRY, encapsulation, single responsibility, and more.amazon.com

This book opened my eyes to a methodical and systematic approach to upgrading legacy codebases step by step. Incrementally transforming code blocks into testable code before making improvements. amazon.com

More Here