Summary:
in Pre-send Script:
1.1 debug dumping build (and trigger/msg/other objects)
NOTE that THIS trigger object is the email trigger (e.g. "Always" or "Failure - Any") NOT the build trigger (AKA build cause).
1.2 check build object for trigger(cause) of build
1.3 set recipient email address
Starting with the doc for . . .
Email-ext plugin in Post-build Actions - Editable Email Notification - Advanced
In Pre-send Script . . .
https://wiki.jenkins-ci.org/display/JENKINS/Email-ext+plugin#Email-extplugin-PresendScript
"""The pre-send script is a feature which allows you to write a script that can modify the MimeMessage object prior to sending. This would allow adding custom headers, modifying the body, etc. Predefined variables include:
- msg - the MimeMessage object which can be modified
- logger - a PrintStream and will write to the job's log.
- build - the build this message belongs to
- cancel - a boolean, which when set to true will cancel the sending of the email
"""
Also available:
- trigger - the email trigger, NOT the build trigger
1.1
THIS perl Data::Dumper equivalent for debugging java objects is useful.
http://www.perlmonks.org/?node_id=699918
Try this in the Email-Ext - Pre-send Script of a jenkins job:
logger.println "MSG: " + msg;
logger.println "ENV: " + msg;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
XStream dumper = new XStream(new JsonHierarchicalStreamDriver());
logger.println "TRIGGER: " + trigger;
logger.println(dumper.toXML(trigger));
logger.println "BUILD: " + build;
logger.println(dumper.toXML(build));
See the partial result (not showing much of the build object). The trigger here is the email "AlwaysTrigger" but we can see that the build action cause was the admin user:
TRIGGER: hudson.plugins.emailext.plugins.trigger.AlwaysTrigger@431ff400
{"hudson.plugins.emailext.plugins.trigger.AlwaysTrigger": {
"email": {
"recipientList": "",
"subject": "$PROJECT_DEFAULT_SUBJECT",
"body": "$PROJECT_DEFAULT_CONTENT",
"recipientProviders": [
{},
{}
],
"attachmentsPattern": "",
"attachBuildLog": false,
"compressBuildLog": false,
"replyTo": "$PROJECT_DEFAULT_REPLYTO",
"contentType": "project"
}
}}
BUILD: custom_email_test_gunk #14
{"hudson.model.FreeStyleBuild": {
"actions": [
{
"default": {},
"int": 4,
"hudson.model.CauseAction": {
"causeBag": [
[
{
"userId": "admin"
},
1
]
]
},
"hudson.scm.CvsRevisionState": {
.
.
From a run with build triggered by Script Trigger we can see the difference in the build object, the build action/cause has ScriptTrigger details:
BUILD: custom_email_test_gunk #10
{"hudson.model.FreeStyleBuild": {
"actions": [
{
"default": {},
"int": 6,
"org.jenkinsci.plugins.scripttrigger.ScriptTriggerRunAction": {
"description": "\n[jenkins] $ /bin/sh -xe /tmp/hudson6220381111236932099.sh\n+ set +x\nJOB_NAME=custom_email_test_gunk\nJOB_VARS_CMD=JUNK=custom_email_test_;SYSTESTNAME=Docker;PRODUCT=gunk;REL=LATEST;DRIVER=gunk;SUT=gunk;SUSER=\nPRODUCT=gunk REL=LATEST DRIVER= SUT= SYSTESTNAME=Docker SUSER=\n./system_test/regression/run_scripts/poll_stuff.sh STDIR=yadda yada yadda \ndiff: yadda yadda \n<description>"
},
"org.jenkinsci.lib.xtrigger.XTriggerCauseAction": {
"build": {
"@class": "hudson.model.FreeStyleBuild",
"@reference": "../../../.."
}
},
"hudson.model.CauseAction": {
"causeBag": [
[
{
"triggerName": "ScriptTrigger",
"causeFrom": " and <description>\n[jenkins] $ /bin/sh -xe /tmp/hudson6220381111236932099.sh\n+ set +x\nJOB_NAME=custom_email_test_gunk yadda yadda ",
"logEnabled": true
},
1
]
]
},
"hudson.scm.CvsRevisionState": {
.
.
1.2
TODO: work out the bit of logic to differentiate between build cause.
I'm working on this myself.
1.3
We can set recipients something like this in the Pre-send Script:
import javax.mail.Message.RecipientType
msg.setRecipients(RecipientType.TO, '[email protected]')