Jenkins EMail-ext plugin sends groovy code in the Email body

10,363

Solution 1

Check the wiki page of the emai-ext plugin about using script content, the steps are described there:

  1. Put your groovy script into a file
  2. Place it on the jenkins master, under JENKINS_HOME/email-templates/
  3. In the email configuration use the script token replacing the content: ${SCRIPT, template="name-of-template"}

Solution 2

For reference here is the sample code by @Vel Ganesh with syntax fixed. This is rough but working with jenkins 2.7.4 Jan 2017).

Follow the answer by @Akos Bannerth. Put groovy script into test.groovy file.

<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>
Share:
10,363
Vel Ganesh
Author by

Vel Ganesh

Updated on August 28, 2022

Comments

  • Vel Ganesh
    Vel Ganesh over 1 year

    The 'Default Content' of Extended E-mail Notification is set as below and Default Content Type is '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>
    

    But the report e-mail has the groovy code itself in its body rather than the expected values.