Setting up Sonar Cloud with Azure DevOps pipelines for .net core

Our team wanted to add code coverage and look at code quality metrics for what we’re producing. Sonar Cloud works with our pipelines to provide that — although in setting it up I referred to a lot of sources in different places to get it working as I wanted — all of which I’ve consolidated into the guide below. I hope this helps people wanting to set this up for themselves.

Setting up Sonar Cloud from scratch

  1. Log in to Sonar Cloud, pick Azure Devops from the list of providers, and use the same credentials for Azure Devops if you’re not already logged in to that account.
  2. If you are not an admin on your companies Azure Devops ‘organization’ you’ll need someone with that access to link the two together. Someone will also need to set up payment details if you’re analysing private code — open source repos are free though!
  3. While you’ve got an admin’s ear — get them to approve the Sonar Cloud app for DevOps: SonarCloud — Visual Studio Marketplace
  4. Once Sonar is set up and an organisation/permissions created — users logging in will be able to see an empty portal screen

Connecting Sonar to DevOps

  1. Create a token in security menu in Sonar (Avatar icon > My Account > Security), save it somewhere as it can’t be retrieved. You should probably be logged in with a service account to generate this token, but it’s easy to change this later.
  2. Add Sonar Cloud Service connection in DevOps (Project Settings > Service Connections) and add token from previous step. Make the name field useful as it will be referenced in a yaml pipeline later.
  3. In Sonar Cloud, click the Plus next to the avatar to add a new project. Then create project manually. Don’t add any more settings after organisation, key and name are input, and you’ve pressed the Set Up button — as these settings will be set up via. the push from Devops.

Creating yaml templates

In ADO Repo, add templates to your pipeline with the correct project name. The organisation key must match, and be in the correct case.

I created a step for preparing Sonar which must run before build, and another step to execute Sonar and analyse.

Prepare sonar step

I need the name of the Sonar Cloud service connection I set up earlier in ADO, and my organisation and project keys from Sonar. Note the last line waiting for quality gate, this will cause my pipeline to fail if the code and coverage conditions in sonar settings are not met

Execute analysis step

Calling steps from the build template

I’ve trimmed the edges of the pipeline yaml file, and there are some parameters which would need replacing, but hopefully the below example is illustrative of where the steps need to be called. Note the additional parameters added to my unit test task to ensure the correct coverage is collected for Sonar to use

Now you can run the pipeline in DevOps, this will populate the project in Sonarcloud and show metrics on the ADO completed build — in the ‘extensions’ and ‘code coverage’ tabs. You can also get more detail in SonarCloud, if you’ve analysed a branch other that master you will need to select the branch from a drop down to view the analysis

Developer, Scrum master and Agile Coach

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store