[HOWTO] Execute C# build from PowerShell on Jenkins

At d-fens we usually use JetBrains TeamCity as a build server. Because one of our customers uses Jenkins as build server I had the chance to set up a build job for one of our C#/.NET projects on Jenkins. The project I mentioned is a C#/.NET Web Application built with Visual Studio 2013. For testing we make use of the Visual Studio built in unit test framework called Microsoft.VisualStudio.QualityTools.UnitTestFramework.
All the build jobs get created automatically by a Jenkins CI plugin that sets up the build jobs according a configuration provided by a seed script. The seed script resides in a git repository. Every project repository then contains a build script that gets executed by the build server. This setup allows an easy ramp up of new jenkins instances. The advantage of having the build script directly in the projects git repository is, that the project can be built at every commit with the suitable configuration.

While preparing the machine the build server is running on and during writing the PowerShell script for building the C# application I had to gather the required information from different blog posts, StackOverflow posts, etc. I thought it could be helpful for other to create a short summary of all the steps I had to perform until I was able to run the build.

I won’t describe or write about the installation of Jenkins in this article because when I started with my tasks Jenkins was already installed and running on the Windows machine.

Preparations to enable building C#/.NET projects

The list below describes all the necessary steps I had to perform to prepare the build server for building C#/.NET projects. It’s important to mention that I wanted to avoid installing Visual Studio on the Jenkins server.

  1. Install latests windows updates on the target machine
  2. Install the desired version of the .NET Framework (i.e. .NET Framework 4.6)
  3. Install the appropriate .NET Framework Targeting Pack (i.e. .NET Framework 4.6 Targeting Pack – Same version as the .NET Framework in step 2)
  4. Install Microsoft Build Tools 2013
  5. Install Microsoft Test Tools 2013
  6. Download NuGet.exe
  7. Copy the file Microsoft.PowerShell_profile.ps1 to C:\Windows\SysWOW64\WindowsPowerShell\v1.0 and C:\Windows\System32\WindowsPowerShell\v1.0
  8. Copy the file Microsoft.WebApplication.targets from a machine where Visual Studio is Running to C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\WebApplications\ (Create folders if they do not yet exist)

PowerShell Build Script

The PowerShell script below gets triggered by Jenkins and performs the build of the project including the execution of the unit tests

  1. NuGet restore – Restore NuGet packages
  2. Building the sources with MSBuild
  3. Executing the unit tests with MSTest
    1. Remove existing test result file
    2. Create new test result file
  4. Testfolder cleanup

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: