How do you integrate SonarQube into a CI pipeline and make it block on gate failure?
IntermediateConceptTesting and Quality Gates
Answer
Run the scanner with the server URL and a token, then wait for the gate result. For Maven: mvn sonar:sonar. For generic scanning use sonar-scanner with -Dsonar.qualitygate.wait=true, which makes the scanner block and exit non-zero when the gate fails. In Jenkins, waitForQualityGate abortPipeline: true after withSonarQubeEnv does the same via a webhook.
sonar-scanner \
-Dsonar.projectKey=shop-api \
-Dsonar.host.url=https://sonar.example.com \
-Dsonar.token=$SONAR_TOKEN \
-Dsonar.qualitygate.wait=true
More on Testing and Quality Gates
What is the testing pyramid?BeginnerWhat is the difference between unit, integration, and end-to-end tests?BeginnerWhat are smoke tests and sanity tests in a deployment pipeline?IntermediateWhat is code coverage and is a high percentage a good target?IntermediateWhat is a quality gate in SonarQube?IntermediateWhat does SonarQube's "Clean as You Code" and new code period mean?Advanced