如何发送“回到正常”詹金斯宣告管道通知? [英] How to send "back to normal" notifications in Jenkins Declarative Pipeline?

查看:153
本文介绍了如何发送“回到正常”詹金斯宣告管道通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将现有的Jenkins管道转换为新的声明性管道,我想知道如何正确处理邮件通知?

I'm trying to convert an existing Jenkins Pipeline to the new Declarative Pipeline and I was wondering how to handle correctly mail notifications ?

我正在使用以下代码:

node {
   try {

      ...

      currentBuild.result = 'SUCCESS'
   } catch (any) {
       currentBuild.result = 'FAILURE'
       throw any
   } finally {
       step([$class: 'Mailer',
           notifyEveryUnstableBuild: true,
           recipients: "baptiste.wicht@gmail.com",
           sendToIndividuals: true])
   }
}

它工作得很好,但是我没有看到如何使用新的声明性语法。我认为通过使用post()和不同的通知可以做一些事情,但我不知道如何。我已经尝试过:

It works well, but I don't see how to use the new declarative syntax for this. I think something could be done by using post() and the different notifications, but I don't know exactly how. I've tried this:

post {
    always {
        step([$class: 'Mailer',
            notifyEveryUnstableBuild: true,
            recipients: "baptiste.wicht@gmail.com",
            sendToIndividuals: true])
    }
}

但问题是它不发送任何回到正常的邮件。

But the problem is that it does not send any "Back to normal" mail.

如何在Jenkins声明性管道中使用Mailer插件,以便发送回到正常邮件?

How can I used the Mailer plugin in a Jenkins declarative pipeline in order to send "Back to normal" mails ?

应该再次使用所有声明式语法的try / catch?

Should use again a try/catch around all declarative syntax ?

推荐答案

问题是在声明性的post部分中,currentBuild.result没有设置为SUCCESS。 FAILURE和ABORTED已设置。所以这里的行为似乎与目前不一致。

Problem is that in the post section of declarative the currentBuild.result is not set to SUCCESS. FAILURE and ABORTED is set though. So the behaviour here seems to be inconsistent at the moment.

我已经从如何获得与Jenkins管道相同的Mailer行为以更好地处理这种情况:

I've improved my answer from How to get same Mailer behaviour for Jenkins pipeline to handle this case better:

pipeline {
  agent any
  stages {
      stage('test') {
        steps {
            echo 'some steps'        
            // error("Throw exception for testing purpose..")
        }
      }
  }
  post {
      always {
          script {
              if (currentBuild.result == null) {
                  currentBuild.result = 'SUCCESS'    
              }
          }    
          step([$class: 'Mailer',
            notifyEveryUnstableBuild: true,
            recipients: "test@test.com",
            sendToIndividuals: true])
      }
  }
}

这篇关于如何发送“回到正常”詹金斯宣告管道通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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