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.
- Install latests windows updates on the target machine
- Install the desired version of the .NET Framework (i.e. .NET Framework 4.6)
- Install the appropriate .NET Framework Targeting Pack (i.e. .NET Framework 4.6 Targeting Pack – Same version as the .NET Framework in step 2)
- Install Microsoft Build Tools 2013
- Install Microsoft Test Tools 2013
- Download NuGet.exe
- Copy the file
Microsoft.PowerShell_profile.ps1
toC:\Windows\SysWOW64\WindowsPowerShell\v1.0
andC:\Windows\System32\WindowsPowerShell\v1.0
- Copy the file
Microsoft.WebApplication.targets
from a machine where Visual Studio is Running toC:\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
- NuGet restore – Restore NuGet packages
- Building the sources with MSBuild
- Executing the unit tests with MSTest
- Remove existing test result file
- Create new test result file
- Testfolder cleanup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param | |
( | |
[string]$build = "Debug" | |
, | |
[switch]$test = $true | |
) | |
$msBuildPath = 'C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild'; | |
$msTestPath = 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\'; | |
$testResults = "TestResults.trx"; | |
$env:Path = $env:Path + ";" + $msTestPath; | |
nuget restore .\src\ArbitrarySolution.sln –MSBuildVersion 12; | |
& $msBuildPath src\ArbitrarySolution.sln /property:Configuration=$build; | |
if ($test) | |
{ | |
Write "Execute unit tests"; | |
if (Test-Path $testResults) | |
{ | |
rm $testResults; | |
} | |
MSTest.exe /testcontainer:.\src\ArbitrarySolution.Project1.Tests\bin\Debug\ArbitrarySolution.Project1.Tests.dll /testcontainer:.\src\ArbitrarySolution.Project2.Tests\bin\Debug\ArbitrarySolution.Project2.Tests.dll /resultsfile:$testResults; | |
} |