A Jenkins plugin, to poll an email inbox, and trigger jobs based on new emails.
Additional:
The poll-mailbox-trigger is a Jenkins "build trigger" plugin, which allows a Job to poll an email inbox. When an unread email is found, that matches the configured criteria, it:
- marks the email as read, so that it is not reprocessed
- triggers a new job
Once the plugin is installed, you can find it's configuration under YourJob > Configure > Build Triggers > Poll Mailbox Trigger. You should see something like this screenshot.
From here you can configure:
- The email server and account to search for UNREAD emails - here are some sample configurations for popular email services
- Additional filters, including:
- folder - the email folder to check (default "INBOX")
- subjectContains - the email must inlcud this subject (default "jenkins >")
- receivedXMinutesAgo - the email must have been received within the last X minutes (default "1440" which is 24 hours)
- More configuration information is available here
- The polling schedule - defaults to every 5 minutes
Once configured, there is an option to Test Connection. This will tell you:
- If there are any errors - please refer to the Troubleshooting section
- If the plugin can connect to the mail server successfully
- How many emails it can find, that match the given criteria
Hit save, and you should be done.
- There will now be a "View Polling Log" option under your Job, so that you can see when the polling was last checked.
- The plugin will now start a new job instance, for each unread email it finds. Once an email has triggered a job, the email will then be marked as READ, so that it doesn't invoke additional job instances in subsequent polling rounds.
Compatibility | Since Version |
---|---|
Java Runtime Environment | 1.7+ |
Jenkins Server | 2.7.1 LTS |
Mail Server Protocols | IMAP, IMAPS, POP3, POP3S |
Mainly, because I want to be able to (re)trigger a (failing) build, from the comfort of my home/beach/pub. I may not always have direct/sychronous access to the build server (due to firewalls, network access, etc). I'm already being notified by email when a job fails, why can't I just send an email response saying "retry"?
If you're working in a corporate environment, and are lucky enough to have a build server there's probably a very small chance that the build server is also exposed to the outside world (without using a VPN).
Email is:
- prevalent - accessible pretty much anywhere
- convenient - it is built into my mobile phone
- asychronous - I can fire it now and let it get picked up later
- adopted - it's already being used to notify me of failed builds
Also, some side notes:
- I haven't met a Jenkins interface for mobile devices that I like.
- Email To SMS Gateways exist, for those that don't have Email on their mobile phones.
- Polling is not ideal
Alternatives to polling?
- Utilising Push-IMAP
- Installing a sendmail/postfix server, forward emails to it, and write a perl script to process incoming emails.
Prerequisites:
- JDK 6 (or above)
To setup for use with Intellij IDEA
./gradlew cleanIdea idea
To run Jenkins (http://localhost:8080) locally with the plugin installed:
./gradlew clean server
To build the Jenkins plugin (.jpi) file:
./gradlew clean jpi
To publish/release the Jenkins plugin:
- Update the
version
ingradle.properties
, to remove "-SNAPSHOT" (increment and re-add after publishing) - Setup the
~/.jenkins-ci.org
credentials file as per the instructions here https://wiki.jenkins-ci.org/display/JENKINS/Gradle+JPI+Plugin - Ensure gradle is using
JDK7
(for dist compatibility) -./gradlew --version
- Then run the command:
./gradlew clean publish
- (todo) automate github version increment (and above steps?)
Screenshot - Version 1.022
The Host field, allows you to enter the DNS name/hostname/IP Address of the server, hosting the email account service.
The Username field, allows you to enter the username required to connect to this email account.
The Password field, allows you to enter the password (N.B. or application password) required to connect to this email account.
The Advanced Email Properties field, allows you to configure the plugin, using standard key=value property notation.
You can override the following default property values:
# Connection configuration
storeName=imaps
# Search configuration
folder=INBOX
subjectContains=jenkins >
receivedXMinutesAgo=1440
# JavaMail configuration
mail.debug=true
mail.debug.auth=true
mail.imaps.host=<above_host>
mail.imaps.port=993
You can also add java imap properties, to further configure the connection.
Below are some sample configurations for common web based email services:
For google passwords, go to "Google account > security > app passwords".
host=imap.gmail.com
username=<your_email>@gmail.com
password=<your_application_password>
For google passwords, go to "Google account > security > app passwords".
host=pop.gmail.com
username=<your_email>@gmail.com
password=<your_application_password>
storeName=pop3s
For hotmail passwords, go to "Account Settings > Security Info > Create a new app password".
host=imap-mail.outlook.com
username=<your_email>@hotmail.com
password=<your_application_password>
host=<your_mail_server>
username=<your_email>
password=<your_password>
host=<your_mail_server>
username=<your_email>
password=<your_password>
mail.imap.ssl.enable=true
mail.imap.starttls.enable=true
host=<your_imap_host_server>
username=<your_email>
password=<your_password>
mail.imaps.port=993
mail.imap.ssl.enable=true
mail.imap.starttls.enable=true
If you've managed to successfully setup a connection,
please contact me, and I'll update this section.
The following build parameters, are injected into the job (sample values provided):
Name | Sample value |
---|---|
pmt_content | <the email's content> |
pmt_contentType | multipart/ALTERNATIVE; boundary=1234 |
pmt_flags | SEEN |
pmt_folder | INBOX |
pmt_from | Me <Sender@gmail.com> |
pmt_headers | Delivered-To=Foobar@gmail.com etc |
pmt_messageNumber | 14 |
pmt_receivedDate | 2014-10-14T12:19Z |
pmt_recipients | Foobar@gmail.com |
pmt_replyTo | Me <Sender@gmail.com> |
pmt_sentDate | 2014-10-14T12:19Z |
pmt_subject | Jenkins > My Job |
Name | Sample value |
---|---|
pmt_mail.debug | TRUE |
pmt_mail.debug.auth | TRUE |
pmt_mail.imaps.host | imap.gmail.com |
pmt_mail.imaps.port | 993 |
pmt_host | imap.gmail.com |
pmt_username | Foobar@gmail.com |
pmt_storeName | imaps |
pmt_receivedXMinutesAgo | 1440 |
pmt_subjectContains | Jenkins > |
Name | Sample value |
---|---|
pmt_jobTrigger | My Job |
N.B. Please note, these variables are injected into the triggered build (only). No other actions (implied or assumed) are taken. (e.g. No jenkins node filtering is performed, unless you've configured the job to do so. No additional jobs are triggered, unless you've configured the job to do so. And so on.)
-
Setup a "Mail Trigger" job, which builds other jobs based on the "pmt_jobTrigger" value.
-
Setup an editable Email notification on the target job, so that it emails you when a job has failed.
-
Include the following html, in the editable Email job configuration.
<a href="mailto:<jenkins_poll_mailbox>@gmail.com?subject=Jenkins > $JOB_NAME">Retry $JOB_NAME</a>
That way, when the job fails, it will email you a nice "Retry My Job" link. Clicking on it, opens a new email, which you can just send straight away (good for mobile phones).
As of version 0.15, you can pass addition custom job parameters to the new build, by simply putting key=value property
notation into the content of your email. N.B. both text/plain
and text/html
content types are supported. Emails
with attachments have not yet been tested.
For example:
Hi Jenkins,
Please execute the job with these parameters:
fruit=banana
veg=carrot
email=foobar@abc.com
--
Kind regards,
Nick
Will inject the following job parameters into the new job instance:
Name | Value |
---|---|
fruit | banana |
veg | carrot |
foobar@abc.com |
###1a. Error : javax.mail.MessagingException: Connection timed out: connect; ###1b. Error : javax.mail.MessagingException: Connection refused;
Solution: Check the Jenkins server can access the email server and port, by running the command (from the Jenkins server):
telnet <your_host> <your_port_143_or_993>
If you get a message similar to the following, then there is no way to create a direct connection to the mail server - probably the network is down, or the connection has been (a: passively / b: actively) blocked by a firewall. If so, please check your network settings with your network administrator. You may need to specify SOCKS proxy details, in the Advanced Email Properties.
Connecting To imap.gmail.com...Could not open connection to the host, on port 993: Connect failed
###2. Error : javax.mail.AuthenticationFailedException: [AUTHENTICATIONFAILED] Invalid credentials (Failure)
Solution: Check the credentials you're using are correct.
###3. Error: javax.mail.MessagingException: com.ibm.jsse2.util.j: PKIX path building failed: java.security.cert.CertPathBuilderException: PKIXCertPathBuilderImpl could not build a valid CertPath.; ...
Solution: To ignore certificate verification errors, you can use the following config property:
mail.imaps.ssl.trust=*
Warning: it's not advisable to ignore certificate verification errors (unless perhaps in a test environment): this defeats the point of using SSL/TLS. Instead, if you know you trust that server certificate, import it in your trust store, and specify the location of the trust store using:
javax.net.ssl.trustStrore=/path/to/cacerts.jks
###4. Error : java.lang.NullPointerException at org.jenkinsci.plugins.pollmailboxtrigger.PollMailboxTrigger.initialiseDefaults(PollMailboxTrigger.java:98)
Solution: I'm not quite sure what the cause is! If you're able to reproduce the issue, please contact me with instructions. In the meantime, the error is caught and the following message is displayed.
Could not get Jenkins instance using Jenkins.getInstance() (returns null). This can happen if Jenkins
has not been started, or was already shut down. Please see
http://javadoc.jenkins-ci.org/jenkins/model/Jenkins.html#getInstance() for more details. If you believe
this is an error, please raise an 'issue' under
https://wiki.jenkins-ci.org/display/JENKINS/poll-mailbox-trigger-plugin.
###5. Error : java.net.UnknownHostException: imaps.gmail.com
Solution: The hostname provided doesn't exist in the DNS servers. Please check that it is correct.
###6. Error : Please set the 'folder=XXX' parameter to one of the following values:
Solution: The provided folder name doesn't exist on the folder, please use one of the values listed.
Want to say thanks but can't find the words? Coffee donations are VERY welcome!
Nick Grealy - nickgrealy@gmail.com
Licensed under the MIT License (MIT)