I am trying to attach the template file using Jenkins pipeline, emailext. Variable (PROJNAME) is not accessible in the template file and I am receiving exceptions as an email:
Exception raised during template rendering: No such property: env for class: SimpleTemplateScript21 groovy.lang.MissingPropertyException: No such property: env for class: SimpleTemplateScript21 at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53) at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307) at SimpleTemplateScript21.run(SimpleTemplateScript21.groovy:1) at groovy.text.SimpleTemplateEngine$SimpleTemplate$1.writeTo(SimpleTemplateEngine.java:168) at groovy.text.SimpleTemplateEngine$SimpleTemplate$1.toString(SimpleTemplateEngine.java:180) at hudson.plugins.emailext.plugins.content.ScriptContent.renderTemplate(ScriptContent.java:151) at hudson.plugins.emailext.plugins.content.ScriptContent.evaluate(ScriptContent.java:82) at org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro.evaluate(DataBoundTokenMacro.java:208) at org.jenkinsci.plugins.tokenmacro.Parser.processToken(Parser.java:308) at org.jenkinsci.plugins.tokenmacro.Action$KiHW1UeqOdqAwZul.run(Unknown Source) at org.parboiled.matchers.ActionMatcher.match(ActionMatcher.java:96) at org.parboiled.parserunners.BasicParseRunner.match(BasicParseRunner.java:77) at org.parboiled.MatcherContext.runMatcher(MatcherContext.java:351)
Pipeline Script:
stage('Email') {
def mailRecipients = "[email protected]"
def jobStatus = currentBuild.currentResult
env.PROJNAME = 'project_name'
echo "projname is ${PROJNAME}"
emailext body: '''${SCRIPT, template="test.template"}''',
mimeType: 'text/html',
subject: "[Jenkins] ${jobStatus}",
to: "${mailRecipients}"
}
Template (filename - test.template):
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>Job is '${env.PROJNAME}'</p>
</body>
</html>
Also tried replacing the variable syntax in template file as "${PROJNAME}" and "${ENV, var="PROJNAME"}" but no luck. Any suggestions?
Didn't help when I replaced with ENV(var="PROJNAME") in template file. I received the email as:
Job is ENV(var="PROJNAME")
The only what worked for me in email template:
<%
import hudson.model.*
def YOUR_VARIABLE= build.getEnvVars()["SOME_BUILD_PARAMETER"];
%>
Then you can use
${YOUR_VARIABLE}