Jenkins的EMail-ext插件在电子邮件正文中发送Groovy代码

3
扩展电子邮件通知的“默认内容”如下所示,并且默认内容类型为“HTML(text/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代码本身,而不是预期的值。

顺便说一句,如果按照@Akos Bannerth的回答所述,在email-ext插件下将此脚本放入test.groovy并运行,那么我们会在第7和9行收到异常。我们漏掉了什么吗?"""“模板渲染过程中引发了异常:无法解析模板脚本(您的模板可能包含错误或正在尝试使用当前不支持的表达式):启动失败:SimpleTemplateScript1.groovy: 7: 预期外的符号:) @第7行,第48列。assed = String.format("%.2f",()) ^ 1""" - gaoithe
2个回答

8
请查看emai-ext插件的维基页面,了解如何使用脚本内容。步骤如下:
  1. 将groovy脚本放入文件中。
  2. 将文件放置在jenkins主机上,路径为:JENKINS_HOME/email-templates/
  3. 在电子邮件配置中使用脚本令牌替换内容:${SCRIPT, template="name-of-template"}
请注意,不要删除任何HTML标签。

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接