Jenkis Editable Email Notification Plugin 使用介绍

Jenkis Editable Email Notification Plugin 使用介绍

前言

Jenkins自己提供的Email插件功能实在有限,只能提供当前Job的基本信息,好比成功、失败以及不稳定的状态给设定的接收着。我在搭建基于Jenkins+Robot framework的自动化测试平台过程当中须要在每一个自动化的测试Job结束后根据当前测试的结果向设定的接收着发送测试报告,要求测试报告的标题及紧急程度按照成功或者失败来肯定。个人第一个想法就是使用Java的Email Libray而后在Job结束后去调用发送邮件功能,以前也一直是这么作的,但自从发现标题中的plugin后发现本身以前使用的方法好low,下面就是我对_Editable Email Notification_这个插件的使用总结。html

需求描述

  1. Job启动参数在启动Job时指定,全部参数都带有默认值;
  2. Check out code from git server;
  3. Execute Automation launch shell script, this script file saved in git;
  4. The shell script will launch automation testing with parameters;
  5. Wait until automation testing finished, decide current job success or failure upon the return value return from testing process;
  6. Send success or failure Email notification to receivers decided by job execute status.java

    插件主要设置参数描述

    下面主要介绍了Email插件中主要参数的设置,因为本人的Jenkins为英文版,因此参数所有为英文,请使用中文的朋友自行对应设置便可。git

Content Type: Both HTML and Plain Textshell

在邮件的正文中要插入HTML代码,因此在Content Type中要选择支持HTML和富文本

Attach Build Log: Do Not Attach Build Logide

在邮件的附件中须要携带automation的详细report,因此不须要带Job自己的log信息

Pre-send Script:测试

// 下面全部的代码都会被执行到,只能支持Java内置的Library,使用以前必定要import
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.mail.internet.InternetAddress;

//设置邮件的Subject,发件人以及发件人的Email Address,这边地址能够设置一个根本不存在的以免骚扰
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String subject= "Automation Report (" + sdf.format(new Date()) +")";
String personalSender = "Automation Team"
String emailAddress = "do.not.reply@your_company.com"

/**
build是一个内置变量,获得当前job的执行状态。点击右边的问号查看支持的内置变量
若是job失败则将当前邮件的重要性,优先级都设置为最高以优先投递
**/
if( build.result.toString().equalsIgnoreCase("FAILURE") )
{
      msg.addHeader("Importance", "High"); 
      msg.addHeader("X-Priority", "1 (Highest)");   

     subject = "[Failed]" + subject;
}else{
     subject = "[Passed]" + subject;
}
// 内置变量,设置邮件的Subject
msg.setSubject(subject);

/**Set sender**/
InternetAddress address = new InternetAddress();
try
{
    address.setPersonal(personalSender);
}
catch (UnsupportedEncodingException e)
{
    e.printStackTrace();
}
address.setAddress(emailAddress);

msg.setSender(address);

Triggersui

这个下面的设置直接决定了你的邮件触发的条件,你能够根据具体的状况设置本身的触发条件,我使用了两个:Failure - Any 和 Success。this

  • Failure - Any插件

    Recipient List: 设置失败的时候你的收件人列表,支持变量。通常失败的时候我都会抄送老板^~^
    Content Type: HTML (text/html)
    Content: 这里面设置要发送的邮件正文模版,支持变量。能够点击右侧的问号来查看支持的内置变量。若是须要插入HTML格式的文件到正文,语法格式相似于:${FILE, path="${REPORT_SUMMARY}"}
    Attachments: 要添加到附件中的文件,支持变量,多个文件使用逗号分割
    Attach Build Log:不须要在附件中携带build logcode

  • Success

    Recipient List: 设置失败的时候你的收件人列表,支持变量。
    Content Type: HTML (text/html)
    Content: 这里面设置要发送的邮件正文模版,支持变量。能够点击右侧的问号来查看支持的内置变量.若是须要插入HTML格式的文件到正文,语法格式相似于:${FILE, path="${REPORT_SUMMARY}"}
    Attachments: 要添加到附件中的文件,支持变量,多个文件使用逗号分割
    Attach Build Log:不须要在附件中携带build log

总结

本文介绍了Jenkins插件Editable Email Notification的一个使用场景,这个插件极大的扩展了内置Email的功能。若是有任何问题欢迎留言或者发送邮件到个人邮箱:
Rush的邮箱

相关文章
相关标签/搜索