Search code examples
jenkinsgroovyjenkins-groovyemail-extjenkins-email-ext

My variable is not interpolated in the body on emailext


This is a piece of my Jenkinsfile:

emailext([
        attachmentsPattern: '**/build/*.log',
        subject: 'Hello World',
        recipientProviders: [developers(), requestor()],
        to: '$DEFAULT_RECIPIENTS',
        body: '''
            <p>Test summary:</p>
            <ul>
                <li><a href="${BUILD_URL}">Status: ${BUILD_STATUS}</a></li>
                <li><a href="${BUILD_URL}/flowGraphTable">Executed steps</a></li>
                <li><a href="${BUILD_URL}/parsed_console">Errors and warnings</a></li>
                <li>Test results:</li>
                ${testResults}
            </ul>
        ''',
        mimeType: 'text/html'
])

But unfortunately testResults variable doesn't get interpolated:

email

How can I fix?

Also, I have tried to replace ''' with """, like this:

emailext([
        attachmentsPattern: '**/build/*.log',
        subject: 'Hello World',
        recipientProviders: [developers(), requestor()],
        to: '$DEFAULT_RECIPIENTS',
        body: """
            <p>Test summary:</p>
            <ul>
                <li><a href="${BUILD_URL}">Status: ${BUILD_STATUS}</a></li>
                <li><a href="${BUILD_URL}/flowGraphTable">Executed steps</a></li>
                <li><a href="${BUILD_URL}/parsed_console">Errors and warnings</a></li>
                <li>Test results:</li>
                ${testResults}
            </ul>
        """,
        mimeType: 'text/html'
])

But I got this error in my Jenkins console output:

Error when executing always post condition:
Also:   org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: 4fb4675f-4b24-4e5a-8344-835e9d514202
groovy.lang.MissingPropertyException: No such property: BUILD_STATUS for class: groovy.lang.Binding

Jenkins version is: 2.441


Solution

  • These are two different errors.

    The first one is caused by the difference between the single and the double quotes in Groovy, that are well described in String interpolation section of Jenkins documentation.

    The second one obviously means that you don't have the BUILD_STATUS variable defined. It may depend on the type of the pipeline and Jenkins version, but here's a couple things I'd recommend to check:

    • AFAIR, Jenkins doesn't expose the build status as en environment variable like BUILD_URL, you have to use a Groovy variable currentBuild.result or something like that;
    • you might want to refer the environment variables as ${env.VARIABLE_NAME} because it will be a Groovy variable then - ${VARIABLE_NAME} might be not defined in this context simply because it's not a shell command. This works differently in usual pipelines and multibranch ones, so without more details it's impossible to suggest a precise solution.

    Given that, I would try the following:

    emailext([
            attachmentsPattern: '**/build/*.log',
            subject: 'Hello World',
            recipientProviders: [developers(), requestor()],
            to: $DEFAULT_RECIPIENTS,
            body: """
                <p>Test summary:</p>
                <ul>
                    <li><a href="${env.BUILD_URL}">Status: ${currentBuild.result}</a></li>
                    <li><a href="${env.BUILD_URL}/flowGraphTable">Executed steps</a></li>
                    <li><a href="${env.BUILD_URL}/parsed_console">Errors and warnings</a></li>
                    <li>Test results:</li>
                    ${testResults}
                </ul>
            """,
            mimeType: 'text/html'
    ])