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 build the repository and generate the artifacts. So, we need to click add tasks from left pane and then search for ‘Visual Studio’. From list of search results, click on ‘Visual Studio task’ and then select add:

Select add tasks and add visual studio build task
Select add tasks and add visual studio build task

We’ll need to configure this task for us. For now, we’ll modify the value of MSBuild Arguments to below:

/p:DeployOnBuild=True /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:DeleteExistingFiles=True /p:publishUrl="$(build.artifactstagingdirectory)\\"

This will basically tell hosted agent that we need to generate separate files for the project for publishing purposes and copy all files to $(build.artifactstagingdirectory) after solution is built. So it will look like below:

Modify MSBuild Arguments for Visual Studio task
Modify MSBuild Arguments for Visual Studio task

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 Build Artifacts task
Search and add Publish Build Artifacts task

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

Modify the publish artifacts task
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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s