Jenkins 声明式管道在每个阶段发送电子邮件

Jenkins declarative pipeline send email at every stage

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

这是一种方法。我用过Slack通知,你可以换成邮件通知。

对于下面的示例,如果 运行 阶段成功,它将发送两个阶段的成功消息,但如果任何一个阶段失败,它将发送失败消息,指示特定阶段失败并失败构建也是如此。

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'
        }
    }
}
    }
}

这是相同的屏幕截图: