Accessing custom environment variables in a jelly pattern

With Jenkins pipeline, you can set any environment variable through the global variable - env .

The Jelly template, in turn, gives you access to the Jenkins API, including the hudson.model.AbstractBuild and hudson.model.AbstractProject objects .

Here are the snippets I'm using:

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 I was unable to access my custom variable from the Jelly template.

I've tried many possible impossible options (with pipeline stepEnv, call several other methods from AbstractBuild class (getEnvironments, getBuildVariables), but nothing.

+3


source to share


1 answer


The only solution I have found is this:



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

      

+2


source







All Articles