jenkinspiplineでEmail-extプラグインを使用する



Use Email Ext Plugin Jenkins Pipline



最初のインストール Email-extプラグイン プラグイン
Jenkinsの構成-> Jenkinsの管理-> jenkinsでのシステムの構成
画像
テストスクリプトはJava言語に基づいているため、テスト結果を公開してデータを生成するには、JUnitプラグインが必要です。 Jenkins-> Jenkinsの管理->プラグインの管理にjunitプラグインをインストールします
Email-extプラグインを使用して、Groovyスクリプトで電子メールをカスタマイズできます。非常に強力で、作者はいくつかのテンプレートを提供しています テンプレート そして、ビルドを開始できます。
Jenkinsファイル:

pipeline{ agent any tools { maven 'M 3' } stages{ stage('checkout code'){ steps { script{ //pull your code }} } stage('run test case'){ steps { script{ //withMaven(maven: 'M 3') { // bat 'mvn test' // } //bat 'cd C:\Users\liul5\Desktop\test\project' bat 'mvn test' }} } } post('Generate report') { always { script{ cucumber fileIncludePattern: '**/cucumber-default-reports/*.json', sortingMethod: 'ALPHABETICAL' junit '**/cucumber-default-reports/*.xml' / / Must have this step, otherwise there is no data emailext subject: 'Automation Result5: Job '${env.JOB_NAME} - ${env.BUILD_NUMBER}'', body:'''${SCRIPT,template='groovy-html-larry-refactor.template'}''', to:'$DEFAULT_RECIPIENTS' emailext subject: 'Automation Result6: Job '${env.JOB_NAME} - ${env.BUILD_NUMBER}'', body:''' ${SCRIPT,template='groovy-html-refactor.template'}''', to:'$DEFAULT_RECIPIENTS' emailext subject: 'Automation Result: Job '${env.JOB_NAME} - ${env.BUILD_NUMBER}'', body:''' total:${TEST_COUNTS,var='total'}, pass:${TEST_COUNTS,var='pass'}, fail:${TEST_COUNTS,var='fail'} ''', to:'$DEFAULT_RECIPIENTS' }}} }

本文には参照メールテンプレートが使用されています${SCRIPT,template='groovy-html-refactor.template'} 3つの引用符で囲む必要があります。作成者から直接提供されたテンプレートを使用できます。${SCRIPT,template='template name'}
テンプレートをカスタマイズする場合は、jenkinsルートディレクトリにルートを作成する必要があります。email-templatesフォルダ、テンプレートをこのディレクトリに配置します(WindowsパスでのJenkins非msiインストールモード:C:Usersusername.jenkinsemail-templates、 msiのインストール方法:C:Program Files (x86)Jenkinsemail-templates)。メールの構文をカスタマイズします。プラグインのドキュメントを確認してください。 資料 、または作成者のテンプレート テンプレート
最終結果グラフ:
画像
画像
テンプレート1:



<STYLE> BODY, TABLE, TD, TH, P { font-family: Calibri, Verdana, Helvetica, sans serif font-size: 12px color: black } .console { font-family: Courier New } .filesChanged { width: 10% padding-left: 10px } .section { width: 100% border: thin black dotted } .td-title-main { color: white font-size: 200% padding-left: 5px font-weight: bold } .td-title { color: white font-size: 120% font-weight: bold padding-left: 5px text-transform: uppercase } .td-title-tests { font-weight: bold font-size: 120% } .td-header-maven-module { font-weight: bold font-size: 120% } .td-maven-artifact { padding-left: 5px } .tr-title background-color: <%= (build.result == null .test { padding-left: 20px } .test-fixed { color: #27AE60 } .test-failed { color: #E74C3C } STYLE> <BODY> <table class='section'> <tr class='tr-title'> <td class='td-title-main' colspan=2> BUILD ${build.result ?: 'COMPLETED'} td> tr> <tr> <td>Project:td> <td>${project.name}td> tr> <tr> <td>Build #${build.number}:td> <td><A href='${rooturl}${build.url}'>${rooturl}${build.url}A>td> tr> <tr> <td>Date:td> <td>${it.timestampString}td> tr> <tr> <td>Duration:td> <td>${build.durationString}td> tr> <tr> <td>Cause:td> <td> %> ${cause.shortDescription} td> tr> <tr> <td>Report:td> <td><A href='${rooturl}${build.url}cucumber-html-reports/overview-features.html'>${rooturl}${build.url}cucumber-html-reports/overview-features.htmlA>td> tr> table> <br/> 0 ) { %> <table class='section'> <tr class='tr-title'> <td class='td-title' colspan='5'>${junitResultList.first().displayName}td> tr> <tr> <td class='td-title-tests'>Totaltd> <td class='td-title-tests'>Passedtd> <td class='td-title-tests'>Failedtd> <td class='td-title-tests'>Skippedtd> <td class='td-title-tests'>Success Ratetd> tr> junitResult.getChildren().each { packageResult -> %> <tr> <td>${total}td> <td>${passed}td> <td>${failed}td> <td>${skipped}td> <td>${rate}%td> tr> %> <tr> <td class='test test-fixed' colspan='5'> ${test.getFullName()} ${test.getStatus()} td> tr> a.getAge() b.getAge()}).each{ failed_test -> %> <tr> <td class='test test-failed' colspan='5'> ${failed_test.getFullName()} (Age: ${failed_test.getAge()}) td> tr> table> <br/> <table class='section'> <tr class='tr-title'> <td class='td-title' colspan='2'>CHANGEStd> tr> cs_list.each() { cs -> hadChanges = true %> <tr> <td> Revision by <B>B> td> <td>${cs.msgAnnotated}td> tr> <% cs.affectedFiles.each() { p -> %> <tr> <td class='filesChanged'>${p.editType.name}td> <td>${p.path}td> tr> <tr> <td colspan='2'>No Changestd> tr> table> <br/> 0 ) { %> <table class='section'> <tr class='tr-title'> <td class='td-title'>BUILD ARTIFACTStd> tr> %> <tr> <td> <a href='${rooturl}${build.url}artifact/${f}'>${f}a> td> tr> table> <br/> <table class='section'> <tr class='tr-title'> <td class='td-title'>BUILD ARTIFACTStd> tr> %> <tr> <td class='td-header-maven-module'>${m.key.displayName}td> tr> def artifactz = mvnbld.artifacts if ( artifactz != null && artifactz.size() > 0) { %> <tr> <td class='td-maven-artifact'> %> <a href='${rooturl}${mvnbld.url}artifact/${f}'>${f}a><br/> td> tr> table> <br/> <table class='section' cellpadding='0' cellspacing='0'> <tr class='tr-title'> <td class='td-title'>CONSOLE OUTPUTtd> tr> %> <tr> <td class='console'>${org.apache.commons.lang.StringEscapeUtils.escapeHtml(line)}td> tr> table> <br/> BODY>

テンプレート2:

<html> <head> <meta http-equiv='Content-Type' content='text/html charset=utf-8'> <title>Build from Jenkisntitle> <style type='text/css'> /*base css*/ a{color:#4a72af} body{background-color:#e4e4e4} body,p{margin:0padding:0} img{display:block} h1,h2,h3,h4,h5,h6{margin:0 0 .8em 0} h3{font-size:28pxcolor:#444!importantfont-family:Arial,Helvetica,sans-serif} h4{font-size:22pxcolor:#4a72af!importantfont-family:Arial,Helvetica,sans-serif} h5{font-size:18pxcolor:#444!importantfont-family:Arial,Helvetica,sans-serif} p{font-size:12pxcolor:#444!importantfont-family:'Lucida Grande','Lucida Sans','Lucida Sans Unicode',sans-serifline-height:1.5} ol li img{display:inlineheight:20px} /*div styles*/ /*.news{text-align:centerpadding-top:15px}*/ .content{width:720pxbackground-color:whitemargin-left: auto margin-right: auto } .round_border{margin-bottom:5px-webkit-border-radius:6px-moz-border-radius:6pxborder-radius:6pxmargin-top:0font-size:14pxpadding:6pxborder:1px solid #ccc} .status{background-color:greenfont-size:28pxfont-weight:boldcolor:whitewidth:720pxheight:52pxmargin-bottom:18pxtext-align:centervertical-align:middleborder-collapse:collapsebackground-repeat:no-repeat} .status .info{color:white!importanttext-shadow:0 -1px 0 rgba(0,0,0,0.3)font-size:32pxline-height:36pxpadding:8px 0} .main img{width:38pxmargin-right:16pxheight:38px} .main table{font-size:14px} .main table th{text-align:left} .bottom-message{width:720pxcellpadding:5pxcellspacing:0px} .bottom-message .message{font-size:13pxcolor:#aaaline-height:18pxtext-align:center} .bottom-message .designed{font-size:13pxcolor:#aaaline-height:18pxfont-style: italictext-align:right} img.cartoon {width: 36px display:inline} style> head> <body> <div class='content round_border'> <div class='status'> <p class='info'>The build ${build.result ?: 'completed'}p> div> <div class='main round_border'> <table> <tbody> <tr> <th>Project:th> <td>${project.name}td> tr> <tr> <th>Build #${build.number}:th> <td><A href='${rooturl}${build.url}'>${rooturl}${build.url}A>td> tr> <tr> <th>Date of build:th> <td>${it.timestampString}td> tr> <tr> <th>Build duration:th> <td>${build.durationString}td> tr> <tr> <th>Build cause:th> <td> %> ${cause.shortDescription} td> tr> <tr> <th>Test results:th> <td>Total: ${total} Passed:${passed} Failed:${failed} Skipped:${skipped}td> tr> tbody> table> div> <div class='artifacts round_border'> <b>Report Artifacts:b> <ul> <li><A href='${rooturl}${build.url}cucumber-html-reports/overview-features.html'>${rooturl}${build.url}cucumber-html-reports/overview-features.htmlA>li> <li><a href='#'>display in new pagea>li> ul> div> div> <table class='bottom-message' align='center'> <tr> <td class='message'>You are receiving this email because you are relavent with this build<br> <p> <a href='#'>support teama> | <a href='#'>QAa> p> td> tr> table> body> html>

参照:
https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin#Email-extplugin-TemplateExamples
http://ikeptwalking.com/using-email-ext-plugin-in-jenkins-pipeline/
https://github.com/jenkinsci/email-ext-plugin/tree/master/src/main/resources/hudson/plugins/emailext/templates
https://gist.github.com/arjabbar/c6f27c64fd18153680f3b52102688c13