如何从Jenkins管道脚本发送自定义模板化电子邮件?
我希望我的Jenkins管道作业在失败时发送自定义电子邮件报告。发送自定义电子邮件非常简单:如何从Jenkins管道脚本发送自定义模板化电子邮件?,jenkins,groovy,jenkins-pipeline,Jenkins,Groovy,Jenkins Pipeline,我希望我的Jenkins管道作业在失败时发送自定义电子邮件报告。发送自定义电子邮件非常简单: mail( to: 'recipient@example.com', from: 'no-reply@example.com', replyTo: 'no-reply@example.com', subject: 'your custom jenkins report', body: 'custom report here', ) 但是,我希望呈现某种模板并将其注入消息体。我
mail(
to: 'recipient@example.com',
from: 'no-reply@example.com',
replyTo: 'no-reply@example.com',
subject: 'your custom jenkins report',
body: 'custom report here',
)
但是,我希望呈现某种模板并将其注入消息体。我不在乎使用什么样的模板语言(Jinja、ERB、其他什么…?)。我想把我的模板也放在git存储库中。我该怎么做?我的想法是这样的:
checkout([
$class: 'GitSCM',
userRemoteConfigs: [[url: 'https://git.example.com/custom-report.git']],
branches: [[name: '*/master']],
])
reportBody = renderTemplate(file: 'custom-report/custom-report.template')
mail(
to: 'recipient@example.com',
from: 'no-reply@example.com',
replyTo: 'no-reply@example.com',
subject: 'your custom jenkins report',
body: reportBody,
)
我可以用一个(虽然不需要共享库;您可以直接将这些步骤放到管道中,但它确实使一些事情稍微方便一些)。我使用以下文件创建了一个全局共享库:
resources/report.txt.groovy
:
Hello from ${job}!
import groovy.text.StreamingTemplateEngine
def renderTemplate(input, variables) {
def engine = new StreamingTemplateEngine()
return engine.createTemplate(input).make(variables).toString()
}
vars/helpers.groovy
:
Hello from ${job}!
import groovy.text.StreamingTemplateEngine
def renderTemplate(input, variables) {
def engine = new StreamingTemplateEngine()
return engine.createTemplate(input).make(variables).toString()
}
然后,在我的管道中,我添加了以下步骤:
variables = [ job: currentBuild.rawBuild.getFullDisplayName() ]
template = libraryResource('report.txt.groovy')
report = helpers.renderTemplate(template, variables)
mail(
to: 'recipient@example.com',
from: 'no-reply@example.com',
replyTo: 'no-reply@example.com',
subject: 'your custom jenkins report',
body: report,
)
这将生成包含以下内容的电子邮件:
Hello from SIS Unix Automation Testing » myjob » master #29!
其中sisunix自动化测试»myjob»master
是我的多分支管道作业的全名
请注意,要使用此方法,您需要禁用沙箱或approve/whitelist脚本,否则将阻止StreamingTemplateEngine
的一些内部构件
StreamingTemplateEngine
的文档可用。我可以通过一个共享库来实现这一点(虽然不需要共享库;您可以直接将这些步骤放到管道中,但它确实使一些事情稍微方便一些)。我使用以下文件创建了一个全局共享库:
resources/report.txt.groovy
:
Hello from ${job}!
import groovy.text.StreamingTemplateEngine
def renderTemplate(input, variables) {
def engine = new StreamingTemplateEngine()
return engine.createTemplate(input).make(variables).toString()
}
vars/helpers.groovy
:
Hello from ${job}!
import groovy.text.StreamingTemplateEngine
def renderTemplate(input, variables) {
def engine = new StreamingTemplateEngine()
return engine.createTemplate(input).make(variables).toString()
}
然后,在我的管道中,我添加了以下步骤:
variables = [ job: currentBuild.rawBuild.getFullDisplayName() ]
template = libraryResource('report.txt.groovy')
report = helpers.renderTemplate(template, variables)
mail(
to: 'recipient@example.com',
from: 'no-reply@example.com',
replyTo: 'no-reply@example.com',
subject: 'your custom jenkins report',
body: report,
)
这将生成包含以下内容的电子邮件:
Hello from SIS Unix Automation Testing » myjob » master #29!
其中sisunix自动化测试»myjob»master
是我的多分支管道作业的全名
请注意,要使用此方法,您需要禁用沙箱或approve/whitelist脚本,否则将阻止StreamingTemplateEngine
的一些内部构件
StreamingTemplateEngine
的文档可用