NDepend & Appveyor

Jim Pelletier
Sep 3, 2018 · 3 min read

Build quality as part of continuous integration

In this article I provide step by step instructions on how to integrate NDepend into your CI pipeline for quality control and historical analytics. For quality control I will demonstrate how to fail your build when violating an NDepend rule and for historical analytics we will publish the NDepend report as a build artifact.

Prerequisites & Assumptions

In order to follow along with the steps provided in this article there are a few prerequisites. You will need:

  • A C# project of some kind checked into GitHub.
  • An NDepend build server license file.

I have also assumed you know how to work with .NET, Visual Studio and Git.

Making the tools available during the build

In order to run NDepend as part of you build pipeline you will need to make the NDepend.Console.exe available to run during the build. When working with a cloud build agent like Appveyor, options for installing tools and setting up the environment are limited. We’ll simply be checking NDepend into source control. First download the zip file from https://www.ndepend.com/download. Then copy its contents into a tools directory. I put mine in ./tools/NDepend. You will also need to put your build server license file in here.

Directory listing of ./tools/NDepend

Create an NDepend project

To do anything with NDepend we need at create an NDepend project. The way to get this done is to:

  1. Open your solution in Visual Studio.
  2. Build your solution to produce assemblies.
  3. Select “Attach New NDepend project to Current VS Solution” from the NDepend menu.
  4. Click “Add Assemblies of VS Solution(s)”.
  5. Select your solution.
  6. Click “Analyze a single .NET assembly”.
  7. Select “Edit Project” from the “Project” menu in the “NDepend” menu.
  8. Select “Paths Referenced” on the right hand side.
  9. Select each path listed and Right click to select “Set as Path Relative (to the NDepend Project File Location)”.
  10. Save your changes.

Create your build script

Next we need to add a build script to tell AppVeyor how to build your project. Full details of how Appveyor build scripts work can be found in the Appveyor documentation https://www.appveyor.com/docs/build-configuration/ but the one I’ll include here is pretty self explanatory and is correct at the time of writing. The file is placed in the root of the repository and is called appveyor.yaml. The interesting thing to note is that we need to resolve the full absolute path of the NDepend project file to pass in to NDepend.

image: Visual Studio 2017before_build:  - ps: nuget restorebuild:  project: SomeProject.slnafter_build:  - ps: $ndproj = Resolve-Path .\SomeProject.ndproj  - ps: .tools\NDepend\NDepend.Console.exe $ndproj

At this stage we are ready to check this code in to github and push.

Setting up AppVeyor and our first build

Next we need to set up our GitHub repo to build in AppVeyor.

  1. Log in or sign up to Appveyor using your GitHub account.
  2. Click “New Project” from the home screen.
  3. Add your project from the list of repos available from your GitHub account.
  4. Click “New Build”.

Failing the build

NDepend will return a non zero exit code on Quality Gate failure. This will in turn trigger Appveyor to mark the build as failed. The easiest way to test this is to modify your “Critical Issues” quality gate. First remove the predicate that restricts this rule to considering only critical issues. Then reduce the failif threshold so it will fail on a single issue. With the default rule set and nearly any solution this should be enough to trip the quality gate. Your rule should now look like this:

// <QualityGate Name="Critical Issues" Unit="issues" />failif count > 1 issueswarnif count > 0 issuesfrom i in Issuesselect new { i, i.Severity, i.Debt, i.AnnualInterest }

Publishing the NDepend report

To publish the NDepend report we need a bunch of files the NDepend writes to ./NDependOut during the build process. Appveyor allows us to zip these files up and present them for download. Add the following to the end of your appveyor.yaml file:

artifacts:  - path: NDependOut    name: NDependOut    type: zip

Then download the zip file from the “Artifacts” tab on a completed build and extract it to view the html report.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade