Integrating ARM Templates with Azure Pipelines

You can integrate Azure Resource Manager templates (ARM templates) with Azure Pipelines for continuous integration and continuous deployment (CI/CD). In this article, you will learn two more advanced ways to deploy templates with Azure Pipelines.

Select Your Option

Before proceeding, let’s consider the different options for deploying an ARM template from a pipeline:

  1. Use ARM Template Deployment Task: This is the easiest option when you want to deploy a template directly from a repository. This approach is covered in the tutorial Continuous Integration of ARM Templates with Azure Pipelines.

  2. Add Task that Runs an Azure PowerShell Script: This option provides consistency throughout the development life cycle by using the same script you used for local testing. Your script deploys the template and can also perform other operations like getting parameter values. This option is covered in the Azure PowerShell Task section.

  3. Add Copy and Deploy Tasks: This offers a convenient alternative to the project script. You configure two tasks in the pipeline - one to stage the artifacts to an accessible location, and another to deploy the template from that location. This option is covered in the Copy and Deploy Tasks section.

Prepare Your Project

Ensure your ARM template and Azure DevOps organization are ready for creating the pipeline:

Azure PowerShell Task

This section shows how to configure continuous deployment using a single task that runs a PowerShell script in your project. You can use scripts like Deploy-AzTemplate.ps1 or Deploy-AzureResourceGroup.ps1.

Here’s an example YAML file that creates an Azure PowerShell task:

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: AzurePowerShell@5
  inputs:
    azureSubscription: 'script-connection'
    ScriptType: 'FilePath'
    ScriptPath: './Deploy-AzTemplate.ps1'
    ScriptArguments: -Location 'centralus' -ResourceGroupName 'demogroup' -TemplateFile templatesmainTemplate.json
    azurePowerShellVersion: 'LatestVersion'

You’ll need to customize the following parts for your environment:

  • azureSubscription: Provide the name of the service connection you created.
  • ScriptPath: Provide the relative path from the pipeline file to your script.
  • ScriptArguments: Provide any parameters needed by your script.

Copy and Deploy Tasks

This approach uses two tasks - one to stage the artifacts to a storage account, and another to deploy the template from that location.

The following YAML shows the Azure File Copy task:

trigger:
- master

pool:
  vmImage: 'windows-latest'

steps:
- task: AzureFileCopy@4
  inputs:
    SourcePath: 'templates'
    azureSubscription: 'copy-connection'
    Destination: 'AzureBlob'
    storage: 'demostorage'
    ContainerName: 'projecttemplates'
  name: AzureFileCopy

You’ll need to customize the following parts for your environment:

  • SourcePath: Indicate the location of the artifacts relative to the pipeline file.
  • azureSubscription: Provide the name of the service connection you created.
  • storage and ContainerName: Provide the names of the storage account and container to use for storing the artifacts.

After creating the copy file task, you can add the task to deploy the staged template:

- task: AzureResourceManagerTemplateDeployment@3
  inputs:
    deploymentScope: 'Resource Group'
    azureResourceManagerConnection: 'copy-connection'
    subscriptionId: '00000000-0000-0000-0000-000000000000'
    action: 'Create Or Update Resource Group'
    resourceGroupName: 'demogroup'
    location: 'West US'
    templateLocation: 'URL of the file'
    csmFileLink: '$(AzureFileCopy.StorageContainerUri)templates/mainTemplate.json$(AzureFileCopy.StorageContainerSasToken)'
    csmParametersFileLink: '$(AzureFileCopy.StorageContainerUri)templates/mainTemplate.parameters.json$(AzureFileCopy.StorageContainerSasToken)'
    deploymentMode: 'Incremental'
    deploymentName: 'deploy1'

You’ll need to customize the following parts for your environment:

  • deploymentScope: Select the scope of deployment (Resource Group, Subscription, or Management Group).
  • azureResourceManagerConnection: Provide the name of the service connection you created.
  • resourceGroupName and location: Provide the name and location of the resource group to deploy to.
  • csmFileLink: Provide the link for the staged template using variables from the file copy task.

By following these approaches, you can effectively integrate ARM templates with your Azure Pipelines for CI/CD.

For more information, see: