How to use the Groovy template engine when the template name is a variable?

I am trying to find a way to use the groovy variable instead of the name of a hard coded template

The current code is as follows: '${SCRIPT, template="groovy-html.template"}' , and I tried using the extension of the nested variables, but I still have an error.

Example:

 def body = '''${SCRIPT, template="${template}"}''' 

Groovy Template File [$ {template}] not found in $ JENKINS_HOME / Email Templates. Generated using the text.jelly template.

My call can be seen at https://github.com/pycontribs/powertape/blob/master/vars/notifyBuild.groovy#L47

+3
source share
3 answers
 def sendEmail(String subject, String template) { def bodyContent = "\${SCRIPT, template=\"${template}\"}" emailext subject: "${subject}", body: bodyContent, recipientProviders: [[$class: 'RequesterRecipientProvider']] } 

Worked above for me.

+1
source

You can try to change

From:

 def body = '''${SCRIPT, template="${template}"}''' 

To:

 def body = """${SCRIPT, template="$template"}""" 
0
source

It took me a while to realize that the SCRIPT extension is not performed by Groovy itself, but by the emailex function.

So the workaround was to prevent Groovy from expanding the outer layer:

 def body = "\${SCRIPT, template='${template}'}" 

After that, when I pass the body emailext argument, it will be decrypted correctly.

0
source

All Articles