Jenkins STAGE_NAME环境变量为null

时间:2018-10-17 11:46:03

标签: jenkins groovy jenkins-pipeline

在我的管道中,我正在通过slackSend向Slack发送消息。但是,当我添加环境变量env.STAGE_NAME时,它返回null。我也尝试过:${STAGE_NAME}${env.STAGE_NAME},但是它们会引发错误

关于这种情况为什么发生以及如何解决的任何想法?

先谢谢了。

更新:添加了代码

node{
    try {
    // send build started notification
        stage("Build start notification"){
            slackSend (color: '#FFFF00', message: "STARTED: Job '${env.STAGE_NAME} ${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
        }
    //reference the sonar tool and start the analysis of the proxy
        stage("SonarQube analysis") {
        withSonarQubeEnv('sonarqube'){
                def sonarScanner = tool name: 'scanner', type: 'hudson.plugins.sonar.MsBuildSQRunnerInstallation'
                bat "${sonarScanner}/bin/sonar-scanner -Dsonar.projectKey=test -Dsonar.sources=apiproxy"
          }
        }
    //wait for webhook from sonarqube. Break build if qyalitygate not ok.
        stage("Quality Gate"){
            timeout(time: 10, unit: 'SECONDS') {
                def qg = waitForQualityGate()
                if (qg.status != 'OK') {
                    error "Pipeline aborted due to quality gate failure: ${qg.status}"
                    slackSend (color: '#FF0000', message: "QualityGate error '${qg.status}")
                }
            }
        }
    //Deploy the apiproxy
        stage ('Build & deploy') {
            withMaven(maven : 'maven_3_5_4'){
                def maven = tool name: 'maven_3_5_4', type: 'maven'
                bat 'mvn clean install -Ptest'
            }
        }
    }
    catch (e){
            slackSend (color: '#FF0000', message: "Failure '${env.STAGE_NAME} ${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
    }
    finally {
    // send build finished notification 
        slackSend (color: '#00FF00', message: """FINISHED: Job "${env.STAGE_NAME} ${env.JOB_NAME} [${env.BUILD_NUMBER}]" (${env.BUILD_URL})""")
    }
}

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

finally {
// send build finished notification 
  slackSend (color: '#00FF00', message: """FINISHED: Job "${env.STAGE_NAME} ${env.JOB_NAME} [${env.BUILD_NUMBER}]" (${env.BUILD_URL})""")
}

据我所见,您遇到了报价问题和变量插值问题。