How to block the merge of Merge Requests when SonarQube Quality Gate is failed, with GitLab

This feature is available with and GitLab self hosted 12.5 and higher

With GitLab it’s possible to block the merge of a merge request if the SonarQube Quality Gate is failed on the MR (Merge Request).

To do that:

  1. First make sure that your pipeline is configured to not allow failures on the quality gate check.

In your YAML pipeline make sure you specify -Dsonar.qualitygate.wait=true and allow_failure=false in your scan.

      name: sonarsource/sonar-scanner-cli:latest
      entrypoint: [""]
      SONAR_TOKEN: "<YourToken>"
      SONAR_HOST_URL: "<YourSonarQubeUrl>"
      GIT_DEPTH: 0
      stage: test
        - sonar-scanner -Dsonar.qualitygate.wait=true
      allow_failure: false
        - merge_requests
        - master
  1. Configure the repository to allow merge only if build succeeds:

In Settings --> General, tick the box Pipelines must succeed in the Merge checks section

  1. When you analyze a merge request with GitLab-CI you’ll notice that:

a) Quality Gate failure causes the pipeline to fail

b) Since pipeline is failed, you can’t merge the merge request

See Also:

1 Like