Access custom environment variables in jelly template

独自空忆成欢 提交于 2019-12-12 12:15:27

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/43950509/access-custom-environment-variables-in-jelly-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!