I was recently asked how to get the code analysis and code metrics info from visual studio into an Azure Pipeline build. My first thought was, how hard could it be? Sure I’ll write a blog post about it. Lol… turns out, it was a little harder than I expected.
Let’s dive in and I’ll explore exactly what you need to do to enable both code analysis and code metrics in an Azure Pipeline build.
Enabling Code Analysis
Static Code Analysis
Enabling static code analysis is easy enough. First set up the rules you want to follow in VS, then in your Visual Studio Build task, under MSBuild Arguments, add /p:RunCodeAnalysis=true
If you are a yaml person, the yaml for the Visual Studio Build Task looks like this:
– task: VSBuild@1
displayName: ‘Build solution **\*.sln’
msbuildArgs: ‘/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation=”$(build.stagingDirectory)\drop” /p:RunCodeAnalysis=true’
Adding static code analysis is easy peasy. However, static code analysis is being deprecated and the newer roslyn based FXCop analyzers are the way to go moving forward. To see how to migrate from static code analysis to FxCop based code analysis, check this out. So how do we enable using roslyn based analyzers in our build?
Really, there’s nothing to this one. Add a dynamic code analyzer to your solution (usually through a nuget package, for my demo I added CodeCracker.CSharp). Check your code in, kick off build and…. bam! Dynamic code analyzers just run and report their results to the build report!
Generating Code Metrics Data
Ok, so getting code analysis into the build was easy enough. What about code metrics? I was hoping there would be a prebuilt task that just did this. Unfortunately, I couldn’t find one in the market place. However, how hard could it be to create one? Right? Let’s find out.
After some digging, it turns out what I need to do is documented here. To run code metrics, all I need to do is add the nuget package Microsoft.CodeAnalysis.Metrics to the projects I want to gather code metric info. And then, I just need to run msbuild /t:Metrics against those specific projects. Running MSBuild in Azure Pipelines is as simple as running a VS Build task. So I updated my build to look like this:
I added VS Build Tasks pointed to the csproj files that I want to run code metrics. And for the MSBuild Arguments, I added
where I also add the name of the output code metrics file it should generate and the directory to save the file. I added the file to $(Build.ArtifactStagingDirctory) so that the code metrics output file would be part of the downloadable build artifacts.
Running a build… this is what I get
Drilling into the getting code metrics task, it looks like this
Notice it writes the output file to my staging directory. And now, my build report looks like this:
Wait a minute… I don’t see anything in my build summary talking about code metrics. Which makes sense, we didn’t publish any of code metrics information back to the build summary. However, we did add the code metrics outputfiles to the build artifacts. So looking at our drop
You can see the two projects we’re gathering code metrics for now have Metrics.xml files as part of the build artifacts.
Opening up a metrics.xml file, the format looks like this:
Ok, so this is cool. We have code complexity information generated as part of our build. But I would really like to be able to view this directly in my build summary. Is there a way to do that?
Looking through the marketplace, I couldn’t find any task that would let me publish something to the build summary report. So in order to do that, I’ll have to create a custom task that will parse and then publish my code complexity information to the build summary tab.
By following the above steps, it’s pretty simple to generate both code analysis and code complexity information during my build. Code analysis is automatically posted to the build summary while there is no out of the box way to publish code complexity information onto the build summary page.
However, it is totally possible to create a custom task that will publish the code complexity information onto the build summary, or even onto another tab as part of the build report. I’ll go ahead and create a rudimentary task that does this for demo purposes. It won’t be very pretty but it will be functional. And i’ll blog all the steps for that in Part 2 of this blog.
For more information on code analysis, check out here
For more information on converting static code analysis to roslyn based ones, check out here
For more information on code metrics
For more information on generating code metrics from the command line