Implement a CI/CD process to build and deploy your ASP.NET app to Azure

Last Update: 6/30/2017

Visual Studio Team Services provides a highly customizable continuous integration (CI) and deployment (CD) automation system for your ASP.NET apps. This quickstart shows how to set up CI and CD to deploy an ASP.NET app to a Windows virtual machine in Azure. You create the virtual machine using Azure Powershell, and then you set up CI/CD in Team Services. In the CI process, you build the app using MSBuild and run tests using VSTest.

Screenshot showing ASP.NET web app

Get ready to go

Before you begin, make sure that you are:

  • A co-administrator of the Azure subscription. If you don't have an Azure subscription, you can create one for free.

  • An administrator of the Team Services account. If you don't yet have a Team Services account, you can create one for free.

  • Using the New Release Definition Editor that is currently in preview.

    preview features action in profile menu

    new release definition editor preview on

Create Azure Windows VM using Powershell

To provide a platform to run your ASP.NET web app, you need a Windows virtual machine that runs IIS.

Create a Windows Server 2016 VM using this script sample. It takes a few minutes for the script to run and create the VM.

Once the VM has been created, open port 80 for web traffic with Add-AzureRmNetworkSecurityRuleConfig as follows:

Get-AzureRmNetworkSecurityGroup `
  -ResourceGroupName $resourceGroup `
  -Name "myNetworkSecurityGroup" | `
Add-AzureRmNetworkSecurityRuleConfig `
  -Name "myNetworkSecurityGroupRuleWeb" `
  -Protocol "Tcp" `
  -Direction "Inbound" `
  -Priority "1001" `
  -SourceAddressPrefix "*" `
  -SourcePortRange "*" `
  -DestinationAddressPrefix "*" `
  -DestinationPortRange "80" `
  -Access "Allow" | `
Set-AzureRmNetworkSecurityGroup

To connect to your VM, obtain the public IP address with Get-AzureRmPublicIpAddress as follows:

Get-AzureRmPublicIpAddress -ResourceGroupName $resourceGroup | Select IpAddress

Create a remote desktop session to your VM:

mstsc /v:<publicIpAddress>

On the VM, open an Administrator PowerShell command prompt. Install IIS and required .NET features as follows:

Install-WindowsFeature Web-Server,Web-Asp-Net45,NET-Framework-Features

Create deployment group

To publish the web deploy package to the IIS server, you define a deployment group in Team Services. This group allows you to specify which servers are the target of new builds as you commit code to Team Services and builds are completed.

In the Team Services Build & Release hub click Deployment groups. Click Add Deployment group. Enter a name for the group, such as myIIS, then select Create.

In the Register machine section, make sure Windows is selected, then check the box to Use a personal access token in the script for authentication. Click Copy script to clipboard.

Screenshot showing update to code

Team Services generates a script that downloads and configures an agent on the VM that receives new web deploy packages then applies it to IIS.

Back in the Administrator PowerShell session on your VM, paste and run the script copied from Team Services. When prompted to configure tags for the agent, choose Y and enter web. When prompted for the user account, press Return to accept the defaults.

Wait for the script to finish with a message Service vstsagent.account.computername started successfully.

In the Deployment groups page of the Build & Release menu, open the myIIS deployment group. On the Machines tab, verify that your VM is listed.

Screenshot showing update to code

Import sample code into your Team Services Git repo

In the VSTS portal, navigate to Code hub for your new VSTS project. Select the option to Import repository.

Screenshot showing menu item to import a repository

In the Import a Git repository dialog, paste the following for Clone URL.

https://github.com/adventworks/aspnet4-sample

Click Import to copy the sample app code into your Team Services Git repository.

Set up continuous integration

On the Files tab of the Code hub, click Set up build.

Screenshot showing button to set up build for a repository

You are taken to the Build & Release hub in VSTS and asked to Choose a template.

In the right panel, select ASP.NET, and then click Apply.

Screenshot showing ASP.NET template

You now see all the tasks that were automatically added to the build definition by the template. These are the steps that will automatically run every time check in code.

For the Default agent queue, select Hosted VS2017.

Click the Triggers tab in the build definition. Enable the Continuous Integration trigger. This will ensure that the build process is automatically triggered every time you commit a change to your repository.

Select Save and queue to kick off your first build. On the Queue build dialog, select Queue.

A new build is started. You will see a link to the new build on the top of the page. Click the link to watch the new build as it happens. Wait for the build to complete and succeed before proceeding to the next section.

Set up continuous deployment

Once the build succeeds, click the Release action on the build summary page.

Screenshot showing build summary

In the dialog that prompts to Create release definition, select Yes.

In the Create release definition wizard, select the IIS Website and SQL Database deployment template, and click Apply.

Screenshot showing IIS template

Click Tasks, and then select the SQL Deployment phase. Click 'X' to delete this phase. We won't be deploying a database in this quickstart.

Select IIS Deployment phase. For the Deployment Group, select the deployment group you created earlier, such as myIIS. In the Machine tags box, select Add and choose the Web tag.

Select the IIS Web App Manage task; click 'X' to delete this task. We will not create a new website for this quickstart. Instead, we will deploy to the default web site.

Select the IIS Web App Deploy task to configure your IIS instance settings as follows. For Website Name, enter default web site. Leave all the other default settings.

Screenshot showing release definition

Select the artifact trigger and make sure the Continuous deployment trigger is enabled.

build artifact trigger in release definition

Click Save. In the Save dialog box, click OK.

To test the release definition, click Release and then Create Release.

create release

On the Create new release dialog box, click Queue.

You will notice a new release being created. Select the link to navigate to the release.

new release created message

You can watch the live logs for the deployment as it happens. Wait for the release to be deployed to the Azure web app.

Update to redeploy the code

Navigate to the Code hub in the VSTS portal. Navigate to Views/Home/Index.cshtml file. Make the following simple change to that file by selecting the edit action.

Screenshot showing update to code

Add the following line of text as indicated in the image above:

<h1>Demo of CI/CD!!</h1>

Commit your changes in Git. This change triggers a CI build, and when the build completes, it triggers an automatic deployment to Azure web app.

Browse to the app

Once deployment has completed, open the browser and test your web app.

http://<publicIpAddress>

Congratulations! You've deployed changes to your application using CI/CD.

Clean up resources

When no longer needed, you can use the az group delete command to remove the resource group and all related resources.

az group delete --name myResourceGroup