Jenkins 发邮件的Job

Jenkins要做到构建失败的时候发送邮件,常规做法是加个全局的post failure,类似这样的代码

pipeline {
    agent any

    stages {

        stage('deploy') {
            steps {
                script {
                    error 'failed';

                }
            }
        }

    }

    post {
        always {
            echo 'This will always run'
        }
        success {
            echo 'This will run only if successful'
        }
        failure {
            mail bcc: '', body: "<b>Example</b><br>Project: ${env.JOB_NAME} <br>Build Number: ${env.BUILD_NUMBER} <br> URL de build: ${env.BUILD_URL}", cc: '', charset: 'UTF-8', from: '', mimeType: 'text/html', replyTo: '', subject: "ERROR CI: Project name -> ${env.JOB_NAME}", to: "foo@foomail.com";
        }
        unstable {
            echo 'This will run only if the run was marked as unstable'
        }
        changed {
            echo 'This will run only if the state of the Pipeline has changed'
            echo 'For example, if the Pipeline was previously failing but is now successful'
        }
    }

}

  这种做法有一点不好之处就是有几个JOB 需要发邮件就需要写几份发邮件代码,对原来Jenkins代码也有侵染。

有一种办法可以避免这样的情况,新建一个JOB ,这个JOB名字就叫做发邮件,把这个JOB 设置为上游JOB 触发,意思是当需要发邮件的JOB 在构建完以后,不管成功失败都会触发发邮件的JOB,来实现统一发邮件。

1.新建pipeline JOB, 在‘其它工程后触发’ 这里填上你想要发邮件的job,如果有多个使用逗号分隔 .

 

 

 

2.pipleline 的代码

pipeline {
    agent any

    stages {

        stage('deploy') {
            steps {
                script {
                    String buildResult = "";
                    String buildNumber = "";
                    String jobName = "";
                    (buildResult, jobName, buildNumber) = getUpstreamBuildResult();

                    String buildUrl = "http://localhost:8080/job/${jobName}/${buildNumber}/console"

                    if (buildResult == 'FAILURE') {
                        echo "send email";
                        mail bcc: '', body: "<b>Example</b><br>Project: ${jobName} <br>Build Number: ${buildNumber} <br> URL de build: ${buildUrl}", cc: '', charset: 'UTF-8', from: 'tianhe1006@126.com', mimeType: 'text/html', replyTo: '', subject: "ERROR CI: Project name -> ${jobName}", to: "jonas.tian@activenetwork.com";
                    }

                }
            }
        }

    }

}

def getUpstreamBuildResult() {
    def upstream = currentBuild.rawBuild.getCause(hudson.model.Cause$UpstreamCause)
    String jobName = upstream ? .getUpstreamProject();
    echo jobName;
    String buildNumber = "${upstream?.getUpstreamBuild()}";
    echo buildNumber;
    def upstreamTriggerBuildResult = Jenkins.instance.getAllItems(AbstractItem.class)
        .findAll {
            it.fullName == upstream.getUpstreamProject()
        }
        .collect {
            proj - > proj.getBuilds().findAll {
                it.number == upstream.getUpstreamBuild()
            }.collect {
                it.getResult()
            }
        }
        .flatten()[0];

    echo "${upstreamTriggerBuildResult}";

    return [upstreamTriggerBuildResult, jobName, buildNumber];
}

  注意一点,这里用到了不安全代码,需要手动approve,运行一次,就会看到报错,一直点approve 就好了,但是如果不想这么麻烦可以安装一个插件

Permissive Script Security Plugin

https://wiki.jenkins.io/display/JENKINS/Permissive+Script+Security+Plugin

安装这个插件后就可以自动approve 不安全代码。

 

posted @ 2019-12-17 15:11  老地下党员  阅读(580)  评论(0编辑  收藏  举报