在Jelly模板中访问自定义环境变量 [英] Access custom environment variables in jelly template
问题描述
借助Jenkins管道,您可以通过名为 env 的全局变量来设置任何环境变量.
With Jenkins pipeline you are able to set any environment variable through Global Variable called - env.
果冻模板则使您能够访问Jenkins API,包括 hudson.model.AbstractBuild 和 hudson.model.AbstractProject 对象.
Jelly template in it's turn gives you ability to access Jenkins API including hudson.model.AbstractBuild and hudson.model.AbstractProject objects.
以下是我使用的摘录:
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;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>
问题是我无法从Jelly模板访问我的自定义变量.
The problem is that I couldn't get access to my custom variable from the Jelly template.
我尝试了很多可能的不可能的选择(使用Env管道步骤,从AbstractBuild类调用其他几种方法(getEnvironments,getBuildVariables),但是什么也没有.
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>
这篇关于在Jelly模板中访问自定义环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!