如何更改 Email-ext 插件以从自定义位置加载脚本

How to change Email-ext plugin to load script from custom location

我喜欢将我的 Jenkins 电子邮件脚本提交到我的工作副本,并将其与 Email-ext 一起使用。

所以我写了类似的东西:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building...'
            }
        }
    }
    post {
        always {
            echo 'Sending email...'
            emailext body: '''${SCRIPT, template="${WORKSPACE}\Src\Scripts\Jenkins\groovy-html2.template"}''',
            mimeType: 'text/html',
            subject: "[Leeroy Jenkins] ${currentBuild.fullDisplayName}",
            to: "user@company.com",
            replyTo: "user@company.com",
            recipientProviders: [[$class: 'CulpritsRecipientProvider']]
        }
    }
}

但我收到以下邮件: Groovy 在 $JENKINS_HOME/email-templates.

中找不到模板文件 [${WORKSPACE}SrcScriptsJenkinsgroovy-html2.template]

通过使用命令行手动覆盖文件来解决它:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building...'
            }
        }
    }
    post {
        always {
            echo 'Sending email...'

            bat "copy /Y ${WORKSPACE}\Src\Scripts\Jenkins\groovy-html2.template \"${JENKINS_HOME}\email-templates\groovy-html2.template\""

            emailext body: '''${SCRIPT, template="${WORKSPACE}\Src\Scripts\Jenkins\groovy-html2.template"}''',
            mimeType: 'text/html',
            subject: "[Leeroy Jenkins] ${currentBuild.fullDisplayName}",
            to: "user@company.com",
            replyTo: "user@company.com",
            recipientProviders: [[$class: 'CulpritsRecipientProvider']]
        }
    }
}

有点粗糙,但它有效。

出于安全原因,模板必须位于正确的目录中。如果您想将它们保存在 SCM 中,我建议您创建一个 Jenkins 作业,将 SCM 签出到正确的目录中。从技术上讲,虽然该目录不应该是可写的,但可能是。或者,您可以在管道本身中使用 groovy 代码

在 Jenkins 2.190 上,使用本地工作区文件作为果冻模板效果很好。它在构建期间通过 SCM 步骤拉取,并收到正确的 html 内容。

这是我的电子邮件分机配置的默认内容

${JELLY_SCRIPT,template="${WORKSPACE}/some_dir/email_template.jelly"}