Jenkins EMail-ext插件在Email主体中发送groovy代码

时间:2014-05-07 06:57:18

标签: email groovy jenkins jenkins-plugins

扩展电子邮件通知的“默认内容”设置如下,默认内容类型为“HTML(文本/ html)”:

<html>
<body>
<%
    if(build.testResultAction) {
        def testResult = build.testResultAction
        def testCount = String.format("%.2f",(testResult.totalCount))
        def testPassed = String.format("%.2f",())
        def testFailed = String.format("%.2f",(testResult.result.failCount))
        def testSkipped = String.format("%.2f",())
        def buildDuration = String.format("%.2f",(testResult.result.duration ))
    }
%>
Hi All,<br><br>

The execution of the Automation suite has been completed and the results are as below.<br><br>

<b><u>Configuration :</u></b><br>
Project Name : $JOB_NAME<br>
Test Server URL : $Test_Server_URL<br>
Group Name : $Group_Name<br><br>

<b><u>Execution Results :</u></b><br>
Status : <font color="blue">$BUILD_STATUS</font><br>
Tests run : $testCount<br>
Failures : $testFailed<br>
Errors : 0<br>
Skipped : 0<br>
Total time : $buildDuration<br>
Finished at: Tue May 06 17:12:19 IST 2014<br>
Build URL : $BUILD_URL<br><br>

The HTML version of the automation results and the log file is attached with this e-mail for your reference.<br><br>

Regards,<br>
Jenkins CI Tool
</body>
</html>

但是报告电子邮件在其正文中具有groovy代码本身而不是预期值。

2 个答案:

答案 0 :(得分:7)

查看emai-ext plugin关于使用脚本内容的wiki页面,其中描述的步骤如下:

  1. 将您的groovy脚本放入文件
  2. 将其放在JENKINS_HOME/email-templates/
  3. 下的jenkins主人身上
  4. 在电子邮件配置中,使用脚本标记替换内容:${SCRIPT, template="name-of-template"}

答案 1 :(得分:1)

此处参考的是@Vel Ganesh的示例代码,其语法已修复。 这很粗糙,但与jenkins合作2.7.4 2017年1月)。

按照@Akos Bannerth的回答。 将groovy脚本放入test.groovy文件中。

<html>
<body>
<%

    import hudson.model.*

    def build = Thread.currentThread().executable
    def buildNumber = build.number
    def buildNameJ = build.getDisplayName()

    def testCount = "0"
    def testPassed = "0"
    def testFailed = "0"
    def testSkipped = "0"
    def buildDuration = "0"
    if(build.testResultAction) {
        def testResult = build.testResultAction
        testCount = String.format("%d",(testResult.totalCount))
        testPassed = String.format("%d",(testResult.result.passCount))
        testFailed = String.format("%d",(testResult.result.failCount))
        testSkipped = String.format("%d",(testResult.result.skipCount))
        buildDuration = String.format("%.2f",(testResult.result.duration ))
    }

    def workspace = build.getEnvVars()["WORKSPACE"]
    def buildName = build.getEnvVars()["JOB_NAME"]
    def BUILD_STATUS = build.getEnvVars()["BUILD_STATUS"]
    def BUILD_URL = build.getEnvVars()["BUILD_URL"]
    def Test_Server_URL  = build.getEnvVars()["Test_Server_URL"]
    def Group_Name = build.getEnvVars()["Group_Name"]
%>

Summary test report <br><br>

<b><u>Configuration :</u></b><br>
Workspace : $workspace<br>
Project Name : $buildName  $buildNameJ<br>
Test Server URL : $Test_Server_URL<br>
Group Name : $Group_Name<br><br>

<b><u>Execution Results :</u></b><br>
Status : <font color="blue">$BUILD_STATUS</font><br>
Tests run : $testCount<br>
Failures : $testFailed<br>
Errors : . . . TODO . . . <br>
Skipped : $testSkipped<br>
Total time : $buildDuration<br>
Finished at: Tue May 06 17:12:19 IST 2014<br>
Build URL : $BUILD_URL<br><br>

test.groovy

</body>
</html>
相关问题