ASP.NET Core 2 Continuous Integration with GIT, VSTS and Azure

In this post, I will take you through the steps of creating an automated build and deployment process for your ASP.NET Core application, using Visual Studio Team Services (VSTS).

Continous integration is a great practice that reduces integration issues and helps you test your code earlier.

Our Goal

Our goal is to create an automated process that is triggered when we commit code to a GIT branch (develop in our example).

Our automated Build process will fetch the project’s code from a GIT repository, build it, run unit tests and create a package that is ready for installation on a web server.
The users in the project will be notified upon success, or in case of a build error or a failing unit test.

We then create a Release definition to automatically pick the package and install it on our Azure web application.

Important: To complete this guide, you will need to have an active Azure subscription, and create a Web App on Azure.

Build Definition

We will need a build definition for each environment. In this example, we will create a definition for the Development environment, that is based on the develop branch.

To create a new Build definition, go to Builds and Releases > Build and click the New button.

Start with an Empty process. Name your build and set the Agent queue.

At the end of this process, you build definition should look like this:

Step 1: Get Sources

This is a standard step where you get the code from a specific branch in your repository.

Step 2: .NET Core Tool Installer

This task will install the .NET Core tools required for building your application.

Step 3: Restore

This task runs the dotnet restorecommand to restore the packages used by your project.

To add the task, select the .NET Core task, then change the version drop down to 2.* (preview) and Command to restore.

Step 4: Build

This task runs dotnet build to build your project.

To add the task, select the .NET Core task, then change the version drop down to 2.* (preview) and Command to build.

Step 5: Test

This task runs dotnet test to run any unit test included in your project.

To add the task, select the .NET Core task, then change the version drop down to 2.* (preview) and Command to test.

Step 5: Publish

This task runs dotnet publish to generate a ready-to-install package of your built project.

To add the task, select the .NET Core task, then change the version drop down to 2.* (preview) and Command to publish.Make sure the Arguments field is set to:

Step 6: Publish Build Artifact

This task publishes the build package to TFS so it can be picked up by the release service later on. Note that the package is named “drop” which is the convention.

Trigger the Build Automatically

To trigger the build automatically whenever you commit code to the develop branch, open the Triggers tab and make sure the Trigger Status is set to “Enabled” and that the appropriate branch is selected.

Automatic Deployment

To automatically deploy your project to Azure whenever a new build is completed successfully, create a new Release definition.

Choose the Azure App Service Deployment template.

Click Add Artifact and select the drop package from Build Source.

Update the Environment name to Dev and click on the Path/ Task link.

Add an Azure App Service Deploy task to your release.

Now choose your Azure subscription and target Web App:

Turn Automatic Release on

To start a new deploymeny whenever a new build is available, turn on the Continous Deployment Trigger button on the Pipeline/ Artifacts page: