Very lately I have worked on setting up some continuous integration (CI) using MSbuild for my c# project. In this blog series I will explain to you how to set up continuous integration.
First of all we will start with creating a MSBuild script which will compile our code. We will also create a small batch file for easy executing the MSbuild script.
When that is in place, we will add several targets to the build script to run for example unit tests, integration tests, code coverage, packaging etc.
So we will take the agile approach and improve our build process based on validated learning.
First of all we need some basic knowledge about MSbuild. I think there are enough pages around on the web which will give you a basic introduction. A good starting point would be the MSDN documentation for MSBuild.
Let’s consider we have the following project structure within our Git repository / local file system.
- src (contains our source code)
- lib (contains build output, and third party libs not available on nuget)
- reports (contains code coverage reports, test scenarios and test results)
- ci.msbuild (your msbuildfile)
- ci.msbuild.cmd (your batch file to execute the build on your local machine)
In this first version of our build file we only have one target (Compile). This target will build our projects. We do not use the solution file because someone can disable items from the build in the Ā solution configuration. By using the projects explicitly we are sure all necessary projects will be build. We also have aĀ PropertyGroup to define some variables.
Using following batch file we can easy execute the build script and providing a Configuration for the build.
As you can see we use msbuild.exe and passing our ci.msbuild file as an argument. The /nologo parameter removes some msbuild header texts from the console output. TheĀ /m parameter enables parallel build based on the processors available in your machine. TheĀ /v:mĀ parameter sets the verbosity to minimal so not everything getsĀ outputted to the console. The /t:Compile parameter tells msbuild.exe to execute the Compile target from our build file and the /p:Configuration=%config% parameter injects our build configuration (Debug/Release) into the build.
When we would skip the /pĀ parameter the default Configuration will be Debug as defined in the PropertyGroup of our build script. When we skip theĀ /t parameter the default target Compile will be executed as defined in our msbuild file.
So for now you are able to build your projects using a simple batch file. In the next part of this blog I will describe adding a LoadNuGetPackagesĀ target which will download all the Nuget packages based on your package configs. At least most of us are using Nuget today. We will also be adding aĀ Clean target which will clean our directories before we will compile again, so we won’t get stuck with possible old assemblies from earlier compiles. Please share this article with your friends and please be curious for the next part of this series :D.