如何在构建恢复时在Jenkins管道中发送通知?

时间:2017-03-03 11:40:50

标签: jenkins notifications jenkins-pipeline

我希望在作业恢复时从我的Jenkins管道构建发送通知。当前构建成功且最后一次构建错误(失败,中止等)时的含义。

我知道如何发送通知。我想我的问题归结为如何检查以前版本的状态,但我可能会弄错。

我试过“currentBuild.rawBuild.getPreviousBuild()?。getResult()”,但得到异常“org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:脚本不允许使用方法org.jenkinsci.plugins.workflow .support.steps.build.RunWrapper getRawBuild“。如果我关闭沙箱,它应该工作。是否可以使用沙盒?

5 个答案:

答案 0 :(得分:17)

我找到了另一个有效的解决方案,它不需要您手动跟踪构建结果。虽然,它需要使用脚本元素:(

pipeline {
  agent any
  post {
    success {
      script {
        if (currentBuild.getPreviousBuild() && 
            currentBuild.getPreviousBuild().getResult().toString() != "SUCCESS") {
          echo 'Build is back to normal!'
        }
      }
    }
  }
}

答案 1 :(得分:10)

有趣的问题。你可以在Jenkins声明性管道中使用'更改'帖子{}部分的一部分。但是您需要在作业中将currentBuild.result设置为SUCCESS或FAILURE,并在post部分中进行检查。就Jenkins而言,似乎没有一种简单的方法可以获得当前的构建状态(失败,成功等),而无需在管道中跟踪它 - 除非我错过了一些微妙的东西。下面是一个示例,您将在更改的部分中发送通知,以检查SUCCESS:

pipeline {
    agent any

    parameters {
        string(name: 'FAIL',     defaultValue: 'false', description: 'Whether to fail')
    }

    stages {
        stage('Test') {

            steps {

                script {

                    if(params.FAIL == 'true') {
                        echo "This build will fail"
                        currentBuild.result = 'FAILURE'
                        error("Build has failed")
                    }
                    else {
                        echo "This build is a success"
                        currentBuild.result = 'SUCCESS'
                    }

                }
            }
        }
    }

    post {
        always  {
            echo "Build completed. currentBuild.result = ${currentBuild.result}"
        }

        changed {
            echo 'Build result changed'

            script {
                if(currentBuild.result == 'SUCCESS') {
                    echo 'Build has changed to SUCCESS status'
                }
            }
        }

        failure {
            echo 'Build failed'
        }

        success {
            echo 'Build was a success'
        }
        unstable {
            echo 'Build has gone unstable'
        }
    }
}

- 比尔

答案 2 :(得分:3)

类似于@Kolky的答案,这将是“Scripted pipeline”的片段,你使用“node {stage1 ... stage2 ... etc}”:

    stage('Email') {
        if (currentBuild.getPreviousBuild().getResult().toString() != "SUCCESS") {
            echo 'Build is back to normal!'
            stage('Send build recovered email') {
                mail body: 'My build back to successful',
//                    from: '', replyTo: '',
                        subject: 'My build back to successful',
                        to: 'mymail@server.com'
            }

        }
    }

答案 3 :(得分:2)

或者,您可以将脚本逻辑移出管道(理想情况下 - 使Jenkins shared pipeline library可重复使用并保持管道清洁),这样您就不需要script块:

def sendNotification(buildStatus) {
  if (buildStatus != 'SUCCESS') {
    // Do nothing - only interested when status becomes GREEN
    return
  }

  mattermostSend "${env.JOB_NAME} has recovered! (<${env.BUILD_URL}|See the build>)"
}

pipeline {
  ...

  post {
    changed {
      sendNotification(currentBuild.currentResult)
    }
  }
}

答案 4 :(得分:0)

随附的文档会有所帮助:

Pipeline Ref Card

相关问题