【问题标题】:How to get same Mailer behaviour for Jenkins pipeline如何为 Jenkins 管道获得相同的 Mailer 行为
【发布时间】:2017-05-29 14:33:01
【问题描述】:

我开始使用 Jenkins 声明式管道。现在我想拥有与 Mailer 插件的 Usage 中定义的相同的电子邮件通知行为:

  1. 每次构建失败都会触发一封新电子邮件。
  2. 在失败(或不稳定)构建后成功构建会触发一封新电子邮件,表明危机已经结束。
  3. 成功构建后的不稳定构建会触发新电子邮件,表明存在回归。
  4. 除非已配置,否则每个不稳定的构建都会触发一封新电子邮件,表明回归仍然存在。

我读到了Notifications in Pipelines,但它没有根据上述规则进行通知。此外,它不包含消息正文中的部分控制台输出,以防构建失败。

有人知道如何在声明式管道中执行此操作吗?

【问题讨论】:

    标签: jenkins jenkins-pipeline


    【解决方案1】:

    通过以下代码,您可以在帖子部分使用邮件程序插件。这提供了预期的行为:

    pipeline {
      agent any
      stages {
          stage('test') {
            steps {
              script {
                  // change to 'UNSTABLE' OR 'FAILED' to test the behaviour 
                  currentBuild.result = 'SUCCESS'
              }
            }
          }
      }
      post {
            always {
              step([$class: 'Mailer',
                notifyEveryUnstableBuild: true,
                recipients: "test@test.com",
                sendToIndividuals: true])
            }
      }
    }
    

    【讨论】:

    • 在管道的开头设置currentBuild.result = 'SUCCESS'很重要,否则邮件发不出去,见Mailer inside Pipeline
    • 你是对的,currentBuild.result 不会在管道的 post 部分自动设置。在工作的最后设置 currentBuild.result = 'SUCCESS' 可能会更好。例如。在帖子部分的开头。在stackoverflow.com/questions/44329032/… 中查看我的回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 2021-11-24
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    相关资源
    最近更新 更多