Skip to content

Instantly share code, notes, and snippets.

@elonmusk408
Forked from rmpestano/qualityGates.groovy
Created February 25, 2021 10:44
Show Gist options
  • Select an option

  • Save elonmusk408/c73d2bc2e67bb9e6d756e3343b71e675 to your computer and use it in GitHub Desktop.

Select an option

Save elonmusk408/c73d2bc2e67bb9e6d756e3343b71e675 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