Integrate NDepend with Jenkins
So far we don't propose a first-class integration with Jenkins but we are planning to do it.
You can vote for Jenkins integration on our User Voice.
For now, you can follow this awesome in-depth tutorial written by Richard Moss reproduced below, courtesy of the author.
Prerequisites and notes
At present, there isn't a dedicated Jenkins plugin available for NDepend, so we're slightly limited in how much integration we can do.
The first step is to call NDepend. We can do this by adding a Execute Windows batch command step to our project and then set the Command field to call NDepend.Console.exe passing in the filename of our project.
I have NDepend and other similar tools used by builds checked into SVN so that they automatically get checked out into a build agents workspace, so in my case I can use a relative path pointing to NDepend.Console.exe. Alternatively, if the location of NDepend's binaries are part of the OS path, you can omit the path completely.
Using absolute or relative paths in a NDepend project
By default, all paths and filenames inside the NDepend project are absolute. Jenkins builds take place in a temporary workspace, the location of which could be different for each agent. In such scenarios, the use of absolute paths could result in either absolute failure, or out-dated / unexpected results.
There are two ways we can work around this - the first is to use command line switches to override the paths in the project, and the second is to make them relative.
Overriding the absolute paths
The InDirs and OutDir arguments can be used to specify override paths. InDirs controls where all the source files to analyse are located and OutDir specifies where the report will be written. Note that InDirs allows you to specify multiple paths if required.
NDepend.Console.exe "%WORKSPACE%\cyotek\source\Applications\WebCopy\WebCopy.ndproj" /InDirs "%WORKSPACE%\cyotek\source\Applications\WebCopy" /OutDir "%WORKSPACE%\cyotek\source\Applications\WebCopy\NDependOut"
Configuring NDepend to use relative paths
These instructions apply to the stand alone tool, but should also work from the Visual Studio extension.
Personally, I don't like absolute paths being stored in documents, so I reconfigure my NDepend projects to use relative paths.
Failing the build
If the Execute Windows batch command returns non-zero, by default Jenkins will fail the build. When NDepend runs successfully and no Quality Gate is failing then it will return the expected zero. However, even if it has otherwise ran successfully, it will return non-zero in the event of Quality Gate failure.
Depending on your needs, you might want to disable this behaviour. For example, if you are using NDepend with an existing code base, then potentially you're going to have genuine violations to investigate, or false positives.
We can handle this either by marking the build as unstable or suppressing the error.
Marking the build as unstable
The Execute Windows batch command has an additional parameter (hidden behind the Advanced button) named ERRORLEVEL to set build unstable. By setting the value of this field to 1 (the exit code NDepend will return when critical violations are encountered), then the build result will be Unstable, but it will continue executing.
Suppressing the error
As the name of the step, Execute Windows batch command, suggests, this isn't the execution of a single command. Jenkins will create a batch file based on the contents of the Command field which it then executes. Therefore, to suppress the error we simply need to exit the batch file ourselves, allowing us to supply our own exit code.
To do this, just add exit /b 0 as a new line in the Command field and any existing error code will be ignored.
Publishing the HTML
After the NDepend analysis has completed, a HTML report will be generated. While this report doesn't offer the level of functionality that viewing the results in NDepend's GUI does, it provides a lot of useful data.
To embed this into Jenkins, we need to add a Publish HTML reports post-build step.
Viewing the report
Once your Jenkins job has been ran and completed successfully new navigation options for viewing the reports should appear in the sidebar for the job, and the status overview providing access to the NDepend report.
Using external resource files to avoid duplication
Another NDepend default is to save all the rules in the project file. These stock rules substantially increase the size of the NDepend project and if you don't modify them it doesn't really make sense to have them duplicated over and over again.
Fortunately, NDepend allows rules to be stored in external files, and so I used the NDepend GUI to create a rules file. Now each project has had all the built in rules deleted and just references the external file. Handy!
If your projects are using absolute paths, you can just the /RuleFiles and /KeepProjectRuleFiles parameters with NDepend.Console.exe for overriding the absolute versions if required.
Similarly, NDepend version 2017 introduces new settings for project debt, but also provides the ability to store these in an external file. Regretfully there doesn't seem to be override switches available in NDepend.Console.exe, but as I've yet to test the technical debt features yet I don't know if this will be an issue or not. (NDepend Team Remark: User feedback will be welcome on that)
About The Author