Continuous Integration Of ASP.NET CORE API Application In VSTS

Introduction 

Visual Studio Team Service is a Microsoft product which provides code management, Project Management, Testing, Continues integration and many more functionalities. In this article, I’m going to discuss about how to implement a continues Integration of ASP.NET CORE API application in VSTS.

Prerequisites
  1. Visual Studio 2015/2017 
  2. Azure subscription

This article flows as per the following.

  1. Create a new project in VSTS with GIT version control
  2. Add an existing ASP.NET CORE API project to the newly created GIT repo from visual studio
  3. Configure a build in VSTS
  4. Configure a release in VSTS with continues deployment of Azure App Service

Create a new project in VSTS with GIT version control

Sign in to your Visual Studio Team Service account.

 

Click on "New Project" from the homepage.

 
Name your team project. In my case, I named it ASP.NETCoreAPIDemo.
I have selected a GIT from the version control with Agile work process. We can choose either GIT or Team Foundation Version control based on project need.



Our repository is created. Let's add a code to it in the following steps. 

Add an existing project to the newly created GIT repo from Visual Studio

I have an existing ASP.NET Core API project on my local machine which I’m going to publish to VSTS GIT.

Open the existing project in VS. Go to File-> Open.
This project consists of a GET RESTFull service which will give you the list of technologies, as shown in below figure.

 

In the right bottom of Visual Studio, you can find "Add to Source Control" or else do File->Add to Source Control, as shown in the below figure.

 

The team explorer window will be opened with two options,

  1. Push to Visual Studio Team Service and
  2. Push to Remote Repository, (We already created a GIT repo in TFS so we can do push to remote repository).

Click on "Publish Git Repo".

Give your remote GIT url and click on Publish.

 

After successfull publishing and syncing, you can find the published files in respective project page of VSTS. 

Configure a build in VSTS

Open the newly created repository in VSTS. Go to build page and click "Add New Definition".

A window will be opened as shown in the below figure.

 
A new create build definition window will be opened with some list of predefined templates. Since we are working on an ASP.NET CORE API site, I’m going to select an Empty Template. Click on "Next" to change the settings.
 
Choose the respective project and click on "Create". The new build definition will be opened as shown in below figure.



Now we need to add some utility to build our application. Click on "Add build" to add the utilities; In our case, we need to add four utilities to build our ASP.NET Core application.

Step 1

Restore NuGet packages.

 
Tool - dotnet

Argument - Restore

Working folder - Choose the folder where the .csproj file present for Visual Studio 2017 will be stored or choose a folder where the project.json is available in case of Visual Studio 2015.

 

Step 2

Build the ASP.NET CORE project and publish the output.

 

Tool - dotnet

Arguments - publish -c $(BuildConfiguration) -o $(Build.ArtifactStagingDirectory)

Working folder - Choose the folder where the .csproj file is present for Visual Studio 2017 or choose a folder where the project.json is available in case of Visual Studio 2015

 

Step 3

Archive the output into a web deploy package.

 

Root folder to archive - $(Build.ArtifactStagingDirectory)

Prefix root folder name to archive paths - Unchecked

Archive type - zip

Artifact file to create - $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip

 

Step 4

Publish the build artifacts to be consumed by a release definition.

 

Path to publish - $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip

Artifact name - drop

Artifact type - Server

 

Once the build steps are completed, we need to setup the variables and default queue agent. 

Click on variables from the build definition menu, and add a new variable which is listed below.

Variables

  1. BuildConfiguration - release
  2. BuildPlatform - any cpu
 
 
Next, we need to change the default queue agent from general setting.

In my case, I used Visual Studio 2017 for creating an ASP.NET Core application. So, my default queue agent will be hosted on VS2017.



We are done with build definition settings. Click on Save button to save our build definition.

Create a Web App in Azure for deployment

Log into your Azure portal. Quickly create a new web app ( New -> Web+Mobile -> Web app) as shown in below figure.

To learn more about Azure App Services, click here,

 
 

http://myaspnetcoreapp.azurewebsites.net/  is the URL generated from the newly created web app.

Configure a Release in VSTS

Go to newly created GIT repo page. Select "Release" from build & release menu. The release page will be opened as shown in the below figure,


Click on "New definition".

The create release definition window will be opened with a list of deployment templates. Let's select Azure App Service Deployment and click Next.


Choose the respective project and build definition; then check on continous deployment; click on create to open the release definition page as shown in below figure.



VSTS will bring your Azure subscriptions automatically based on login. We just need to select the respective subscription. Click on "Authorize" and it will do an authorization process to get the app service name.



I haven't created any deployment slot so I will skip it in settings, finally give your URL in APP service URL and click on Save

Get back to code and let's add one more techology in our technologyList, to check the work flow of continous Integration once the commit and sync is done from visual studio

Change the code, as shown in below figure.

 

Build Status in VSTS

  
Release Status 

 
Test the updated response for the request http://myaspnetcoreapp.azurewebsites.net//api/Technologies/TechnologiesList



Summary

We have learned how to create a build and release definition for the ASP.NET Core application with Azure App Service for the deployment in VSTS and implemented the continous integration.

Reference

https://www.visualstudio.com/en-us/docs/build/apps/aspnet/ci/build-aspnet-core

I hope you have enjoyed this article. Your valuable feedback, questions, or comments about this article are always welcome.

Up Next
    Ebook Download
    View all
    Learn
    View all