Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access custom environment variables in jelly template

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;#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>
</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.

like image 781
sshepel Avatar asked Oct 18 '22 12:10

sshepel


1 Answers

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>
like image 174
sshepel Avatar answered Nov 25 '22 14:11

sshepel