Set the build unstable if sonar Quality Gate is failed

守給你的承諾、 提交于 2021-02-11 14:44:21

问题


I have a very simple pipeline. Everything is defined in my pom.xml files and .m2/settings.xml. I want to set my build as instable on Jenkins when SonarQube's Quality Gate is failed. Here is what I did but I have several errors like "expected }". Does anyone know how it works ? Note that the environment part is optional. Thank you.

pipeline {

    agent {
        label "master"
    }

    tools {
        // Note: this should match with the tool name configured in your jenkins instance (JENKINS_URL/configureTools/)
        maven "Maven 3.6.0"
        jdk 'Java 1.8'
    }

    environment {
        // This can be nexus3 or nexus2
        NEXUS_VERSION = "nexus3"
        // This can be http or https
        NEXUS_PROTOCOL = "http"
        // Where your Nexus is running
        NEXUS_URL = "192.168.1.8:8081"
        // Repository where we will upload the artifact
        NEXUS_REPOSITORY = "repository-example"
        // Jenkins credential id to authenticate to Nexus OSS
        NEXUS_CREDENTIAL_ID = "nexus-credentials"
    }

    stages {

        stage ('Initialize') {
            steps {
                sh '''
                echo "PATH = ${PATH}"
                echo "M2_HOME = ${M2_HOME}"
                '''
            }
        }

        stage("mvn clean deploy") {
            steps {
                script {
                    // If you are using Windows then you should use "bat" step
                    // Since unit testing is out of the scope we skip them
                    sh "mvn -B clean deploy"
                }
            }
        }

        stage ("SonarQube check") {
            steps {
                script {
                    sh 'mvn -B sonar:sonar'
                }

                step {                     
                    qualitygate = waitForQualityGate()                     
                    if (qualitygate.status != "OK") {                         
                        currentBuild.result = "UNSTABLE"                     
                    }                 
                }  
            }
        }          
    }         
}

回答1:


You need to wrap the qualitygate stuff and all inside a script block as shown below:

stage ("SonarQube check") {
    steps {
        script {
            sh 'mvn -B sonar:sonar'

            qualitygate = waitForQualityGate()                     
            if (qualitygate.status != "OK") {                         
                currentBuild.result = "UNSTABLE"                     
            }                 
        }
    }
}


来源:https://stackoverflow.com/questions/61225468/set-the-build-unstable-if-sonar-quality-gate-is-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!