Blocking the merge of Pull Requests on failed Quality Gate, with Azure DevOps

With Azure DevOps it’s possible to block the merge of a pull request if the SonarQube Quality Gate is failed on the PR (Pull Request).

This feature is available with Azure DevOps Server 2019 as well as TFS 2015 and higher. For Azure Devops Services (or cloud) as well as some recent Azure Devops Server versions(late 2019 & 2020), the configuration relying on branch policies and status checks is described here.

This is called a Build policy in Azure DevOps, and can be defined per target branch, ie you can select which branches can accept pull request merges only if Quality Gate is passed. These branches are called protected branches

To do that:

  1. First Select the branch you want to protect with the passed Quality Gate build policy.
    Navigate to branches, and on the branch you want to protect, click on the vertical […] icon on the right.
    Select the Branch policies menu

  2. In the Branch policies screen click on Add build policy or Edit and existing build policy

  3. In the Edit build policy screen, select le Policy requirement Build must succeed in order to complete pull requests, and save

Once this is done, you’ll notice that any pull request to be merged on the protected branch can’t be merge if the Quality Gate on the Pull Request is Failed

See also:

That’s it folks !