簡體   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