34
votes

I am new to Jenkins and I want to know how it is possible to display the HTML report (not the HTML code) generated after a successful build inside a mail body (not as an attachment).

I want to know the exact steps I should follow and what should be the content of my possible jelly template.

7

7 Answers

57
votes

Look deeper into the plugin documentations. No need for groovy here.

Just make sure Content Type is set to HTML and add the following to the body:

${FILE,path="my.html"}

This will place the my.html content in your email body (location of file is relative to job's workspace. I use it and it works well.

I hope this helps.

EDIT: Note that you must have the Jenkins version 1.532.1 (or higher) to support this feature with the email-ext plugin.

8
votes

It worked for me with Jenkins 1.558

${FILE,path="target/failsafe-reports/emailable-report.html"}
6
votes

Besides reading the file with body: ${FILE,path="index.html"}, you need to set the proper content type, either globally or explicitly for one execution, with mimeType: 'text/html.

emailext subject: '$DEFAULT_SUBJECT',
                    body: '${FILE,path="index.html"}',
                    recipientProviders: [
                        [$class: 'CulpritsRecipientProvider'],
                        [$class: 'DevelopersRecipientProvider'],
                        [$class: 'RequesterRecipientProvider']
                    ], 
                    replyTo: '$DEFAULT_REPLYTO',
                    to: '$DEFAULT_RECIPIENTS',
                    mimeType: 'text/html'
3
votes

It should be something like this:

Navigation:

Configure -> Editable Email Notification

Default Content:

${FILE,path="path/result.html"}
2
votes

You just need to assign the link to the environment variable and then you can use that variable to print in the email using ${ENV, var=ENV_VARIABLE}.

1
votes

You can use Editable Email Notification post build action to send html content as part of mail body.

Copy html content in Default Content and select Content Type as HTML (text/html), as in below image: enter image description here

1
votes

If you use a custom path

I had a complication trying to achieve this result because my path was dynamically changing and I had to use a variable inside a FILE variable. So when I tried any of the following

body: '${FILE,path=${report}}'
body: "${FILE,path=${report}}"
body: '''${FILE,path=${report}}'''

and many more, it didn't work. On the other hand I couldn't read the file with groovy because of Jenkins restrictions

My workaround was to read the html directly with shell like so

html_body = sh(script: "cat ${report}", returnStdout: true).trim()

and then just send the email

emailext replyTo: '$DEFAULT_REPLYTO',
  subject: "subject",
  to: EMAIL,
  mimeType: 'text/html',
  body: html_body

where ${report} was a path to html file like /var/jenkins/workspace_318/report.html