Continuous Integration (Part 2–Jenkins CI)

by Derek Pinkerton 5. June 2011 15:39

In this second part of a two part post I will cover setting up a continuous integration system using Jenkins CI, an open source continuous integration server, I will be referring back to the MSBuild project file created in part 1 of this post.

Installation

The first thing you’ll need to do is to install Jenkins. I was installing Jenkins on a imageWindows 2003 Server so I downloaded the native windows package. Jenkins supports multiple operating systems including Windows, Linux (multiple distributions) and Mac, so download the appropriate one for your platform. The Windows installer is very straightforward. Once installed your default web browser is launched, loading the Jenkins configuration web site. The site failed to load for me the first time, but I believe this is because the Jenkins service was still starting. I simply refreshed the site and it came up properly.

Configuration

You should now setup the configuration options for Jenkins. To do this click Manage Jenkins then Configure System. The main items you will need to configure are the Jenkins server URL, email server settings. There are a number of other options included in the default install of Jenkins that you may want to look over. Each item has a question mark icon to the right of it that you can click to get more information. Be sure to click the Save button at the bottom of the page when you are done making changes.

Plugins

Jenkins has a number of plugins available to extend its capabilities. Before setting up the build we’ll need to add the plugins for Jenkins that we’ll use. To install plugins click the Manage Jenkins link in the left hand menu from the Jenkins main page (the dashboard), then click Manage Plugins. From the plugin manager screen, click the Available tab and scroll through the list to find the plugins you would like to add. Check the box next to each plugin and then click the Install button at the bottom of the page. Wait until ALL plugins have installed and click the Schedule Restart button at the top of the page. Jenkins will restart and the dashboard will be displayed once the restart has completed.

Here is the list of plugins that I have installed.

  1. imageAuthentication and User Management
    1. Active Directory Plugin – Enables Jenkins to authenticate users against active directory/LDAP.
  2. Build Reports
    1. MSTest - This plugin converts MSTest TRX test reports into JUnit XML reports so it can be integrated with Hudson's JUnit features.
    2. Violations - This plugin does reports on checkstyle, pmd, cpd, fxcop, pylint, jcReport, and findbugs violations.
  3. Build Tools
    1. MSBuild - This plugin makes it possible to build a Visual Studio project (.proj) and solution files (.sln).
  4. Build Notifiers
    1. Email-Ext - This plugin is a replacement for Jenkins's email publisher. It allows you to customize the emails that Jenkins sends.
    2. Twitter – This plugin allows Jenkins to tweet build notifications on the Twitter social network.
    3. Hudson Speaks! - This plugin gives Hudson a voice using FreeTTS.
    4. Jenkins Sounds - This plugin allows Jenkins to play audio clips as build notifications.
    5. Instant Messaging - This plugin provides abstract support for build notification via instant-messaging.
  5. Other Post-Build Actions
    1. Subversion Tagging - This plugin automatically performs Subversion tagging (or copy) on successful build.
    2. Workspace Cleanup - This plugin deletes the project workspace after a build is finished.

Configuring the plugins

Once the plugins are installed we need to set some configuration settings in order to use them. To do this click Manage Jenkins from the left hand menu and then click Configure System. You will notice that this screen now has more options than it did the last time you were on it.

  • imageMSBuild – You will need to configure the path to the MSBuild.exe program. The location of this file depends upon the version of the .Net framework you are compiling against. There is a “Framework64” folder on 64 bit machines. As long as the .Net framework version you are targeting is installed, these folders should be present. You do not need Visual Studio to be installed for MSBuild to be present. I would suggest using the .Net framework version as the name for each path, as shown in the screenshot. Here are some typical paths for a 32 bit system.
    • .Net 4.0 - C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe
    • .Net 3.5 - C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe
    • .Net 2.0 - C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe
  • Extended E-mail Notification – Choose the format (html or plain text) and configure the subject and body you would like in the email notifications. These can be overridden by each build project.

If you plan to use the Twitter, Hudson Speaks, or Jenkins Sounds plugins, configure them here as well. As before be sure to click the Save button at the bottom of the page when you are done making changes.

Setting up the continuous integration build

Now that Jenkins has been installed and configured we are ready to setup our first project/job.

imageFrom the main Jenkins dashboard, click New Job. Enter the name you would like to use to identify this build. This name will be used in notifications so make it something meaningful. Choose Build a free-style software project from the radio options and click ok. The project configuration screen will appear.

image

Click the “Advanced” button in the Advanced Options section to expand the section. I would recommend checking the Quiet Period checkbox and giving it a value between 5 and 60 seconds. This will prevent the build from occurring for the specified time period after the build trigger (source code change) has occurred. I like this because this allows the user committing the code to commit multiple times before the build is run, which allows a small window of time to commit multiple changes with different commit comments that will be included in the same build. It also allows the coder to redeem themselves before the infamous broken build email is sent in the event that the first check in would have broken it.

Use Custom Workspace – This will allow you to change the default location from the Jenkins install directory to a folder of your choosing. The default install path on a windows machine for Jenkins is in the Program Files folder, which can make it difficult to get your build setup correctly due to the space in the path. Although you should be able to get this working by adding quotes around paths, it may be easier to simply change the workspace.

imageSource Code Management - Setup the access to your source code repository. In my case I’m using Subversion so I simply need to select Subversion from the list and enter the URL to my subversion server. If your repository does not allow anonymous read access, you will need to enter a username and password as well.

Check-out Strategy – I typically use the "Always check-out a fresh copy” as this gives you the guarantee that the workspace has a clean copy of the latest version every time the build is preformed.

Build Triggers - For this build we will use the Poll SCM option to have source code changes trigger the build. Jenkins has a robust schedule configuration. I wanted Jenkins to check for changes every 5 minutes so I set the schedule to “*/5 * * * *”. Click the question mark icon to the right of the schedule box to get more details on how to define your schedule.

imageBuild - Now it’s time to define what is being built. In the Build section click the Add build step button. We are using MSBuild project files to define the build so we will choose “Build a Visual Studio project or solution with MSBuild” from the menu that appears. The following options now appear:

  • MSBuild Version – This is a drop down list of all the paths that you configured earlier. Select the appropriate version for the project you are building. The example project was created in Visual Studio 2010 using .Net framework version 4.0.
  • MSBuild Build File – Here you will specify the path the the MSBuild file you will be using. Visual Studio project files (*.csproj for c# and *.vbproj for VB) are valid MSBuild files. Solution files (*.sln) are not. We will be using the custom MainBuild.proj file in the sample project. The path to this file is relative to the workspace folder. In my sample the MainBuild.proj file is in the root of the SVN repository so we can simply specify the filename:
    • MainBuild.proj
  • Command Line Arguments – This is where you specify which targets within the build file will be built, as well as any other MSBuild options needed. In this first build step will will want to rebuild our custom build actions (see part 1 of this post for more information). To do this will will tell MSBuild to build the RebuildBuildActions target.
    • /t:RebuildBuildActions

Now that we have our custom build actions rebuilt, we will need to build the rest of the project. Click the Add build step button again and select “Build a Visual Studio project or soluction using MSBuild.”

  • MSBuild Version – Set this to the same setting as before, .Net 4.0
  • MSBuild Build FileMainBuild.proj
  • Command Line Arguments/t:IntegrationBuild

With our continuous integration build we want our unit tests to be run to make sure that the check-in did not break any functionality. In the sample project I am using the Microsoft unit testing framework to create the unit tests. In order to run the tests I need to add another build action. Click the Add build step button and select “Execute Windows batch command” from the list. In the Command box that appears enter the following two commands. The first will delete the old results file in case it hasn’t been cleaned up by Jenkins already, and the second runs the tests and tells MSTest to create the results.trx output file.

del /f/q results.trx
mstest /testcontainer:"CI.Tests\bin\Debug\CI.Tests.dll" /resultsfile:results.trx

Post-build Actions – Now we need to tell Jenkins what we want it to do after the build has completed. For the continuous integration build we will want the results of running our unit tests to be published and we will want to send an email. Check the “Publish MSTest test result report” checkbox and enter results.trx into the “Test report TRX file” field that appears. This will tell Jenkins to interpret the test results file and it will add a trend graph to this project’s status page. To have Jenkins send an email check the E-Mail Notification checkbox.

Now that all of the settings have been configured, click the Save button at the bottom of the page.

Setting up the nightly full rebuild

If you’re not already on the dashboard click the Back to dashboard link on the left. Now click the New Job link. Like before enter a job name and select Build a free-style software project from the radio options and click ok. The project configuration screen will appear. Enter the same settings as before for the Source Code Management section.

Build Triggers – For the nightly rebuild we will be using the Build Periodically option instead of Poll SCM. Check the box and enter “0 0 * * 1-5” which will tell Jenkins to build this project every weekday at midnight.

Build – For the nightly rebuild we will rebuild the entire solution again, but this time we will skip running the tests since they were run after every code check-in.

  1. Add a build step to rebuild the custom build actions
    1. Click Add build action and select Build a Visual Studio project or solution using MSBuild.
    2. Select the correct msbuild version .Net 4.0
    3. Enter MainBuild.proj as the build file.
    4. Command Line Arguments: /t:RebuildBuildActions
  2. Add a build step to perform the main build
    1. Click Add build action and select Build a Visual Studio project or solution using MSBuild.
    2. Select the correct msbuild version .Net 4.0
    3. Enter MainBuild.proj as the build file.
    4. Command Line Arguments: /t:IncrementBuildNumber;MainBuild

Now setup email notifications and any other post build actions you would like, then click the Save button at the bottom of the page.

That’s it. You should now have a fully functional continuous integration and nightly rebuild system.

Tags:

c# | Continuous Integration | Qualtrax

Comments

Comments are closed