Issue
I am currently working on sending custom email notifications from jenkins build pipeline designed using declarative syntax.I have Config File Provider Plugin 3.8.0, Email Extension Plugin 2.82 & Email Extension Template Plugin 1.2
installed on my jenkins server.
I have also done global system configurations(smtp creds, default values etc.) required for Email Extension Plugin to work.
Then I created a simple groovy template script and added it as managed file on Jenkins using Config File Provider Plugin with name my-email-template. While adding file I selected file type as Extended Email Publisher Groovy Template.
Then I added below steps in my Jenkinsfile,
steps {
emailext body: '''${SCRIPT, template="managed:my-email-template"}''',
mimeType: 'text/html',
subject: "[Jenkins] ${currentBuild.fullDisplayName}",
to: "[email protected]"
}
Through above configurations I received a mail with body as;
Groovy Template file [managed:my-email-template] was not found in $JENKINS_HOME/email-templates.
As per official Email Extension Plugin documentation, I have used managed: prefix for my template file referrence, so I was expecting it to be referring to the managed file configured, but it's still searching the file under jenkins home.
Can someone help me with this?
Solution
By default email extension plugins looks inside ${JENKINS_HOME}/email-templates/ directory. If you have a script named "my-email-template" and not using Config File Provider Plugin , then you would put your script in the ${JENKINS_HOME}/email-templates/ directory and use it in below way:
stage("Send Email")
{
steps {
emailext body: '''${SCRIPT, template="my-email-template"}''', subject:
'Email subject ', to: '[email protected]'
}
}
In case , the email extension plugin in unable to find the template file, then it will always give the error as below irrespective if you are working with managed plugins:
Groovy Template file [my-email-template.template] was not found in $JENKINS_HOME/email-templates.
Using Config File Provider Plugin and managed: prefix :
Step 1 : Got to Manage Jenkins -> Managed Files
Step 2 : Click -> Add a new config
Step 3 : Select Type as Extended Email Publisher Groovy Template and click submit
Step 4 : Give Name of your file as your choice and add content of your template file.Ensure you copy this name as it will be used in the pipeline. Click on submit.
Step 5 : Add code in your pipeline. Ensure you put the correct Name "managed:Groovy Email Template"
stage("Send Email")
{
steps {
emailext body: '''${SCRIPT, template="managed:Groovy Email Template"}''', subject:
'Email subject ', to: '[email protected]'
}
}
The above should run and you can see the contents of the template file at below path :
${JENKINS_HOME}/org.jenkinsci.plugins.configfiles.GlobalConfigFiles.xml
Note:
If the above method does not work then please restart jenkins / reinstall the plugin email extension plugin, as done by vsbehere as the method is correct.
Answered By - Altaf