Code Metrics Trend in Jenkins with VS Code Metrics Powertool VS2013

code-metrics-vs

Prerequisites

Visual Studio Code Metrics Powertool for Visual Studio 2013: http://www.microsoft.com/en-us/download/confirmation.aspx?id=41647

Jenkins plugin Visual Studio Code Metrics Plugin: https://wiki.jenkins-ci.org/display/JENKINS/Visual+Studio+Code+Metrics+Plugin

First you need to download and install Visual Studio Metrics Power tool which will install metrics.exe in the following location: c:\Program Files (x86)\Microsoft Visual Studio 12.0\Team Tools\Static Analysis Tools\FxCop\metrics.exe.

In Jenkins instance install Visual Studio Code Metrics plugin which will enable you new options that have to be configured.

Navigate to Manage Jenkins >  Configure System > VS Code Metrics PowerTool

image

Go to build job.

Add a new build step and configure it as follows:
image

Add a new post-build step and configure it as follows:
image

Results

After you run the build job, you will start to get results.

On the job results page
image

By accessing Code Metrics option from left side you can go deep into details
image

Host your ASP.NET application in AppHarbor

app-harbor

AppHarbor is a .NET platform as a Service hosted in cloud in other words you can build your .NET solutions in cloud. Prices start from free. My example will use all free features, but if you want to go pro and keep your continuous deployment infrastructure in cloud, AppHarbor can be a good choice.

In terms of integration with code repositories, AppHarbor supports Bitbucket, Codeplex and GitHub. It’s time to learn Git, so I will use a GitHub repository.

AppHarbor allows you to add multiple add-ons into your app, starting from free but also subscriptions. Add-ons categories are: databases (including MSSQL), email, monitoring, queue (message queues).

I will be using a blank ASP.NET MVC application. Source code can be found at https://github.com/mitasoft/build-example-aspnet-mvc.

Basic app deployment

Create a new application in AppHarbor and integrate it with GitHub repository. Go to Settings, disable precompilation, choose the branch you want to build (I use master branch). In few minutes build process should be done, click Deploy (besides the Build) and then wait for the app to load. Check the recording below to see the steps in action.
Each commit in the repository will trigger an automatic build and deploy in AppHarbor. Cool. 

app-harbor-config

Run unit tests

AppHarbor at the moment of writing this post, offer support for the following testing frameworks:

  • MbUnit (2.4.0.0 – 2.4.2.175)
  • MSTest
  • NUnit (2.4.8.0, 2.5.0.0 – 2.5.3.9345 and 2.5.4.0 – 2.5.9.10348)
  • xUnit.net (1.1.0 – 1.9.1)
  • MSpec (0.5.8.0 and 0.5.12.0)

Next step is to add XUnit support to app and create few dummy tests. Run Install-Package xunit for DemoApp and create tests to see if AppHarbor will really execute them. Commit is detected by AppHarbor, build process starts and after that tests are run.

ut

SQL Server add-on

AppHarbor allows you to add SQL Server support for your app. You can get a 20 MB database for free. After you install this add-on, click on Go to SQL Server for connection details. Those details can be used in your app for running scripts or add mock data.

image

image

image

image

Scripting TFS Command line for get latest version, check-out, check-in, new branch

visual-studio-2013-logox300

Getting latest version of you code from TFS is not so easy, especially if you compare with Git. In some cases you need to be able to perform get latest command, check-ins, check-outs from a custom script and not from Jenkins plugins. Finding the right sequence of TF commands and the right combination of parameters can be very hard.

Note: For TFS Online (http://*****.visualstudio.com) you need to use Team Explorer Everywhere (TEE-CLC-12.0.2.zip) because the classic tf.exe does not support passing credentials via command line.

For on-premise TFS instance, WindowsLiveId must have the format of domain\username.

Get latest version from TFS Online or On-Premise

tf workspace
-new CodeWorkspace;WindowsLiveId
-noprompt
-server:https://***.visualstudio.com/defaultcollection
-login:WindowsLiveId,Password

tf workfold
-map $/TFS-PATH-TO-PROJECT .                  <— you need to use that dot (.)
-workspace:CodeWorkspace
-server:https://***.visualstudio.com/defaultcollection
-login:WindowsLiveId,Password

tf get
/force
/all
-recursive
-noprompt
-login:WindowsLiveId,Password

tf workfold
-unmap .                                                              <— you need to use that dot (.)
-workspace:CodeWorkspace
-login:WindowsLiveId,Password

tf workspace
/delete CodeWorkspace
/noprompt
/server:https://***.visualstudio.com/defaultcollection
/login:WindowsLiveId,Password

Check-out

tf checkout $/TFS-PATH
/login:WindowsLiveId,Password

Check-in

tf checkin $/TFS-PATH
/noprompt
/force
/comment:’***FROM_CI***’
/login:WindowsLiveId,Password

Create new branch

tf branch $/existing-branch $/new-branch
/noget
/checkin
/noprompt
/comment:’***FROM_CI***’
/login:WindowsLiveId,Password

Jenkins and TFS Online

This post is about getting the latest code from TFS hosted in cloud not on premise. It’s applicable to a TFVS repository not to a Git repository.

Getting the latest version from VisualStudio.COM (aka. Visual Studio Online) doesn’t work with the traditional TF.EXE. Reason for that is that tf.exe doesn’t support VSO login dialog. Which makes impossible to use your Windows ID credentials, unless they are already cached in your Team Explorer or Visual Studio.

This can be done at this point using Team Explorer Everywhere for Team Foundation Server. For this example I’m using version TEE-CLC-12.0.2.

Team Explorer Everywhere

Download this package into a folder on your machine.  Once you are done, you will see tf.cmd which is the replacement for tf.exe .

Add this location to your environment Path under System variables section.

image

Configure your Jenkins instance

As a prerequisite, you need to have already a Jenkins instance, up and running.

Go to Manage Jenkins > Manage Plugins. Switch to Available tab and search and install the following two plugins:

Configure Team Foundation Server Plug-in

Team Foundation Server plugin is using by default tf.exe. We need to change this configuration to point the location where you’ve unpacked Team Explorer Everywhere.

Go to Manage Jenkins > Configure System and scroll down until you find Team Foundation Server section. Update the path there.

image

Configure MSBuild plugin

This plugin is responsible for calling msbuild.exe.

Go to Manage Jenkins > Configure System and scroll down until you find MSBuild section.

image

Create Jenkins project

Navigate to Jenkins home page, choose New Item, pick Freestyle project and provide the Item name field.

image

Important: Running TFEE for the first time will ask you to accept the End User License Agreement. This command must be run from the context of your workspace per this project. Just before setting up the source control settings we need to run: tf eula –accept.

eula-accept

Accepting TF eula

Under the Build section and a new Execute Windows batch step.

image

Note: If you’ve change the environment Path property, you need to restart the Jenkins service. If you don’t, will continue to say that it doesn’t know about tf.cmd.

Save the project and run the job. You should get a result in console saying that you’ve accepted the EULA.

You can now remove previous added step, because EULA is now accepted.

Configure Source Control Management

Scrolling down in the configuration page for this project, you will get to Source Code Management section.

VERY IMPORTANT: For login name you MUST use Window Live ID, but you MUST have also alternate credentials activated – don’t use them.

Login name: your.liveid@yourdomain.com

User password: yourliveidpassword

scm-configuration

At this point everything should work as expected.

image

Adding the build step

Go back to the projects/job list and select TFVS demo. Choose to configure this project. Scroll to the Build section and add a new step called “Build a Visual Studio project or solution using MSBuild”.

image

Depending on how your file/directory structure is, you have to provide the solution path relative to workspace.

image

image

image

And the result is:

image

Feel free to contact me. I’m eager to know if this helped you or if your are having troubles in applying this steps.