We have been looking migrating some of our common .NET Core libraries into new NuGet packages and have taken the chance to change our build process to use Azure DevOps Multi-stage Pipelines. Whilst doing this I hit a problem getting SonarQube analysis working, the documentation I found was a little confusing.
As part of the YAML pipeline re-design we were moving away from building Visual Studio SLN solution files, and swapping to .NET Core command line for the build and testing of .CSproj files. Historically we had used the SonarQube Build Tasks that can be found in the Azure DevOps Marketplace to control SonarQube Analysis. However, if we used these tasks in the new YAML pipeline we quickly found that the SonarQube analysis failed saying it could find no projects
##[error]No analysable projects were found. SonarQube analysis will not be performed. Check the build summary report for details.
So I next swapped to using use the SonarScanner for .NET Core, assuming the issue was down to not using .NET Core commands. This gave YAML as follows,
- task: DotNetCoreCLI@2 displayName: 'Install Sonarscanner' inputs: command: 'custom' custom: 'tool' arguments: 'install --global dotnet-sonarscanner --version 4.9.0 - task: DotNetCoreCLI@2 displayName: 'Begin Sonarscanner' inputs: command: 'custom' custom: 'sonarscanner' arguments: 'begin /key:"$(SonarQubeProjectKey)" /name:"$(SonarQubeName)" /d:sonar.host.url="$(SonarQubeUrl)" /d:sonar.login="$(SonarQubeProjectAPIKey)" /version:$(Major).$(Minor)' …. Build and test the project - task: DotNetCoreCLI@2 displayName: 'En Sonarscanner' inputs: command: 'custom' custom: 'sonarscanner' arguments: 'end /key:"$(SonarQubeProjectKey)" '
However, this gave exactly the same problem.
The solution it turns out was nothing to do with using the either of the ways to trigger SonarQube analysis, it was down to the fact that the .NET Core .CSProj file did not have a unique GUID. Historically this had not been an issue as if you trigger SonarQube analysis via a Visual Studio solution GUIDs are automatically injected. The move to building using the .NET core command line was the problem, but the fix was simple, just add a unique GUID to each CS project file.
<Project Sdk="MSBuild.Sdk.Extras"> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> <PublishRepositoryUrl>true</PublishRepositoryUrl> <EmbedUntrackedSources>true</EmbedUntrackedSources> <ProjectGuid>e2bb4d3a-879c-4472-8ddc-94b2705abcde</ProjectGuid> …
Once this was done, either way of running SonarQube worked.
After a bit of thought, I decided to stay with the same tasks I have used historically to trigger analysis. This was for a few reasons
- I can use a central Service Connector to manage credentials to access SonarQube
- The tasks manage the installation and update of the SonarQube tools on the agent
- I need to pass less parameters about due to the use of the service connector
- I can more easily include the SonarQube analysis report in the build
So my YAML now looks like this
- task: SonarSource.sonarqube.15B84CA1-B62F-4A2A-A403-89B7A063157.SonarQubePrepare@4 displayName: 'Prepare analysis on SonarQube' inputs: SonarQube: Sonarqube projectKey: '$(sonarqubeKey)' projectName: '$(sonarqubeName)' projectVersion: '$(Major).$(Minor)' extraProperties: | # Additional properties that will be passed to the scanner, # Put one key=value per line, example: # sonar.exclusions=**/*.bin sonar.dependencyCheck.reportPath=$(Build.SourcesDirectory)/dependency-check-report.xml sonar.dependencyCheck.htmlReportPath=$(Build.SourcesDirectory)/dependency-check-report.html sonar.cpd.exclusions=**/AssemblyInfo.cs,**/*.g.cs sonar.cs.vscoveragexml.reportsPaths=$(System.DefaultWorkingDirectory)/**/*.coveragexml sonar.cs.vstest.reportsPaths=$(System.DefaultWorkingDirectory)/**/*.trx … Build & Test with DotNetCoreCLI - task: SonarSource.sonarqube.6D01813A-9589-4B15-8491-8164AEB38055.SonarQubeAnalyze@4 displayName: 'Run Code Analysis' - task: SonarSource.sonarqube.291ed61f-1ee4-45d3-b1b0-bf822d9095ef.SonarQubePublish@4 displayName: 'Publish Quality Gate Result'
Even though I don’t use it in the YAML, I still found a use for the .NET Core SonarScanner commands.
We use the Developer edition of SonarQube, this understands Git branches and PR. This edition has a requirement that you must perform an analysis on the master branch before any analysis on other branches can be done. This is because the branch analysis is measured relative to the quality of the master branch. I have found the easiest way to establish this baseline, even if it is of an empty project, is to run SonarScanner from the command on my PC, just to setup the base for any PR to be measured against.