Setting up CI/CD pipeline to deploy a .net core application to an IIS Server through Azure Devops
Tags: azure, devops, dotnet, iis
Published: 2020-04-17 19:29:01

----------
This article will showcase on how to deploy your .net core application to an iis server through a pipeline configured in azure devops.

#### Overview

Recently I got a project requirement to publish a .net core web application to an iis server in an azure virtual machine. The major scope of this project was to configure, build and release pipelines to deploy the web application without any manual interactions unless it was needed. Therefore, I thought of sharing the experience i got while doing this project from the scratch.

_Note: How to configure the DNS would not be explained in this tutorial._

----------

What you will learn

-   Add the project source to azure devops source control via git.
-   Add Azure Virtual Machine.
-   Setup IIS Manager in azure vm — windows server 2016
-   Setup build pipeline.
-   Setup deployment group.
-   Setup release pipeline.
-   Provision agents for deployment.

Prerequisites to begin:

-   .Net 3.0 SDK installed

----------

#### Step 1: Creating a repository in azure devops  
  

Login to azure devops and create a project by selecting git as the source control. Then selecting the menu item repos, will show you the repository to be initialized. If you are using an existing project with repositories, then simply go to repos and add a new repository. Or else, if you are already having a git repository in somewhere else other than azure devops you can import it by selecting the import option. Once you have finished creating a new repository, you are able to clone the repository as preferred, using HTTPS/SSH. (SSH Recommended)

![](https://cdn-images-1.medium.com/max/880/1*YiI2fpeDxqtFx2RZj5H0Qw.png)

clone repository

Clone the repository into your local file folder by executing the following command.

git clone {clone-url-copied}

Change directory to the cloned folder and create the .net core web application. If you are familiar with the dotnet commands it will be easy to use the following steps to create the application. Else you can open up visual studio and create the application.

```javascript
dotnet --help //if needed

dotnet new sln -n {solution-name}  
dotnet new webapp -n {project-name} //3.0 .net core web app  
dotnet sln add {project-name} //only if the project is in same directory. else provide the path

dotnet build

git add .  
git commit -m "commit message"  
git push
```

The project source will be added into the repository by now as mentioned in the figure below.

![](https://cdn-images-1.medium.com/max/880/1*7R86DAUpwz6bQq5gWExYGw.png)

#### Step 2: Setup the build pipeline

  

Now everything is ready to setup the build pipeline for this application. Two ways to setup the build pipeline. As shown in the above figure we can click on setup build, which will provide us most relevant build templates per the source and create the pipeline with YAML. Else we can go from the scratch by selecting pipelines → new pipeline → use the classic editor. Going forward, user will be asked to select the version control, project, repository and the branch to trigger the build. Providing the required data will take you to the editor to select a template.  
Select the ASP.NET Core template and apply.

![](https://cdn-images-1.medium.com/max/880/1*84nfirxi-kxhLq3XEJx_Og.png)

This template will include 5 tasks to build the application.

![](https://cdn-images-1.medium.com/max/880/1*VxOVwW5hlSSbLuBxz6yvrw.png)

You can give a preferred name to the build pipeline by editing the first highlighted area on the top. The task highlighted (Use sdk task) would not be included in this template. It is added manually by clicking on the + icon on the top to ensure that we are using the correct sdk version which the project is using. In a scenario where, application is using .net core version 3.1.100 we will have to change the version into 3.1.100. Otherwise the build will fail. Anyhow, in our scenario we are using 3.0.100

Other tasks can be left with the default values since this is a simple .net core web applications.

![](https://cdn-images-1.medium.com/max/880/1*3L1OpnDImLlOb104YvkU0w.png)

Navigate to the Triggers section and check the _Enable continuous integration checkbox._ This means that, when ever the master branch gets a commit by a developer, this build pipeline will trigger.  Now we are ready to run our first build. Click on the Save & queue drop-down and select save and queue. This will prompt you asking for some parameters. Keep the default values for now and click on save and run.

By now you have triggered the build pipeline manually. You can observe the pipeline by clicking on the ongoing job. The logs of each tasks can be seen after clicking on the ongoing job as shown below.

![](https://cdn-images-1.medium.com/max/880/1*iyjFpK2ylqzLXpXctKr8OQ.png)

We are done with the build pipeline now. Lets move on to step 3.

#### Step 3: Creating a Azure Virtual machine

  

Sign in to your azure account and login to the azure portal. Create a new resource group. In the search by search for, “virtual machines”. If you don’t have an existing virtual machine please create a new by clicking on add or create virtual machine. Following figure will provide support in creating the vm.

![](https://cdn-images-1.medium.com/max/880/1*jSTavzJ4knO7XMTa4ABGag.png)

Note: I have selected a low disk size since this is a demo application. You are free to select per your requirement. Furthermore, I have used Remote Desktop Protocol(RDP) as the connection, which will provide a user friendly GUI to connect to the VM. Proceed creating the vm by clicking on review + create → create. (Will take few minutes) Successful creation will provide the following.

![](https://cdn-images-1.medium.com/max/880/1*E2wMemb9tMWp5mWfsc8xXw.png)

Navigate to the Virtual machine resource → overview. Here you will see the analytics of your vm for the selected duration.

![](https://cdn-images-1.medium.com/max/880/1*awGnkNZyb2Gjh1-DdcUwvw.png)

(IP address and port should exist. removed for security concerns in above img)

Click on connect and select RDP. Once you are navigated to the connect view, download the RDP file by selecting a public or private IP address as required. Open the downloaded RDP file and click on more choices to use a different account and enter the username and password you entered while creating the virtual machine. This is the local account approach. If the vm is belongs to a domain, enter the {domain/username}. PS: user must be in administrator group or must have remote access privileges.

![](https://cdn-images-1.medium.com/max/880/1*seTwBLu9M30ZFbBRBUl8Pg.png)

Now the user is logged into the virtual machine.

#### Step 4: Setup IIS in azure virtual machine

  

After login into the vm follow the following steps.

search for server manager → Add roles and features → Next (Before you begin) → Installation Type (Role based or feature based) → Server selection (Select server from pool) → Server Roles (Enable Web Server (IIS)) → Features (Enable .Net Framework 3.5) → Web server Roles → Role Services (Enable IIS 6 Management compatibility)

Before installation please refer the following figure to ensure you have configured IIS properly. If everything is correct you are good to go.

![](https://cdn-images-1.medium.com/max/880/1*gb1HUU72pSZ1HXQto2GFoQ.png)

Will take some time to finish the installation. Till that you can install the,  
[.net core 3.0 hosting bundle](https://dotnet.microsoft.com/download/dotnet-core/thank-you/runtime-aspnetcore-3.0.0-windows-hosting-bundle-installer). If IE does not allow to download the bundle turn off the enhanced security option as follows.

![](https://cdn-images-1.medium.com/max/880/1*FzxIFzQbuhz9cpTqfcyHsg.png)

#### Step 5: Setup release pipeline

  

Back to azure devops. In order to deploy the web application to IIS we should create a deployment group and provision a service agent in our windows server 2016 in the virtual machine.

Deployment group:

A deployment group represent the environment that the application will be deployed to. It is a set of target machines each having a service agent installed.

Navigate to pipelines → deployment groups in azure devops. Click on +New to create a new deployment group.

![](https://cdn-images-1.medium.com/max/880/1*9cap1Dovq3aXTt0dyzJT9w.png)

Creating a deployment group will automatically create a deployment pool for you. But deployment groups and pools can be shared among projects specifying the scope from tags. For this demo I will continue with the new deployment group and pool.

Now we need to install the service agent into the target machine. To install, the generated script must be executed in the server **powershell as an admin**. To authenticate the script we must attach the personal access token of the user to the script. Devops will create a token and handle that for you if you check the check box highlighted in the following image. If the token is attached, we are not prompted to enter the authentication type and the credentials. Else will have to provide the type and the credentials.

![](https://cdn-images-1.medium.com/max/880/1*a7KWcr8N_1XSRfepzjMcuA.png)

**_Note: In azure devops server this option does not exist. Therefore, if you are willing to use the authentication type as PAT, you should create a PAT from devops and give manage and view access permissions._**

Go to virtual machine and open the windows powershell as an admin and execute the script copied to your clipboard. Will take few minutes to register the agent.

![](https://cdn-images-1.medium.com/max/880/1*nRjtP-J-RIAadTXN_c_Vig.png)

As mentioned in the above figure, while registering the agent it will prompt for several requirements.

-   Enter replace →y
-   Group tags → y
-   Add tags → dev, prod (to specify the scope the deployment group will be handling we using tags)
-   Add credentials we used while creating the vm as we are logged in to the vm through that account.

Now check the deployment groups in azure devops. You will see the following. The agent is installed and the group is active.

![](https://cdn-images-1.medium.com/max/880/1*GzCkOgYNtZmnN_VCJS8h7A.png)

Step 5: Setup release pipeline

We are almost done now. Go to releases → new release if no prior releases. Follow the below images. Will explain after the figures.

![](https://cdn-images-1.medium.com/max/880/1*oWARM60S28RinYXbMifPNg.png)

![](https://cdn-images-1.medium.com/max/880/1*_5wIQQ6gvQL4s1XY5Ts2Mw.png)

Select the add artifacts and add the build pipeline as the reference to this release. Make sure you select the correct build if you have several builds.

![](https://cdn-images-1.medium.com/max/880/1*XeM5jlDaAumf3iav7871fQ.png)

To enable continous deployment, follow the steps below

![](https://cdn-images-1.medium.com/max/880/1*EOgMeOScUh7UykMztqijxA.png)

Go to the tasks by selecting the link in the stage

![](https://cdn-images-1.medium.com/max/880/1*oQO2xlefV7pNRhmjAcZdpw.png)

Select IIS Deployment and add the correct deployment group we configured. If you need to strictly specify the scope of the deployment use the tags you added while registering the agent. And make sure the artifacts are taken from the correct build.

Moving onto next task.

![](https://cdn-images-1.medium.com/max/880/1*BsH6ECu4qgDes__1M37JqQ.png)

Enable IIS check will be a good option to check if the iis is installed in the target machine or not. And action as create or update will check for the existing website with the same name and if exist it will update else will create new one. Make sure the path is correct. it should be the root folder of iis. If you need to customize the url bindings. unlink the parameteres from the Job itself and add as preferred. For now i am keeping the hostname undefined which is equal to the localhost of the iis server.  
_Will look into DNS configuration in a future article._

![](https://cdn-images-1.medium.com/max/880/1*0_8FJ8LiSoIhgZujcm7xTg.png)

Good to go keep the defaults in deploy task. Now save the configurations and create a release. Select the latest build version and create.

![](https://cdn-images-1.medium.com/max/880/1*DUyvoKxZKaH9_8FiiKAuuA.png)

Navigate to the release displaying in top. click on the logs button to view the logs. Successful deployment will show the below figure.

![](https://cdn-images-1.medium.com/max/880/1*krpUy-pTtRjfJLvHr4vdYA.png)

As the final step you can navigate to the iis server in azure vm by going to server manager → iis → select server and right click → select iis manager and go to the site deployed and browse.

![](https://cdn-images-1.medium.com/max/880/1*pB-3GWzd9gBAD5CEviDZfg.png)

References:  
[https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-3.1](https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-3.1)/  
[https://docs.microsoft.com/en-us/azure/architecture/solution-ideas/articles/cicd-for-azure-vms](https://docs.microsoft.com/en-us/azure/architecture/solution-ideas/articles/cicd-for-azure-vms)

Raise if any questions :)