Access custom environment variables in jelly email-ext template

Jenkinsfile:

node { 
       env.MYVAR = 'My variable'
       emailext body: ${JELLY_SCRIPT, template="myTemplate"}, subject: 'MySubject', to: 'me'
}

myTemplate:

<?jelly escape-by-default='true'?>
<!DOCTYPE html [
        <!ENTITY nbsp "&#38;#38;nbsp&#59;">
        ]>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define">
    <head>
        <style>
            body table, td, th, p, h1, h2 {
            margin:0;
            font:normal normal 100% Georgia, Serif;
            background-color: #ffffff;
            }
        </style>
    </head>
    <body>
        <j:set var="buildEnv" value="${build.getEnvironment(listener)}" />
        <j:set var="myVar" value="${buildEnv.get('MYVAR')}" />
        <table>
            <tr>
                <td>Variable</td>
                <td>
                    <p>${myVar}</p>
                </td>
            </tr>
        </table>
    </body>
</j:jelly>

The problem is that I couldn’t get access to my custom variable from the Jelly template.

I have tried a lot of possible ana impossible options (withEnv pipeline step, call several other methods from AbstractBuild class (getEnvironments, getBuildVariables), but nothing.

tried this solution also:

<j:set var="myvar" value="${it.getAction('org.jenkinsci.plugins.workflow.cps.EnvActionImpl').getOverriddenEnvironment()}"/>
<p>My var: ${myvar}</p>

could you please help me guys

Thanks,
Satya

Can you try this? (note the added double quotes around the body info).

node { 
       env.MYVAR = 'My variable'
       emailext body: "${JELLY_SCRIPT, template='myTemplate'}", subject: 'MySubject', to: 'me'
}

Also, to access environment variables in the template, you can use the ENV token.

${ENV,var="VARIABLENAME"}

<?jelly escape-by-default='true'?>
<!DOCTYPE html [
        <!ENTITY nbsp "&#38;#38;nbsp&#59;">
        ]>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define">
    <head>
        <style>
            body table, td, th, p, h1, h2 {
            margin:0;
            font:normal normal 100% Georgia, Serif;
            background-color: #ffffff;
            }
        </style>
    </head>
    <body>
        <table>
            <tr>
                <td>Variable</td>
                <td>
                    <p>${ENV,var="MYVAR"}</p>
                </td>
            </tr>
        </table>
    </body>
</j:jelly>

Thanks for quick response @slide_o_mix

im not able to parse jelly script JellyException: Could not parse Jelly script : null
error in this line

${ENV,var=“MYVAR”}

Now that I look at this again after looking at the source code. I don’t think there is a good way to get at the environment variables from the template. If you are using pipeline, then AbstractBuild can’t be used at all since pipeline doesn’t inherit from AbstractBuild. This would require an update to the email-ext plugin to work I think.

The only thing I can think of would be to call Run<?,?>.getEnvironment(TaskListener listener) which you should be able to call on the build object that is part of the Jelly script mapping. I am very rusty with Jelly, so I can’t help on how you might do that as it doesn’t look like a TaskListener is set as part of the binding for the Jelly script.

Can you describe your use case a little, perhaps there is another way to accomplish what you want to do.

@slide_o_mix My use case is simple

I’m conducting end-to-end tests and want to send email notification for the passed and failed tests count. The email should also include the test results, the number of servers alive before and after testing, and some environment variables in the template. Unfortunately, Gmail isn’t accepting CSS in my Groovy template then i choose jelly template.

How is gmail not accepting your CSS? The Groovy template output and Jelly output should produce the same thing (e.g. an html file), so switching from Groovy to Jelly probably won’t change the outcome at all.

@slide_o_mix sorry for late reply

When utilizing a Groovy script in Gmail, the CSS seems to be getting removed, whereas when employing Jelly, the CSS is displayed correctly. For further clarification, a screenshot has been provided.

There really should be no difference since both Groovy and Jelly are just being used to render the template into (in this case) html. It would be your setup that is somehow causing that to occur.

@slide_o_mix Raised a PR for Gmail no longer supports the Style tag.

@slide_o_mix how can i acess env values (or) build params in groovy template. could you please help me that

In groovy you can use them directly and they should resolve. Groovy scripts/templates have a missingMethod lookup that occurs when Groovy can’t find something in the binding. You could also use ENV(var="FOO") as token macros are supported that way as well.

Any examples?

Thanks,
Satya Kommula

Not that I know of. I don’t actually use this myself, but I used to be the maintainer for the plugin.

I saw the question on the PR to basil about accessing build environment variables in the templates and I am also trying to find a way to do that that as well. I’ve found similar questions on stack overflow but I haven’t had any luck with them: Jenkins Pipeline - emailext Does not Resolve env Variable in Template - Stack Overflow

<% def envEnviron = it.getAction("org.jenkinsci.plugins.workflow.cps.EnvActionImpl").getEnvironment()
    println(envEnviron)
%\>

This leads to the following error:

Exception raised during template rendering: Cannot invoke method getEnvironment() on null object

java.lang.NullPointerException: Cannot invoke method getEnvironment() on null object

The use case I have is a new ‘batch’ of functionality is created and an email is sent to a developer group informing them of the new version, the version name is built on the fly in the job. The job does not archive anything.

That means that that action is not found in the job. You may want to try dumping all the actions for your job with it.getAllActions() which you can see the javadoc for here: Actionable (Jenkins core 2.425 API)