sanjiv
sanjiv

Reputation: 269

Jenkins pipeline as code email ext

I am using email ext syntax to send the email in pipeline as code and having an HTML template to send the email.

I need to access the Jenkins environment variable in HTML template. It is working for standard environment variables like BUILD_URL. But it is not working for environment variable set with env.VARIABLE='VALUE'. If I try to access ${VARIABLE} in HTML template it is not working.

Any idea please.

Upvotes: 1

Views: 1561

Answers (2)

user8616584
user8616584

Reputation: 11

Below text in Jenkins File worked for me

env.CURRENT_BRANCH = master
env.PIPELINE_TYPE = mytype

Below lines in Jelly\HTML file

Using the below variable all the environment variables defined in Jenkins can be accessible

${customEnvVar.PIPELINE_TYPE}-${customEnvVar.CURRENT_BRANCH}

Before this code customEnvVar needs to defined

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

Upvotes: 1

sshepel
sshepel

Reputation: 890

To get environment variables in your template, you should use some scripts to access Jenkins API.

In Jenkins there are two most popular options that I know: Groovy templates and Jelly templates.

Some more info you can find on Email-ext plugin page.

I have the same problem here: Access custom environment variables in jelly template, but I was able to get access to build parameters.

Here is a template how to do that with Jelly:

<?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('MY_BUILD_PARAMETER')}" />
  <table>
    <tr>
      <td>Variable</td>
      <td>
        <p>${myVar}</p>
      </td>
    </tr>
  </table>
</div>
</body>
</j:jelly>

So, you just need add some jelly tags, and to get build parameters value in this templates you need call getEnvironment method from build object.

<j:set var="buildEnv" value="${build.getEnvironment(listener)}" />
<j:set var="myVar" value="${buildEnv.get('MY_BUILD_PARAMETER')}" />

Upvotes: 1

Related Questions