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的郵箱

相關文章
相關標籤/搜索