Jenkins声明式管道在每个阶段都发送电子邮件 [英] Jenkins declarative pipeline send email at every stage

查看:73
本文介绍了Jenkins声明式管道在每个阶段都发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Jenkins管道的每个阶段发送电子邮件.是否可以 ?.基本上每个阶段,无论成功还是失败,我都想发送电子邮件.阶段涉及Build,package,deploy

Hi I am trying send email at every stage in a Jenkins pipeline . Is it possible ?. Basically every stage I would like to send email if it success or failure . Stages involve Build , package , deploy

推荐答案

这是一种方法.我已使用Slack进行通知,您可以将其替换为邮件通知.

Here is one way. I have used Slack for notification, you can replace it with mail notification.

对于下面的示例,如果阶段运行成功,它将在两个阶段发送成功消息,但是,如果任何一个阶段失败,它将发送指示特定阶段已失败并且也使构建失败的失败消息./p>

For the below example, it will send the success message for both the stages if stage run is success, but if any one stage is failed it will send failure message indicating that particular stage has failed and fail the build as well.

注意:要在每个阶段更好地复用帖子构建,可以使用共享库.

Note: For better reuse of the post build in every stage, you can make use of shared library.

pipeline {
    agent any

    stages {
        stage("Echo Environment Variable") {
    steps {
        sh "echo 'Hello World'"
    }
    post {
        success { 
            slackSend channel: '#notification', color: 'good', message: "${env.STAGE_NAME} is Success", teamDomain: 'testnotifgroup', tokenCredentialId: 'slack-token'
        }
        unstable { 
            slackSend channel: '#notification', color: 'warning', message: "${env.STAGE_NAME} is Unstable", teamDomain: 'testnotifgroup', tokenCredentialId: 'slack-token'
        }
        failure { 
            slackSend channel: '#notification', color: 'danger', message: "${env.STAGE_NAME} is Failed", teamDomain: 'testnotifgroup', tokenCredentialId: 'slack-token'
        }
    }
}

stage("Test Stage") {
    steps {
        echo 'Hello World'
    }
    post {
        success { 
            slackSend channel: '#notification', color: 'good', message: "${env.STAGE_NAME} is Success", teamDomain: 'testnotifgroup', tokenCredentialId: 'slack-token'
        }
        unstable { 
            slackSend channel: '#notification', color: 'warning', message: "${env.STAGE_NAME} is Unstable", teamDomain: 'testnotifgroup', tokenCredentialId: 'slack-token'
        }
        failure { 
            slackSend channel: '#notification', color: 'danger', message: "${env.STAGE_NAME} is Failed", teamDomain: 'testnotifgroup', tokenCredentialId: 'slack-token'
        }
    }
}
    }
}

这是相同的屏幕截图:

Here is the screenshot for the same:

这篇关于Jenkins声明式管道在每个阶段都发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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