Skip to content

Instantly share code, notes, and snippets.

@rmpestano
Last active April 21, 2023 13:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rmpestano/86099dec7067cb8795912df7b0e735b2 to your computer and use it in GitHub Desktop.
Save rmpestano/86099dec7067cb8795912df7b0e735b2 to your computer and use it in GitHub Desktop.
Jenkins Sonar quality gates integration via shared library
#!/usr/bin/env groovy
/**
* Fails the pipeline if project quality doesn't meet the configured quality profile (see https://docs.sonarqube.org/display/SONAR/Quality+Gates AND https://docs.sonarqube.org/display/SONAR/Quality+Profiles).
* This DSL function must be invoked after the quality analisys has been published. Example:
*
* stage('sonar') {
* steps {
* withSonarQubeEnv('sonar') { //there must be a tool named 'sonar' configured on Jenkins (/jenkins/configureTools)
* sh 'mvn sonar:sonar' //publish project quality on sonar
* }
* }
* }
*
* stage('quality-gates') {
* steps {
* qualityGates() //this stage will fail if project quality doesn't meet its quality profile
* }
* }
*
*/
def call() {
timeout(time: 3, unit: 'MINUTES') {
echo "Initializing quality gates..."
sh 'sleep 10' //small delay because project quality can still being published on previous stage (specially on bigger projects).
def result = waitForQualityGate() //this is enabled by quality gates plugin: https://wiki.jenkins.io/display/JENKINS/Quality+Gates+Plugin
if (result.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${result.status}"
} else {
echo "Quality gate passed with result: ${result.status}"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment