With Jenkins pipeline you are able to set any environment variable through Global Variable called - env.
Jelly template in it's turn gives you ability to access Jenkins API including hudson.model.AbstractBuild and hudson.model.AbstractProject objects.
Here are the snippets that I use:
Jenkinsfile:
node { 
       env.MYVAR = 'My variable'
       emailext body: ${JELLY_SCRIPT, template="myTemplate"}, subject: 'MySubject', to: 'me'
}
Jelly template (myTemplate):
<?jelly escape-by-default='true'?>
<!DOCTYPE html [
    <!ENTITY nbsp "&#38;nbsp;">
]>
<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>
</div>
</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.
The only solution that I found is:
<j:set var="myvar" value="${it.getAction('org.jenkinsci.plugins.workflow.cps.EnvActionImpl').getOverriddenEnvironment()}"/>
<p>My var: ${myvar}</p>
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With