Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting email-ext script templates to work with Jenkins pipeline

I recently converted to Jenkins 2.x and I am experimenting with pipeline flow, but I can't seem to get the email-ext plugin to work with groovy script templates. Although my standard flow still work fine, if I try the following I get an error with unexpected token SCRIPT

    emailext mimeType: 'text/html', replyTo: 'xxxx', subject: "${env.JOB_NAME} - Build# ${env.BUILD_NUMBER} - ${env.BUILD_STATUS}", to: 'xxxx', body: "${SCRIPT, template='regressionfailed.groovy'}"

I know that there were issues with token expansion early on, but it seems like from the latest wiki updates those have been fixed. I also still get no token expansion for any tokens. Is there any good reference to get this working again. I would like to switch to the pipeline flow but the email template with token expansion is key to may work flow.

like image 465
naven87 Avatar asked Oct 06 '16 03:10

naven87


People also ask

How do I run a script in Jenkins pipeline?

Click New Item on your Jenkins home page, enter a name for your (pipeline) job, select Pipeline, and click OK. In the Script text area of the configuration screen, enter your pipeline syntax.

How do I change my email template in Jenkins?

Added script under *Jenkins -> Manage Jenkins -> Editable Email Notification Templates-> Add New Template* and then under "Default Content" section, we added default groovy scripts (for example: groovy-html. template - Copy paste content). Save. Now under project job, under Post-build section we selected same template.

How do I trigger an email in Jenkins?

Go to the Jenkins home page and click the 'Manage Jenkins' menu option. Then, select the 'Configure System' option. Enter the SMTP server name under 'Email Notification'. Click the 'Advanced' button and then click the checkbox next to the 'Use SMTP Authentication' option.


4 Answers

There is no problem using emailext in declarative pipeline. But your script won't be able to access "build.result" parameter correctly because it is not yet finished. Like in the default script groovy-html.template.

Edit: Actually you can access build.result if you manually set it yourself.

So it is best to add a stage in the end of the declarative pipeline like so:

stage('Send email') {
    def mailRecipients = "[email protected]"
    def jobName = currentBuild.fullDisplayName

    emailext body: '''${SCRIPT, template="groovy-html.template"}''',
        mimeType: 'text/html',
        subject: "[Jenkins] ${jobName}",
        to: "${mailRecipients}",
        replyTo: "${mailRecipients}",
        recipientProviders: [[$class: 'CulpritsRecipientProvider']]
}

Also note, that if you are using your own script you cannot name it "groovy-html.template" or " groovy-text.template" because they are default of emailext (so the file will not even be accessed). See "Script content" here.

like image 176
user3360767 Avatar answered Oct 17 '22 03:10

user3360767


Apparently everybody know it. There are 2 way to define a pipeline: declarative pipeline (start with 'pipeline') and scripted pipeline (start with 'node')

Using declarative pipeline, it must be specified the script to execute a procedure, i.e. use def to define variables. So in pipeline case:

stage('Email') {
    steps {
        script {
            def mailRecipients = '[email protected]'
            def jobName = currentBuild.fullDisplayName
            emailext body: '''${SCRIPT, template="groovy-html.template"}''',
            mimeType: 'text/html',
            subject: "[Jenkins] ${jobName}",
            to: "${mailRecipients}",
            replyTo: "${mailRecipients}",
            recipientProviders: [[$class: 'CulpritsRecipientProvider']]
        }
    }
}

I spent some time for this, I hope it is helpfull for someone else.

like image 23
Daniele Cruciani Avatar answered Oct 17 '22 02:10

Daniele Cruciani


Faced the same issue today, apparently having the body defined before the emailext seem to do the trick:

def emailBody = '${SCRIPT, template="regressionfailed.groovy"}'
def emailSubject = "${env.JOB_NAME} - Build# ${env.BUILD_NUMBER} - ${env.BUILD_STATUS}"
emailext(mimeType: 'text/html', replyTo: 'xxxx', subject: emailSubject, to: 'xxxx', body: emailBody)

Remember you might still need to redo parts of your template.

like image 6
Martin Avatar answered Oct 17 '22 03:10

Martin


I am surprised that nobody pointed out the fundamental issue with the error reported by OP. The error is coming from the Groovy compiler itself and it is coming because ${SCRIPT...} appeared inside double-quotes making it a GString (an invalid one). To fix the error mentioned in OP, you just have to use single-quotes instead:

    emailext mimeType: 'text/html', replyTo: 'xxxx', subject: "${env.JOB_NAME} - Build# ${env.BUILD_NUMBER} - ${env.BUILD_STATUS}", to: 'xxxx', body: '${SCRIPT, template='regressionfailed.groovy'}'

You can reproduce the error even using the standalone Groovy interpreter like this:

$ cat << 'END' > /tmp/t.groovy
> def emailext(Map opts) {
> }
>
> emailext mimeType: 'text/html', replyTo: 'xxxx', subject: "${env.JOB_NAME} - Build# ${env.BUILD_NUMBER} - ${env.BUILD_STATUS}", to: 'xxxx', body: "${SCRIPT, template='regressionfailed.groovy'}"
> END
$ groovy /tmp/t.groovy
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/private/tmp/t.groovy: 4: unexpected token: SCRIPT @ line 4, column 150.
   TATUS}", to: 'xxxx', body: "${SCRIPT, te
                                 ^

1 error
like image 2
haridsv Avatar answered Oct 17 '22 03:10

haridsv