繁体   English   中英

如果声纳质量门失败,则将构建设置为不稳定

[英]Set the build unstable if sonar Quality Gate is failed

我有一个非常简单的管道。 一切都在我的 pom.xml 文件和 .m2/settings.xml 中定义。 当 SonarQube 的质量门失败时,我想在 Jenkins 上将我的构建设置为不稳定。 这是我所做的,但我有几个错误,如“预期的}”。 有谁知道它是如何工作的? 请注意,环境部分是可选的。 谢谢你。

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"                     
                    }                 
                }  
            }
        }          
    }         
}

您需要将 qualitygate 内容和所有内容包装在一个script块中,如下所示:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM