Use pull request decoration in Azure DevOps with SonarCloud

Some time ago I wrote about how to fail your Azure Pipeline, if your SonarQube quality gate fails. I often get the request how you can do the same thing with SonarCloud. But it’s much better to use pull request decoration. This is really an awesome feature. You get the SonarQube Errors directly in the pull request and the quality gate is evaluated.

The configuration is a bit tricky and the options are pretty hard to find. And this only works in SonarCloud or with the SonarQube Developer Edition! I switched completely to SonarCloud – this is much more convenient.

I asume that you already have a build with the prepare, run and publish tasks configured. Note that there is a different extension for SonarCloud then for SonarQube. Add and configure the tasks accordingly and run the build with the analysis.

Now got to the project in SonarCloud. Go to “Generell Settings”, “Pull Requests”. Select VSTS and enter a Personal Access Token for Azure DevOps that SonarCloud uses to connect to Azure DevOps.

Now run the build again. If the analysis is complete got the the branch policy in your Azure Repo. If you now add a new Status Policy you will find in the drop down a policy called “SonarCloud/quality gate”. Select it and save the policy.

That’s it! Now create a pull request and enjoy the SonarCloud decoration. The Status Policy will fail the PR if the SonarCloud Quality Gate fails.


  1. If I update the code , should I have to build it again before raising the PR? If not, then how will Sonar know the static code analysis of the newly introduced code?

    1. You have to run the build that performs the analysis once on *master*, after you configure you PAT for Azure DevOps in SonarQube. After that the PR automatically triggers the build and runs the branch analysis. After the merge to master you should perform again a analysis in master. SonarQube is aware of the branches the analysis is preformed on.
      Does this answer your question?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s