NDepend & Appveyor
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.

Create an NDepend project
To do anything with NDepend we need at create an NDepend project. The way to get this done is to:
- Open your solution in Visual Studio.
- Build your solution to produce assemblies.
- Select “Attach New NDepend project to Current VS Solution” from the NDepend menu.
- Click “Add Assemblies of VS Solution(s)”.
- Select your solution.
- Click “Analyze a single .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.
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.
- 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 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.
