Alex Weitz
Alex Weitz

Reputation: 3399

Jenkins Pipeline - emailext Does not Resolve env Variable in Template

In my jenkinsfile I defined an environment variable:

env.PACKAGES = htmlPackageList

I tried accessing it from the template with ${PACKAGES} and ${env.PACKAGES}, but both times I just receive the email with a plain string.

template.html:

<pre style='line-height: 10px; color: green; font-size: 20px; white-space: pre; margin: 1em 0; display: block;'>$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS.<br/> </pre>
<br/>
Check console <a href="$BUILD_URL">Build URL</a> to view full results.<br/>
<br/>
<h4>Change list:</h4>
<div style="padding-left: 30px; padding-bottom: 15px;">
    ${CHANGES, showPaths=true, format="<div><b>%a</b>: %r %p </div><div style=\"padding-left:30px;\"> &#8212; &#8220;<em>%m</em>&#8221;</div>", pathFormat="</div><div style=\"padding-left:30px;\">%p"}
</div>
--<br/>

<h4>Published Packages:</h4>
${PACKAGES}

The email is invoked by:

success {
    script{
        emailext body: ('${FILE,path="Jenkins/Templates/template.html"}'),
        to: "${env.EmailTo}",
        subject: '[Jenkins] [$PROJECT_NAME] - [Build # $BUILD_NUMBER] - [$BUILD_STATUS]',
        mimeType: 'text/html'
     }
}

Upvotes: 1

Views: 1388

Answers (1)

Milind Technology
Milind Technology

Reputation: 11

The below declaration in template(groovy) can fetch all the env variables from pipeline

<% def envEnviron = it.getAction("org.jenkinsci.plugins.workflow.cps.EnvActionImpl").getEnvironment()
    println(envEnviron)
%\>

The below also can be used to get only the overridden variables in pipeline

<% def envOveriddenEnviron = it.getAction("org.jenkinsci.plugins.workflow.cps.EnvActionImpl").getOverriddenEnvironment()
    println(envOveriddenEnviron)
%\>

Upvotes: 1

Related Questions