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_这个插件的使用总结。

需求描述

  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.

插件主要设置参数描述

下面主要介绍了Email插件中主要参数的设置,由于本人的Jenkins为英文版,所以参数全部为英文,请使用中文的朋友自行对应设置即可。

Content Type: Both HTML and Plain Text

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

Attach Build Log: Do Not Attach Build Log

在邮件的附件中需要携带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);

Triggers

这个下面的设置直接决定了你的邮件触发的条件,你可以根据具体的情况设置自己的触发条件,我使用了两个:Failure - Any 和 Success。

  • Failure - Any

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

  • 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的邮箱

posted @ 2017-03-26 18:37  Rush_SONG  阅读(8909)  评论(0编辑  收藏  举报