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

时间:2021-02-19 03:13:46

标签: jenkins jenkins-pipeline jenkins-groovy

嗨,我正在尝试在 Jenkins 管道的每个阶段发送电子邮件。是否有可能 ?。基本上每个阶段如果成功或失败,我都想发送电子邮件。阶段涉及到构建、打包、部署

1 个答案:

答案 0 :(得分:1)

这是一种方法。我用 Slack 做通知,你可以用邮件通知代替。

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

注意:为了在每个阶段更好地重用后期构建,您可以使用共享库。

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

这是相同的屏幕截图: enter image description here