I am new to groovy
. I am using Jenkins scripted
pipeline. It’s a gradle
project. We are using the library in our Jenkinsfile. Basically, I want to fail the pipeline based on Sonar results. Below is the library code that I import in my Jenkinsfile. By default, stopOnFailure:false
. I should make it as true
to fail the pipeline. How do I pass this argument in my Jenkinsfile.
Note: I am not allowed to make any changes to my library
Filename: sonarqube.groovy
def scan(Map args=[stopOnFailure:false, waitForCompletion:true], Closure closure) {
closure.resolveStrategy = Closure.DELEGATE_FIRST
withEnv(["JAVA_HOME=${tool 'JDK 11'}"]) {
withEnv(["PATH+SONARSCANNER=${tool 'SonarQube Scanner'}/bin"]) {
withSonarQubeEnv('CICD Sonarqube') {
closure()
env.SONARQUBE_URL = env.SONAR_HOST_URL
}
if (args.waitForCompletion == true) {
sleep 5
timeout(time: 1, unit: 'HOURS') { // Just in case something goes wrong, pipeline will be killed after a timeout
def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv
if ((args.stopOnFailure == true) && (qg.status != 'OK')) {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
}
}
}
My Jenkinsfile.groovy
(scripted pipeline)
stage('CodeQuality') {
withEnv(["SONAR_USER_HOME=${WORKSPACE}/.sonar"]) {
withSonarQubeEnv {
sh "./gradlew sonarqube"
}
}
}
4