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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM