如果声纳质量门失败,则将构建设置为不稳定 [英] Set the build unstable if sonar Quality Gate is failed

查看:229
本文介绍了如果声纳质量门失败,则将构建设置为不稳定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的管道.一切都在我的pom.xml文件和.m2/settings.xml中定义.当SonarQube的Quality Gate失败时,我想在Jenkins上将构建设置为不稳定.这是我所做的,但是我遇到了一些错误,例如"expected}".有谁知道它是如何工作的? 请注意,环境部分是可选的. 谢谢.

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

推荐答案

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

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

这篇关于如果声纳质量门失败,则将构建设置为不稳定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆