0
votes

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.

3

3 Answers

1
votes

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

1
votes

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"}
0
votes

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.