4

I want to use the Groovy scripting feature in the email-ext plugin for Jenkins, but I'm new to this and there seems to be a lot of assumed knowledge. Like how one goes about invoking one of these templates in the first place.

The answer to this is probably quite obvious, but I'm feeling a bit lost and would appreciate being pointed in the right direction.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Sparky
  • 2,694
  • 3
  • 21
  • 31

3 Answers3

15

This example is based on the official email-ext documentation, which unfortunately does not provide any concrete examples on how to use the $SCRIPT line of code in Pipeline. If you wish to use an HTML template as the body for your email then you need to:

  1. Create a template file called my-email.template or whatever you like - you can find some template examples here

    <body>
      <h3>Using "build" environment variables:</h3>
      <p>
        <a href="<%= build.absoluteUrl %>"><%= build.fullDisplayName %></a>
      </p>
      <h3>List of all available "build" environment variables:</h3>
      <div>
        <% println build.properties.collect{it}.join('<br />') %>
      </div>
    </body>
    
  2. Have your Jenkins administrator place the my-email.template file inside $JENKINS_HOME\email-templates directory on Jenkins machine - make sure that user jenkins owns this directory as well as its content (i.e. template files)

  3. In Pipeline load my-email.template as body content:

    stage('Send email') {
        def mailRecipients = "jenkins-user@example.com"
        def jobName = currentBuild.fullDisplayName
    
        emailext body: '''${SCRIPT, template="my-email.template"}''',
        subject: "[Jenkins] ${jobName}",
        to: "${mailRecipients}",
        replyTo: "${mailRecipients}",
        recipientProviders: [[$class: 'CulpritsRecipientProvider']]
    }
    
Goran Vasic
  • 1,079
  • 10
  • 11
  • 2
    I know this is an old post but I am trying the same ( to send cppcheck reports by email) and I found this template I wanted to try, I just created a file.template in `$JENKINS_HOME\email-templates` and then added a stage like this to my pipeline `emailext( mimeType: 'text/html', body: '${SCRIPT, template="my_email.template"}', subject: "[Jenkins] ${jobName}", to: "${mailRecipients}")` but instead of receiving the template I receive a the string `${SCRIPT, template="my_email.template"}` – Jesus Fernandez Apr 07 '21 at 15:52
  • @JesusFernandez Have you tried wrapping it in 3 single quotation marks, like in the example above? – Goran Vasic Apr 07 '21 at 16:00
  • yes I have, but I have just realized I am trying to send a jelly script not an html so I guess this does not work for me (it is my first time using this plugin...) – Jesus Fernandez Apr 07 '21 at 16:01
  • how to use conditionals in this , this didn't work for me ```if ( "${env.MY_ENV}" == "true" ) { %>``` – vgdub Jun 03 '21 at 14:24
1

It's a pretty old thread but here's what I did to make use of the built in template do this

    stage('Send Email')
    {
        steps
        {
            emailext body: '${JELLY_SCRIPT,template="html"}',recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']], subject: 'Build Successful ${JOB_NAME}',mimeType: 'text/html'
        }
                
    }

The Plugin Documentation states that the html Jelly script is built in so you can easily make use of it.

https://plugins.jenkins.io/email-ext/

Showmik Bose
  • 109
  • 5
-2

The answer is, in fact, blatantly obvious, and included in the email-ext documentation:

${SCRIPT, template="groovy-text.template"}
Zoe
  • 27,060
  • 21
  • 118
  • 148
Sparky
  • 2,694
  • 3
  • 21
  • 31
  • 5
    The documentation is poorly written, judging by the number of questions people are asking. There are no examples on how to use this line in your Jenkinsfile. Simply entering this line will cause the "unexpected token: SCRIPT" error, so could you please provide an example of how to use this line? – Goran Vasic Feb 10 '17 at 15:00
  • I'm afraid this was ages ago and I can't even remember which project this was for but I have vague memories of using it in the box for the email contents which you access via the Jenkins web UI. Sorry I can't be more helpful. – Sparky Feb 10 '17 at 16:22