Continuous Integration for .Net Deployment (Jenkins and NAnt)
Summary:
One of the repetitive tasks that we do in product development is project deployment and depending on the size and complexity of the project; the time to invest in deployment process might be a huge concern. You may not be pushing all of the builds to the production server but to test each build we need to go through a deployment phase on local server. This is all about automating the stuff that we do manually on day-to-day basis which of-course is prone to human-mistakes and time consuming.
Now, this may include just a single website or a bunch of stuff together as a build. It may include few websites projects with a collection of web services, some windows services, and the databases of-course. Doing all of this every time we need to deliver the build, is time consuming and repetitive and each repetitive task always has a way to automate the things.
A frequent build deployment for the testing is going to cost a lot of time, which if saved; can be utilized in other productive work. Also, the manual process may involve mistakes and is not always fool-proof. It can also take time to investigate the issues caused by manual process.
The story behind:
The idea of this approach came up when we (as a team) realized we were spending too much time on the deployment process in our project. There needs to be some automated tools to do the repetitive task. So, as an example in this project, we need to do a bunch of things in each deployment as listed,
- A web application with 20 different configurations (different contents and separate virtual directory for each.)
- Couple of other web applications
- Five windows services
- Three web services
- Three databases
Now, imagine the time required to deploy all of this. There was definitely a scope for reducing time.
Solutions:
We chose Jenkins and NAnt because both are open-source and easy to learn & implement. Jenkins is one of the best open-source tools available to do the deployment process and along with that; with the help of NAnt tasks you can also verify the build with automated testing. It is easy to set up, learn and extend with the help of community provided plugins or your own scripts/batch files. Below are the tasks automated with the help of Jenkins and NAnt:
- Taking latest form source control (SVN in our case) for couple of repositories.
- Compiling the project framework using MSBuild. [Thanks to Microsoft for providing separate installation of MSBuild which previously was a part of Visual Studio 2015. So we don’t have to install Visual Studio on the build server now.]
- Compiling and publishing the projects [created a batch file to get the configurations for different 20 sites, which includes the content files and .config files.]
- Creating new databases. (We already had a tool to rebuild the databases. We just triggered that through jenkin’s job)
- Created batch files for installing/uninstalling the windows services.
- Automating the task for generating virtual directories through a batch file.
- Automating the task for adding registry entries of database connection strings with encryption for every website.
We used NAnt task to create a directory structure for each build, which contains separate sub-directories for each of the project inside that; so everything resides inside a common directory named as a [branch-name]_[build-number]; which if need to be removed can be easily done. We created batch files to install/uninstall the windows service from the same location instead of using MSI setups that will install the services to windows program files. We can use Service Control Manager to communicate with windows services which allows us to install multiple instances of the same windows service by providing separate names.
Conclusion:
With this system-designed using Jenkins and NAnt, now we can trigger the build process with a couple of parameters [like SVN branch name and database name] and everything else is taken care. We reduced significant amount of time after implementing automated jobs for the deployment phase. There are various plugins available for Jenkins to perform different task like NAnt, NUnit, Subversion, TFS, etc. Since, NAnt task can be used to trigger external programs like executable or a batch file; we can make use of small utility programs or batch files in the combination. Jenkins also has email notification feature which we can configure to get the email notification of the build reports. Another useful feature is, you can set your Jenkins job to run periodically and for scheduling jobs Jenkins uses, similar syntax like CRON.
Automating the jobs reduces the human errors like, doing a step in a wrong way or missing it completely. Reducing the time in deployment means, delivering the build for testing quickly, so you get more time to test and identify the build’s stability.
Reference article(s):
http://www.tutorialspoint.com/jenkins/
http://nant.sourceforge.net/release/latest/help/index.html
http://nant.sourceforge.net/release/latest/help/fundamentals/buildfiles.html [Example NAnt build file]
http://nantcontrib.sourceforge.net/ [Add-on to NAnt]

nice article