Integrate NDepend with AppVeyor

So far we don't propose a first-class integration with AppVeyor but we are planning to do it.

You can vote for AppVeyor integration on our User Voice.

For now, you can follow this awesome in-depth tutorial written by Jimmy Pelletier reproduced below, courtesy of the author.

In this documentation 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# or VB.NET project of some kind checked into GitHub.
  • A Build Machine license file. To obtain such file contact us at support@ndepend.com. If you are in trial period let us know your company name and postal address in the email. Else if you own a Build Machine license key, let us know your license key in the email.

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 your 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. Then copy its content into a tools directory. I put mine in ./tools/NDepend. You will also need to put your Build Machine license file in here.

ndepend appveyor integration

Create an NDepend project

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

  • Install NDepend in Visual Studio with NDepend.VisualStudiExtensionInstaller.exe.
  • Open your solution in Visual Studio.
  • Build your solution to produce assemblies.
  • Select “Attach New NDepend project to Current Visual Studio Solution” from the NDepend menu.
  • Click “Add Assemblies of Visual Studio Solution(s)”.
  • Select your solution.
  • Click “Analyze # .NET assembly”.
  • Select “Edit Project” from the “Project” menu in the “NDepend” menu.
  • Select “Paths Referenced” on the right hand side.
  • Select each path listed and Right click to select “Set as Path Relative (to the NDepend Project File Location)”.
  • Save your changes.
An NDepend Developer license is needed to do these steps. If you only have BuildMachine licensing you can still create a NDepend project from a Visual Studio solution from VisualNDepend.exe Start Page > New NDepend project, and then populate the list of assemblies to analyze. In such situation you'll still need to set paths referenced as relative to the NDepend Project File Location, as explained above.

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 but the one I’ll include here is pretty self explanatory and is correct at the time of writing (September 2018). 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. Notice that here our NDepend project file is named SomeProject.ndproj.

image: Visual Studio 2017
before_build:
  - ps: nuget restore
build:
  project: SomeProject.sln
after_build:
  - ps: $ndproj = Resolve-Path .\SomeProject.ndproj
  - ps: .tools\NDepend\NDepend.Console.exe $ndproj

Setting up AppVeyor and our first build

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

  • Log in or sign up to AppVeyor using your GitHub account.
  • Click “New Project” from the home screen.
  • Add your project from the list of repos available from your GitHub account.
  • Click “New Build”.

Failing the build

NDepend will now fail on any Quality Gate failure. AppVeyor terminates a build upon a command that returns a non-zero exit code. At least one Quality Gate failing means that NDepend.Console.exe will return a non-zero exit code as explained in Quality Gates and Build Failure.

Thus, the easiest way to test build failure is to modify the “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 Quality Gate CQLinq query should now look like this:

// <QualityGate Name="Critical Issues" Unit="issues" />
failif count > 1 issues
warnif count > 0 issues
from i in Issues
select 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.

About The Author

Jimmy Pelletier

Jimmy Pelletier

Jim has been talking about microservices since back before they got that small and event driven architectures before we had a cloud to build them on. Organiser of the Melbourne ALT.NET user group and part of the DDD Melbourne conference crew. He's now spending his time building and enabling autonomous teams at Page Up as well as helping build Melbourne's development community.