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

被刻印的时光 ゝ 提交于 2019-12-14 03:49:16

问题


I am trying to find a way to make use of a groovy variable in stead of using a hardcoded template name

Current code looks like: '${SCRIPT, template="groovy-html.template"}' and I tried to use nested variable expansion but I still got an error.

Example:

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

Groovy Template file [${template}] was not found in $JENKINS_HOME/email-templates. Generated using text.jelly template.

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


回答1:


def sendEmail(String subject, String template)
{
   def bodyContent = "\${SCRIPT, template=\"${template}\"}"

   emailext  subject: "${subject}",
             body: bodyContent,
             recipientProviders: [[$class: 'RequesterRecipientProvider']]
}

The above worked for me.




回答2:


You may try changing

From:

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

To:

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



回答3:


It took me some time to realise that the SCRIPT expansion is not made by Groovy itself but by emailex function.

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

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

After this, when I pass the body argument to the emailext, it will be expanded correctly.



来源:https://stackoverflow.com/questions/46326920/how-to-use-groovy-template-engine-when-template-name-is-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!