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)
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.
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.
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.
This template will include 5 tasks to build the application.
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.
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.
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.
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.
Navigate to the Virtual machine resource → overview. Here you will see the analytics of your vm for the selected duration.
(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.
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.
Will take some time to finish the installation. Till that you can install the,
.net core 3.0 hosting bundle. If IE does not allow to download the bundle turn off the enhanced security option as follows.
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.
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.
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.
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.
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.
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.
To enable continous deployment, follow the steps below
Go to the tasks by selecting the link in the stage
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.
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.
Good to go keep the defaults in deploy task. Now save the configurations and create a release. Select the latest build version and create.
Navigate to the release displaying in top. click on the logs button to view the logs. Successful deployment will show the below figure.
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.
References:
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
Raise if any questions :)
Top comments (0)