37

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.

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
HobbitOfShire
  • 2,144
  • 5
  • 24
  • 41

7 Answers7

59

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.

Eldad Assis
  • 10,464
  • 11
  • 52
  • 78
  • 1
    i set the content to html and did as mentioned now it is returning an error saying in the body of the mail saying `ERROR: File '/data/test.html' does not exist` – HobbitOfShire Feb 27 '14 at 14:56
  • 1
    `/data/test.html` is an absolute path from the root of the file system (leading slash). Try only `data/test.html`. – Eldad Assis Feb 27 '14 at 15:02
  • supposed that the absolute path of my html file is `/data/jenkins/jobs/GAMETV/htmlreports/HTML_Report/GAMETV.html` what should i put as path ? – HobbitOfShire Feb 27 '14 at 15:27
  • 1
    As an initial test, put the whole path `/data/jenkins/jobs/GAMETV/htmlreports/HTML_Report/GAMETV.html` to see if it works. – Eldad Assis Feb 27 '14 at 15:32
  • Is your Jenkins running as a slave? Is the file accessible to the user running Jenkins? Is the java process that runs the Jenkins instance located on the same file system as the file? Try putting the file in the job's workspace and try just that. – Eldad Assis Feb 27 '14 at 15:40
  • the html file i am trying to show is actually the html file generated on my last build so it should be accessible to the user as it is generated by the Jenkins' job – HobbitOfShire Feb 27 '14 at 15:55
  • Try testing with a manually created file in the workspace. Make sure you have an updated version of the plugin... – Eldad Assis Feb 27 '14 at 15:56
  • it seems that the problem is with my Jenkins version as the one i have is ver. `1.513` and the required one for the plugin is `1.532.1` – HobbitOfShire Feb 27 '14 at 16:16
  • Well, did you manage? Did you update your Jenkins and plugin? If so, let me know so I can edit my answer for others to benefit. – Eldad Assis Mar 01 '14 at 17:52
  • yes i managed to do it.just mention that the email-ext plugin needs at least 1.532.1 version of jenkins – HobbitOfShire Mar 03 '14 at 08:49
  • I was able to use this functionality with Jenkins ver. 1.509.4 and Email-ext 2.35.1. Also You can use absolute path and environment variables :) – Stoinov Aug 11 '14 at 13:58
  • this syntax sends html report in the email but i am looking for link to send in email. can some one help me on this? – Nick Nov 16 '15 at 22:36
  • Can i somehow enlarge the content of file in email. It looks small – Mohit Arora Aug 24 '16 at 12:09
  • @EldadAK your solution didn't work for me. For Editable Email notification Content Type selected as Html and Default Content field I've given value per your answer. Please clarify what step am I doing wrong ? – vikramvi Dec 05 '16 at 11:10
  • Did you add the `${FILE,path="my.html"}` to the email body? Do you have any error? – Eldad Assis Dec 05 '16 at 19:43
  • @EldadAK I did below steps but couldn't get it working. ${FILE,path="workspace/target/site/serenity/index.html"} in Content field of Editable Email Notification > Triggers > Always. It shows error message "ERROR: File 'workspace/target/site/serenity/index.html' does not exist". Can you please share screen shot – vikramvi May 17 '17 at 10:56
  • 2
    Got it working by updating "Editable Email Notification" > "Content Type" value to HTML (text/html) and "Default Content" field value to "${FILE,path="target/site/serenity/index.html"}. Didn't do any changes for Triggers fields. – vikramvi May 17 '17 at 12:36
  • I am also working on the same and able to see the report in mail body but the content display is not proper so do I need to any CSS in configuration property to get the proper display in email body? – user3538483 Jan 17 '19 at 13:44
  • Unfortunately this fails when job is aborted. It throws Null Pointer exception. – adev Apr 04 '19 at 01:08
  • Assuming you have expressions in the html like '${BUILD_NUMBER}' will they get resolved or will the remain as are. I need a way to format them before the actual send. – Yaron Feb 23 '22 at 15:54
10

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'
Raúl Salinas-Monteagudo
  • 3,412
  • 1
  • 24
  • 22
8

It worked for me with Jenkins 1.558

${FILE,path="target/failsafe-reports/emailable-report.html"}
Karan Thakur
  • 143
  • 2
  • 6
3

It should be something like this:

Navigation:

Configure -> Editable Email Notification

Default Content:

${FILE,path="path/result.html"}
Saikat
  • 14,222
  • 20
  • 104
  • 125
3

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

Sergey Pleshakov
  • 7,964
  • 2
  • 17
  • 40
2

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

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

Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
  • As of 2021, it seems to have been deprecated. – Elad Avron Jul 05 '21 at 07:34
  • @EladAvron if you face issue that instead of HTML content it puts the actual file path in the email body, then check if this latest version of the plugin - https://plugins.jenkins.io/token-macro/ is installed. I faced this issue and resolved when I updated this plugin. (it is internally used by Email Ext plugin, but seems to be not auto updated along with Email Ext update) – Sachin Ramdhan Boob Oct 25 '21 at 14:26