jenkinsjenkins-pipelinejenkins-pluginsemail-extjenkins-email-ext

Jenkins: Passing user defined variables to Email-Ext plugin


I'm using the Email-Ext plugin to send a results email at the end of my build in the post build stage of a declarative pipeline. In the body of the email I'm trying to insert some environment variables and some user defined environment variables.

The built in variables show up fine, however any user defined variables I define are just empty in the body of the email or don't work in the attachmentPattern field either:

My environment variables:

pipeline {
    agent any
    environment {
        buildFolder = "build_${BUILD_NUMBER}" 
        robotFolder = "build_${BUILD_NUMBER}/robotDemo" 
        serverName = "abc123"
        robotResults = "${buildFolder}/*.txt"
    }

The email-ext format I am using in my post{} build:

emailext attachLog: true, attachmentsPattern: 'build_${env.BUILD_NUMBER}/Robot_Results.txt', body: '<b>Job Name:</b> ${ENV, var="JOB_NAME"}<br><b>Build Number:</b> ${ENV, var="BUILD_NUMBER"}<br><br><b>Build URL:</b><br>${ENV, var="BUILD_URL"}<br><br><b>Log Files:<br><br><br><pre>${BUILD_LOG_EXCERPT, start="^====", end="^report.html"}</pre><pre>${ENV, var="serverName"}</pre>', mimeType: 'text/html', subject: 'ABS Results', to: 'richard.scott@sap.com'

For example: ${ENV, var="JOB_NAME"}, shows up fine. However ${ENV, var="serverName"} doesn't show at all.

I've tried variations such as...

${serverName}
${env.serverName}

...but they don't work either.

Any ideas how to use user defined environment variables like 'serverName' above in the email-ext plug-in?

Thanks & best regards, Richard.


Solution

  • You’re using single quotes, but only double quotes allow variables to be interpolated.

    I would suggest you to use a Groovy script for rendering the email contents.

    You can either use the predefined Groovy script like this:

    emailext body: '''${SCRIPT, template="groovy-html.template"}'''
    

    Or add your own template like described in the documentation. I couldn't figure our if it's possible to load that template from the workspace, but you could try it with:

    emailext body: '''${SCRIPT, template="$WORKSPACE/groovy-html.template"}'''
    

    The syntax for getting values from the environment or parameters when interpolating is:

    ${env.MY_ENVVAR}
    ${param.MY_BUILDPARAM}
    

    (env and param is ja Groovy map holding all the values)

    And for all other variables:

    ${varName}