embed image in email body jenkins pipeline

早过忘川 提交于 2019-12-08 06:44:39

问题


I need to add an image in email as email body not as attachment, from Jenkins via pipeline. I am using emailext plugin in Jenkins pipeline, below is the code I am using.

emailext (
          subject: "test email",
          body: """
          <html>
          <body>
          <p>please find attached score: Job '${env.JOB_NAME}':</p>
          <p> The last commit was by ${last_commit_user} </p>
          <p>Please check jenkins console at "</p> 
          <p> For detailed report on this analysis, visit "</p>
          </body>
          </html>
          """,
          to: email_recipients,
          attachmentsPattern: '${BUILD_NUMBER}.jpg'
)

I do not want to use "attachmentsPattern" , that comes as an attachment, I tried using,

body: """ 
<html>
<img src="image_name.jpg" >
</html>
"""

That comes only as blue box in my email , I am giving proper image path relative to my Jenkins workspace, I tried to search relevant solutions but in vain

please advise


回答1:


You need to convert your image to Base64. I do it using python.

import base64

base64Img = ''

with open("image.png", "rb") as imageFile:
    base64Img = base64.b64encode(imageFile.read())

with open("image.html", "wb+") as writer:
    writer.write('<img src="data:image/png;base64,'.encode())
    writer.write(base64Img)
    writer.write('">'.encode())

This writes a file ''image.html''. This file you can then append into your <body>. In my pipeline I do it like so:

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



回答2:


Push the image file in workspace directory of jenkins job.
Goto the workspace and click on the image to get the URL.
Place img src="IMAGE_URL" in email template.




回答3:


To include images into your Jenkins Extended E-mail mails, you have to:

  • Put the images into this Jenkins directory: $JENKINS_HOME/war/images/
  • And then restart Jenkins service

And you can insert the image YOUR_IMAGE.jpg into your groovy-html.template. Note that ${rooturl}static/e59dfe28/images is constant.

<p>Some HTML...</p>
<img src="${rooturl}static/e59dfe28/images/YOUR_IMAGE.jpg"/>
<p>Some HTML...</p>


来源:https://stackoverflow.com/questions/47432692/embed-image-in-email-body-jenkins-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!