Email “如何发送”;“恢复正常”;Jenkins声明性管道中的通知?

Email “如何发送”;“恢复正常”;Jenkins声明性管道中的通知?,email,jenkins,jenkins-pipeline,Email,Jenkins,Jenkins Pipeline,我正在尝试将现有的Jenkins管道转换为新的声明性管道,我想知道如何正确处理邮件通知 我目前正在使用以下代码: node { try { ... currentBuild.result = 'SUCCESS' } catch (any) { currentBuild.result = 'FAILURE' throw any } finally { step([$class: 'Mailer',

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

我目前正在使用以下代码:

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()和不同的通知可以做一些事情,但我不知道具体如何做。我试过这个:

post {
    always {
        step([$class: 'Mailer',
            notifyEveryUnstableBuild: true,
            recipients: "baptiste.wicht@gmail.com",
            sendToIndividuals: true])
    }
}
但问题是它不会发送任何“恢复正常”的邮件

我如何在Jenkins声明性管道中使用Mailer插件来发送“恢复正常”的邮件


是否应该在所有声明性语法中再次使用try/catch

您可以像全明星一样检查以前的版本:

pipeline {
  agent { label 'docker' }
  stages {
    stage ('build') {
      steps {
        sh 'ls'
      }
    }
  }
  post {
    always {
      script {
        if (currentBuild.result == null || currentBuild.result == 'SUCCESS') {
          if (currentBuild.previousBuild != null && currentBuild.previousBuild.result != 'SUCCESS') {
            echo 'send your back to normal email here, maybe something like this, your mileage may vary'
            emailext (
              subject: "Back to normal: ${currentBuild.fullDisplayName}",
              body: "Project is back to <blink>normal</blink>",
              mimeType: 'text/html',
              recipientProviders: [[$class: 'RequesterRecipientProvider'], [$class: 'DevelopersRecipientProvider']]
            )
          }
        }
      }
    }
  }
}
管道{
代理{label'docker'}
舞台{
阶段(‘构建’){
台阶{
嘘
}
}
}
职位{
总是{
剧本{
if(currentBuild.result==null | | currentBuild.result==SUCCESS){
如果(currentBuild.previousBuild!=null&¤tBuild.previousBuild.result!=“成功”){
echo“在此处将您的邮件发送回普通邮件,可能类似于此,您的里程可能会有所不同”
电子邮件文本(
主题:“返回正常:${currentBuild.fullDisplayName}”,
正文:“项目恢复正常”,
mimeType:'text/html',
recipientProviders:[[$class:'RequestErrorRecipientProvider'],[$class:'developerRecipientProvider']]
)
}
}
}
}
}
}

问题在于,在声明的post部分中,currentBuild.result未设置为SUCCESS。但设置了失败和中止。因此,目前这里的行为似乎不一致

为了更好地处理这个案件,我改进了我的答案:

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])
      }
  }
}

生成失败时发送邮件。当构建成功时,您将检查上一个构建是否成功。如果没有,您将发送一封邮件告诉用户它再次工作

    post {

        failure {
            mail to: 'user@mail.com',
            subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
            body: "Build failed: ${env.BUILD_URL}"
        }

        success {
            if (currentBuild.previousBuild != null && currentBuild.previousBuild.result != 'SUCCESS') {
                mail to: 'user@mail.com',
                subject: "Pipeline Success: ${currentBuild.fullDisplayName}",
                body: "Build is back to normal (success): ${env.BUILD_URL}"     
            }           
        }
    }

现在,通过使用
fixed
post条件()可以简化此操作

下面是我在沙箱管道项目中编写的一个快速示例

pipeline{
    agent {
        label 'Build'
    }
    stages{
        stage('Build'){
            steps{
                script{
                    echo "Building..."
                }
            }
        }
    }
    post{
        success {
            echo "Success"
        }
        failure {
            echo "Failure"
        }
        fixed {
            echo "Back to normal"
        }
    }
}

LGTM。也适用于脚本化管道,sans
script
tag:-)