Sunday, November 13

Building .NET Application with Jenkins using MSBuild and MSTest

Objective
Quick feedback to the developers and transparent information about the health of the projects. This post will focus on .NET application.

Environment
Small team of 3 developers. Two projects: .NET WPF client application and J2EE RESTful web service.

Why Jenkins

I've decide to use Jenkins as Continuous Integration server. I've use CrouseControl.NET in the past but was not so happy about it. TeamCity was another very good option and I've decide on Jenkins after all because of the strong community involvement and quick development around it, and also the fact that it is free and stay free was important factor.

Notice
Those instructions are given as-is without warranty. Use them at your own risk. You should ensure you have the proper license to run each tool on your machines.

Step-by-step

  1. Download Jenkins for Windows.
  2. Run Jenkins setup. Install it in C:\CI\Jenkins (no spaces in name)
  3. Stop Jenkins service, open Jenkins.xml and change http port to 6080. Restart the service.
  4. Open http://localhost:6080 to confirm Jenkins installation
  5. Install GitExtensions to C:\CI\GitExtensions. Install msysgit co C:\CI\git
  6. Go into Jenkins configuration -> Plugins -> Available and install git plugin. Go into Jenkins -> Configuration and change Path to git execution to C:\CI\git\cmd\git.cmd (not git.exe)
  7. Change Jenkins windows service to run with specific Windows user by open the Services mmc, open the Properties dialog for Jenkins service and change the login user to a user in your machine.
  8. Login with the user and run command to create ssh public/private key: open git bash goto ~/.ssh and run ssh-keygen.exe -t rsa -C "your-name@your-domain.com"
  9. Use the command cat id_rsa.pub in git bash to get the public key (remove new lines)
  10. Paste the public key in the remote git site (assembla, github).
  11. Create new build task. Choose git as SCM and enter your remote repository address.
  12. Run the build task, which will hang, stop it after few seconds and copy the command Jenkins execute. Paste that command into command line window and run it. You will get a message ask to approve the remote service fingure print, approve it.
  13. Run the task again and everything should work as planed.
  14. Go into Jenkins configuration -> Plugins -> Available and install MSBuild plugin
  15. Go into Jenkins configuration and under MSBuild click Add MSBuild in the name enter .NET 4 and in Path to MSBuild enter C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe
  16. Download Windows SDK for .NET 4 - select GRMSDK_EN_DVD.iso. Install only ".NET Development" component. (Update: Please download Windows 8.1 SDK if you are using Windows 8 and above).
  17. Using this script copy MSTest related files from your development machine into CI server at C:\CI\MSTest
  18. Use gacutil from Windows SDK to register ".Resource.dll" and ".UnitTestFramework.dll" - it is important the files will have version 10.0.0.0 (not 10.1.0.0)
  19. Run mstest registry file [MSTest Registry Export] to add test types and other values request by MSTest to the CI server registry
  20. Go the the job configuration and add build step of type Execute windows batch command with the following command Test Batch Command
  21. Go into Jenkins configuration -> Manage Plugins -> Available -> and select  JENKINS MSTest plugin
  22. Go into the build configuration. In the Post Build Actions check Publish MSTest test result report and write TestResult.trx in Test report TRX file
[MSTest Registry Export]
You will need to export values from your registry in order to run MSTest on Jenknis machine.
For 32bit machines export HKLM\SOFTWARE\Microsoft\VisualStudio\10.0\EnterpriseTools\QualityTools
For 64bit machines export HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\EnterpriseTools\QualityTools

If you have 64 bit developer machine and 32 bit Jenkins server just do search and replace of Wow6432Node\ to nothing to remove the 64 bit registry path part.

[Test Batch Command]
del /f /q TestResult.trx
    C:\ci\mstest\mstest.exe /noisolation /resultsfile:TestResult.trx /testcontainer:Project\Project.Test.dll

    Sources
    I've use the following posts and articles to build my Jenkins server.

    Jenkins

    http://zeljkofilipin.com/jenkins-windows-and-git/


    unit test without VS

    Conclusion
    I've only start using Jenkins now but it already helping to improve our project. It took about 4 late nights to play with it and set it up but I think it worth the trouble.
    Next I'm going to add deployment package creation automation, after this will be solid I hope to get to continuos deployment by pushing the packages to our web site.
    Also add build task for our J2EE web service and finally put a wall-screen to present the state in our development room.