Create a CI/CD pipeline in VSTS to deploy ARM templates

This is the final post in the series of basics for working on Infrastructure as a Code or IaaC. For first two posts, please refer below two links:

  1. Code virtual network configuration in Azure using ARM templates, Visual Studio and Git
  2. Sync local source code and commit history with Visual Studio Team Services

We learned how to create ARM template inside Visual Studio, enable versioning, deploy ARM template using Visual Studio, make new configuration changes, sync source and commit history to VSTS. In this blog post, we’ll learn how to create a CI/CD pipeline to deploy ARM templates.

First, we need to login to VSTS subscription account with the proper credentials. It will take to the VSTS Dashboard by default which will show the projects created. If you hover over the name of the project, it will show links like code, test etc. From there we need to select code for the project we created earlier:

Select code from links shown for the project created
Select code from links shown for the project created

It will take us to the repositories for the project. Now we need to select ‘Set up Build’ from the right top side on the user interface:

Select setup build from the git repository
Select setup build from the git repository

From the new page shown, we’ll select the ’empty process’ link so that there are no tasks pre-configured for us:

Select empty process link from the list of options
Select empty process link from the list of options

Since we have selected ‘Setup build’ from repository, vSTS will configure some settings directly for the build definition. In the next page, it will ask which agent to use, where-in we’ll use ‘hosted agent’ from list of options shown:

Select hosted agent for the default agent queue
Select hosted agent for the default agent queue

In very simple terms, a hosted agent is a pre-configured server which is used to compile the source code to generate artifacts and is provided by Microsoft. Now, we need to first add a task so that we can generate the artifacts. So, we need to click add tasks from left pane and then search for ‘copy task’:

search and add copy task

We need to modify it as below so that it can copy json files from the code repo:

modify copy task as shown
modify copy task as shown

We now need to copy the artifacts generated on hosted agent to VSTS back. For this purpose, we need to add a task to build definition. Again, click add task from left pane, search for ‘Publish’ and then add the ‘Publish Build Artifacts’ task:

search and add publish artifacts task

Once its done, we’ll modify it to look like below:

modify the publish artifacts task

We now have a ready build definition for CI purposes. We can choose to add triggers where-in we can select one or more branches to be checked, modify the build definition name, use some variables etc. For the purpose of simplicity and this blog post, we’ll not go more in details.

Now, we need to deploy the resources on Azure. The general guideline is to create a release definition, link it to build and then create a CD pipeline. For purpose of simplicity, we’ll directly add a release task in the same build definition that we created above. For this, select add task from left pane and search for ‘Azure Resource Group Deployment’ task:

Search and add Azure Resource Group Deployment task
Search and add Azure Resource Group Deployment task

Once its added, we’ll need to provide Azure related details like name of subscription, resource group name and location. We’ll also need to provide the azure template location. You’ll need to provide respective details for your environment for same. Below is a sample from my details:

Provide details for the Azure Resource Group task
Provide details for the Azure Resource Group task

Now, we’ll select ‘Save and Queue’ from right pane and then Queue a new build:

Save and Queue a new build
Save and Queue a new build

Once its complete, we can see the resources deployed into the Azure Portal.

One of the philosophies of the IaaC is to modify configuration at the source rather than at destination. So as we have learned, for modifying configuration, we need to modify at the source, check-in new code and then build & deploy using the CI/CD definition.

6 thoughts on “Create a CI/CD pipeline in VSTS to deploy ARM templates

  1. Hi Mohit I am trying to deploy 4 ARM templates using Azure CLI script means instead of choosing Azure Resource Deployment I am choosing Azure Cli as the task and running the command 4 times. In this scenario how i can create the release ci/cd pipeline?

    Like

Leave a comment