电子邮件扩展插件未加载通过配置文件提供程序插件添加的 groovy 模板

时间:2021-05-18 13:00:50

标签: jenkins jenkins-pipeline jenkins-plugins jenkins-groovy jenkins-email-ext

我目前正致力于从使用声明式语法设计的 jenkins 构建管道发送自定义电子邮件通知。我在 jenkins 服务器上安装了 Config File Provider Plugin 3.8.0, Email Extension Plugin 2.82 & Email Extension Template Plugin 1.2

我还完成了电子邮件扩展插件工作所需的全局系统配置(smtp 凭据、默认值等)。

然后我创建了一个简单的 groovy 模板脚本,并使用名称为 my-email-template 的配置文件提供程序插件将其添加为 Jenkins 上的托管文件。添加文件时,我选择了文件类型为Extended Email Publisher Groovy Template

然后我在我的 Jenkinsfile 中添加了以下步骤,

steps {
  emailext body: '''${SCRIPT, template="managed:my-email-template"}''',
  mimeType: 'text/html',
  subject: "[Jenkins] ${currentBuild.fullDisplayName}",
  to: "abc@gmail.com"    
}

通过上述配置,我收到了一封邮件,正文为;

<块引用>

Groovy 模板文件 [managed:my-email-template] 未在 $JENKINS_HOME/email-templates。

根据官方 Email Extension Plugin documentation,我使用了 managed: 前缀作为我的模板文件引用,所以我希望它引用配置的托管文件,但它仍在搜索文件在 jenkins home 下。

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

默认情况下,电子邮件扩展插件会在 ${JENKINS_HOME}/email-templates/ 目录中查找。 如果您有一个名为“my-email-template”的脚本并且没有使用 Config File Provider Plugin ,那么您可以将您的脚本放在 ${JENKINS_HOME}/email-templates/ 目录中并在以下方式:

stage("Send Email")
   {
     steps {
                emailext body: '''${SCRIPT, template="my-email-template"}''', subject: 
                'Email subject  ', to: 'abc@gmail.com'
          }
   }

如果电子邮件扩展插件无法找到模板文件,那么无论您是否使用托管插件,它都会始终给出如下错误:

Groovy Template file [my-email-template.template] was not found in $JENKINS_HOME/email-templates.

使用配置文件提供程序插件和托管:前缀:

第 1 步:必须管理 Jenkins -> 托管文件
第 2 步:单击 -> 添加新配置
第 3 步:选择类型作为Extended Email Publisher Groovy Template,然后点击提交
第 4 步:选择您的文件名称并添加模板文件的内容。确保复制此名称,因为它将在管道中使用。点击提交。

enter image description here

第 5 步:在您的管道中添加代码。确保输入正确的名称 “managed:Groovy 电子邮件模板”

stage("Send Email")
   {
     steps {
                emailext body: '''${SCRIPT, template="managed:Groovy Email Template"}''', subject: 
                'Email subject  ', to: 'abc@gmail.com'
          }
   }

上面应该运行,你可以在下面的路径看到模板文件的内容:
${JENKINS_HOME}/org.jenkinsci.plugins.configfiles.GlobalConfigFiles.xml
注意
如果上述方法不起作用,请重新启动jenkins /重新安装插件电子邮件扩展插件,如vsbehere所做的那样,方法正确。

相关问题