user3360767
user3360767

Reputation: 986

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

I like to commit my Jenkins email script to my working copy and use it with Email-ext.

So I wrote something like :

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: "[email protected]",
            replyTo: "[email protected]",
            recipientProviders: [[$class: 'CulpritsRecipientProvider']]
        }
    }
}

But I get the following mail: Groovy Template file [${WORKSPACE}SrcScriptsJenkinsgroovy-html2.template] was not found in $JENKINS_HOME/email-templates.

Upvotes: 0

Views: 1769

Answers (3)

Noam Manos
Noam Manos

Reputation: 17040

On Jenkins 2.190, using a local workspace file as a jelly template, works well. It is pulled with SCM step during the build, and the correct html content is received.

This is the Default Content of my email-ext configuration:

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

Upvotes: 1

user3360767
user3360767

Reputation: 986

Solve it by manually overriding the file using command line:

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: "[email protected]",
            replyTo: "[email protected]",
            recipientProviders: [[$class: 'CulpritsRecipientProvider']]
        }
    }
}

A bit crude, but it works.

Upvotes: 0

David van Laatum
David van Laatum

Reputation: 654

Templates must live in the correct directory for security reasons. If you want to keep them in a SCM I suggest you create a Jenkins job that checks out that SCM to the correct directory. Technically though that directory shouldn't be writable but probably is. Alternatively you can use the groovy code in the pipeline itself

Upvotes: 1

Related Questions