Continuous Integration and Delivery for Beginners (DevOps) – DEVELOPPARADISE
17/07/2018

Continuous Integration and Delivery for Beginners (DevOps)

Introduction

DevOps is a combination of software development and operation (Dev & Ops) which strongly advocate automation at all steps in software development process. This will enable faster delivery, more stable operating environment with minimum human errors, less maintain cost and many more advantages for software development team.

For example, how about your live deployment if you building an enterprises level application which run on 10 webservers with load balancer? So, answer is here now !!

Earlier, DevOps is limited within certain team who named as DevOps engineers. But now it is time to start software engineers like you !!

Background

There are several tools available for continues integration and continues delivery. Jenkins and Visual studio team service are more famous within those tools. In this article I will more focus on how to do this using VSTS with basic steps. In my Next articles I will more focus on more advance features in VSTS.

There are so many articles for azure deployment using VSTS. But still few guidelines and articles for on premises deployment using VSTS. Therefore, I will more focus on-premises deployments.

I assume that all of you familiar with basic operations of visual studio team service including git repository maintain. 

Pre-requisites

.Net 3.5 should be installed to server because web deploy will use Msdeploy which require .Net 3.5 to execute.

If this still not installed. Please run below powershell script in administrator mode.

dism.exe /online /enable-feature /all /featurename:NetFX3 /Source:D:/sources/sxs

 

Step 1 – Create deployment group

After you setup deployment group, it may have data base server and web servers. Then we can install deployment agents in each machine. After that, it will enable parallel deployments.

First navigate to the selected project in VSTS. Then open the “Build and release” tab and select new “Deployment Groups” as per the given image. Finally create your own deployment group by clicking “Create”.

Continuous Integration and Delivery for Beginners (DevOps)

 

Step 2 – Install VSTS Agent in on-premises server

Just copy and paste this PowerShell script in on-premises server (PowerShell should run with administrator mode and make sure to mark “Use a personal access token in the script for authentication” before copy the script). It will generate a personal access token to validate and authenticate with the web server.

This is the script contain that need to run, install and configure deployment agent whatever machine you need. Since this script has some GitHub Url it need to download and install some files your on-premises server. Therefore, it should have an internet connection. 

Once you run this script, Deployment agent will install to on-premises server and it will run and listen through windows services.

Continuous Integration and Delivery for Beginners (DevOps)

 

There will be few question ask by PowerShell when you run above code snippet.

First question for enter deployment group tags for agent – just enter few tags

Second question for enter user account for combination – use default account

Then move again to your VSTS acount and check the “Targets” tab in your deployment group. Then you can see the tags you enter in on-premises server. 

Finally Now your deployment group should be online if you follow these steps correctly !!

Continuous Integration and Delivery for Beginners (DevOps)

 

Step 3 – Configure build operation

In this step we will configure first build in VSTS and this will mainly base on MsBuild.

Navigate to ”Build and release” section and select “Build” tab. Then create a new build as per the given image. 

Continuous Integration and Delivery for Beginners (DevOps)

Select “VSTS Git” option (or relevant source control) and your team project.

Continuous Integration and Delivery for Beginners (DevOps)

Select ASP.NET as template in next screen.

Continuous Integration and Delivery for Beginners (DevOps)

Now you will end up with basic features of ASP.Net web deploy.

But depending on your project requirement these basic features will not enough for you. If you need more features you can search suitable plugin from market place. (https://marketplace.visualstudio.com/vsts) Most of them are freely available now. Still if you unable to find suitable plugging, you may write your own plugin.

In this article I will use npm and grunt as additional features. Because in my project, I have used grunt for Js, css minification, create css file using less file and generate above the fold content using penthouse to map google speed insight requirements. (https://developers.google.com/speed/pagespeed/insights/)

So, I need some grunt commands with my build operation.

Since we are discussing beginners level we will deal with most of default features in this build process.

Select “Phase1” feature and then select “Hosted VS2017” as agent queue.  You can rename Phase1 with any preferred name.

Continuous Integration and Delivery for Beginners (DevOps)

Other than that, build can be proceed with these default values. Depending on the project recrement you may alter the values.

As I mention above I will add npm and grunt additionally to my build definition. Please note that this is an additional feature and you may not need to use this unless you are using grunt command in your project.

Continuous Integration and Delivery for Beginners (DevOps)

 

Just use command as “install” without npm.

Continuous Integration and Delivery for Beginners (DevOps)

Follow the same steps for install grunt and add the grunt command without “grunt” prefix. (Make sure to add “Grunt File Path” in the plugin)

Then you can re order these individual tasks by dragging and dropping depending on your requirements. see below image.

Continuous Integration and Delivery for Beginners (DevOps)

 

According to the image you can see the buid pipeline. Below tasks are configured in that pipeline and it will run one after another

Use NuGet – will install nuget to build server

NuGet restore – will restore nuget with required packages according to package.config

Npm install – will install npm (Optional)

Grunt production – will run grunt task which mentioned in configuration (Optional)

Build solution – solution will build. You can use default “MSBuild Arguments” here.

Test Assemblies – will run all unit tests. If you have any test project please configure the paths here.

Publish symbols path – will publish the project. Default values will able to publish your project.

Publish Artifact – will create publish folder in this step. If you interest you can change path of publish or Artifact name (It will include variables and built project as a zip file). Anyway, default values will able to Publish the Artifact.  

Now you are almost done with the deployment process. Just save all setting and que a new build and check whether it build correctly. If there is an error you have to debug and figure it out before continue to next step. If there is any error just comment it below and I will help for figure it out. 

 

Step 4 – Set up the Release

Now it’s time to setup the first automated release. Move to “Build and release” tab and select “Releases” menu. Then select “Create release definition” and click “IIS Website and SQL Database Deployment” as per the given image.

Continuous Integration and Delivery for Beginners (DevOps)

 

Then you can enter environment name like beta or live etc. 

Continuous Integration and Delivery for Beginners (DevOps)

 

Then you will end up with release pipeline configuration menu. Select “Task” tab and you need to complete some configurations here. 

Then you should select “IIS Website” as Configuration type.
Any name as Website name.
your website app pool name as Application pool name
as per the given image.
 
Continuous Integration and Delivery for Beginners (DevOps)
 
 
Then select “IIS Deployment” tab and Fill it with mandatory fields.
Deployment group (Deployment group which you already created in previous steps)
Enter tags if you interest and just save the form with default values for rest of fields.
 
Once you configure, move to “IIS Web App Manage” section.
Configuration Type (IIS Website)
Enter website Name (Any name)
Action (Create or Update)
Physical path (D:/Projects/Applications/TestApp etc.)
IIS Application pool (Current app pool details)
 
Continuous Integration and Delivery for Beginners (DevOps)
 
Now you are almost done with configurations. Just few more steps to enable continues integration.
There are few additional tasks as “Copy Files to: Backup”, “Archive $(Build.BackUpPath)” and “Run Sql Command”.
We will discuss these later in this article and they are not require to basic deployment.
 
 

Step 5 – Enable CI and CD

Select “Builds” from navigation and select “Edit definition” task as per the given image below.
 
Continuous Integration and Delivery for Beginners (DevOps)
 

Then select “Triggers” tab and just mark “Enable continuous integration” checkbox. If you interest to enable disable this feature in branch wise, you can do it here also.

Once you enable this it will automatically trigger when you committed to the particular branch. 

 
Continuous Integration and Delivery for Beginners (DevOps)
 
Then you need to set the trigger for release. Then it will do the deployment to on premises server.
So, navigate to “Release” menu and edit.
Then Select pipeline tab and click “Continuous deployment trigger” icon as per the image.
Finally enable the Continuous deployment. If you interest you can add the branch filters also.
 
Continuous Integration and Delivery for Beginners (DevOps)
 
Wow !! You Just Complete You Forst Continues Intergration Project …

Now it’s time to test your first project. Please do a small change your code and commit and wait until deployment complete. That’s it.

 

Some Tips you may helpfull !!

1 – Please use variable as much as possible.

It may help full for longer run and maintains for these configuration.

Variable available in “Build”and “Release “configuration” tabs. Use variable prefixed with ‘$’ sign.

eg : $(Build.ProjectPath)

Continuous Integration and Delivery for Beginners (DevOps)

 

2 – Mange connection string and other web.config variables

As per the above example you just need to add connection string name as variable name and connection string as the value in release variable section. It will automatically replace the web.config values according to the variables. 

<add name="umbracoDbDSN" connectionString="server=xxxx;database=yyyy;user id=zzz;password='aaa'" providerName="System.Data.SqlClient" />

This is our web.config so,

Varible name => umbracoDbDSN

Value => server=xxxx;database=yyyy;user id=zzz;password=’aaa’

 

3 – Include additional folder to deployment (Add files to build artifact)

For example, if you need deploy all files in “Content/minified” folder but if it still not deploying, 

Open your .csproj file and add following code (Please change values depend on your requirement)

<Content Include="Content/minified/**/*.*">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>

 

4 – Backup file before deployment

Add “Copy Files” task to your release definition as per the given image. Then enter values as below.

Source Folder (Your file path)

Contents (Files which need to copy included in above path e.g: bin/*)

Target Folder (Destination folder)

Continuous Integration and Delivery for Beginners (DevOps)

 

5 – Backup database before deployment

Add “Run Sql Command” task to your release definition as per the given image.

Server Name (database instance name)

Database Name (Your data base name)

SQL Command (Please change values)

BACKUP DATABASE [yyyy]
  TO DISK = 'F:/SQLbackup/$(Build.BuildId).bak' 
GO

SQL Server Login (data base user name)

SQL Server Authentication password (user password)

Continuous Integration and Delivery for Beginners (DevOps)

 

6 – Configure automatic email trigger for approval

There is option to pre and post deployment approvals. You can enter users or user groups here. If user group added only one person approval will enough to complete the deployment.  

Continuous Integration and Delivery for Beginners (DevOps)

 

Next Step

In this tutorial we followed in very simplest way. In My next article I will discuss more advance features in Continues integration including how write a custom extension to Visual studio market place.

In addition Jenkins is another solution to continues delivery and integration. It is more flexible more complex than visual studio. So, I will setup the same environment using Jenkins with my upcoming articles.