Sunday, August 25, 2013

Hudson as Continuous System Integration tool for Dot Net projects.



Hudson is a JAVA web application that allows you to monitor your build process. Hudson is extendable via plug-ins.( Official Hudson Website.)Hudson can be used in an environment where we have to maintain many applications/jobs, for example, multiple applications which generate reports in csv, tsv, txt etc formats whose data and format integrity is quite essential, and thus their Unit Test must be verified with every major-minor change in a common component or with every check-in to the version control tools like SVN/TFS.

Hudson can also be used for automated versioning of assemblies/dlls which get version with every build.

Installing The Hudson

The only pre-requisite is that you have Java installed. Here are the links to the files:
o    Java
o    Hudson
Once installed you can start Hudson by doing the following:

Open up a console window (Command Prompt), navigate to the folder containing the ‘Hudson. War’ file and issuing the following command:

java -DHUDSON_HOME=c:Hudson -jar hudson.war


Hudson is now running and can be accessed via your web browser at http://localhost:8080/

 To install Hudson as a Windows service use the “Install as Windows Service” option on the “Manage Hudson” page and follow the instructions.
Configuring Hudson
Configuration of Hudson is done using the “Manage Plugins” and “Configure System” options on the Manage Hudson page. First visit the “Manage Plugins” option and click on the “Available” tab to display a list of plug-ins that can be installed.
Select the following Hudson plug-ins and click ‘Install’:
o    MSBuild Plugin – This plugin allows you to use MDBuild to build .NET projects.
o    MSTest Plugin – This plugin converts MSTest TRX file metadata into a format that it can be integrated into the Hudson’s JUnit features. This will make the test reports available as build output.

After the install, be sure to restart your Hudson service.

To configure the MSBuild plugin. Select the “Configure System” option and look for the “MSBuild Builder” section. Configure it to look like the following and click the ‘Save’ button
Notice that you can have multiple MSBuild configurations. This might be handy for building projects using the different versions of the .NET Framework.

Building a Job

Build jobs are added via the “New Job” option on the Hudson main page. Select a name and the “Build a free-style software project” option as shown below. Then click “OK”.
Now a ‘job’ will appear in the main Hudson page.
To manage the new project, click the link for the project you just created click the ‘Configure’ link in the list on the left. First select the “Discard Old Builds” option and configure the policy for limiting disk consumption.

Then configure your “Source Code Management” options. Notice the period in the “Local module directory (optional)” field. That will force Hudson to check out the project directly into the workspace rather than into a subdirectory.

To setup the build select the “Build a Visual Studio project of solution using MSBuild” option from the “Add build step” drop down list. After selecting the “MS Build Version” that we configured before and adding other configuration information.
This will get the solution built. Now we want Hudson to execute the unit tests for the project. We can add a “Windows batch command” to call MSTest.

Finally, select the “Publish MSTest test result report” from the “Post Build Actions” section and assign a name e.g.: TestResults.trx. This will take the ‘trx’ test results metadata file (XML), you can transform it into HTML and make it available as part of the build output.

Click “Save” at the bottom and the project should be ready to build manually. Restart Hudson for the configuration to take effect.
Manual Build
On the main Hudson page you will see a list of all the jobs. On the right-hand side there is a button that allows you to schedule a job. Clicking that button will start the build process. Hudson also shows you a progress bar for your build.


Continuous System Integration

To kick off a build we can also navigate to the following URL:
http://HOSTNAME/job/PROJECTNAME/build
where HOSTNAME is your server / port and PROJECTNAME is the project name. You can get these parameters by looking at the URL in Hudson when your project is selected. This knowledge is key to having the source control system trigger Hudson to build a project.
Having Hudson perform builds based upon changes in the source code, requires that we install a post commit hook into the source control repository. This will vary depending upon your source control system. For Visual-SVN, I add a hook by right clicking on the repository and selecting the “All Tasks” – “Manage Hooks…” option, then configure a post commit hook to execute a Ruby script.
 The Ruby script takes an argument for the project name. Here is the Ruby script:


# Push a notice to the hudson server to initiate a build.

# Ensure the required libs are present
require "net/http"
require "uri"

# Get the project name
hudsonProject = ARGV[0]

# Create the uri and issue the request
uri = URI.parse("http://hppav1:8080/job/" + hudsonProject + "/build/")
Net::HTTP::get_print uri
Now whenever code is checked in, Hudson will be triggered to build and run the tests for the project.
The next step may be to setup email or IM notification of build results.


No comments: