Email a build notification to the BitBucket user who pushed a branch

452 views
Skip to first unread message

Mike Lehan

unread,
Jun 1, 2016, 8:11:32 AM6/1/16
to Jenkins Users
I spent a long time searching for a way to have Jenkins email whichever user pushed a branch (on BitBucket) with the notification of how the build for that branch progressed. I wasn't able to find anything so wrote my own solution and documented it on Stack Overflow.

http://stackoverflow.com/questions/37567901/setting-jenkins-to-email-a-build-notification-to-the-bitbucket-user-who-pushed-a

Given this is a Jenkins community I though it might be helpful to share it with you:

Question

A project repository has been successfully connected to a Jenkins server using the BitBucket plugin, and a project set up such that:
  • Each push to a branch in BitBucket will trigger a webhook sent to the Jenkins server
  • When the Jenkins server receives the webhook it will build the changed branch (by specifying branch name as ** in the config)
  • After the build is complete a notification is sent back to BitBucket of the build status using the BitBucket notifier
  • Each of these has been easy to set up with just the instructions in the plugin and a few quick Googles. However I've now run into a problem which is maybe more a matter of wanting to run in an unconventional manner than anything else.
Using the normal emailer plugin or the Email-ext plugin it's possible to set emails to send to people involved in the creation of a build. For example the Email-ext plugin allows choice of:
  • Requester
  • Developers (all people who have commits in the build based off its last version)
  • Recipient list (a pre-set list)
  • Various "blame" settings for broken builds
The development process being followed involves each project being worked on by one developer in a named branch, e.g. userA/projectB. Obviously other developers could check that out and push to make changes but that's frowned upon. Even in that instance, the user who pushes the change to BitBucket should be notified.

None of the current settings support this. Requester is the closest, but that only works for manual builds. It seems a very simple requirement that the push to SCM that triggered a build should notify the user who pushed, but this is not documented anywhere that is easy to find.

Answer

After a lot of searching it seems the only way to accomplish this is by using a Pre-send script. This is added to the Advanced setting of the Email-ext post-build step, and takes the form of code written in Groovy which is a Java extension.

The script can take advantage of Environment variables, but is hard to test as there's no way to run the script with these in place. You can test simple Groovy scripts from Home -> Manage Jenkins -> Script console.

One important "gotcha" with the environment variables is that they are "included" in the script, rather than variables or constants. E.g. before the script compiles and runs, the content of the variable is pasted in place of its $NAME. In the example below the multi-line string syntax is used to include the BitBicket payload, whereas it might be expected that def payload = $BITBUCKET_PAYLOAD
would simply work.

import javax.mail.Address
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage
import groovy.json.JsonSlurper


def jsonSlurper = new JsonSlurper()


def bitbucket = jsonSlurper.parseText('''
   $BITBUCKET_PAYLOAD'''

)


switch (bitbucket.actor.username){
 
case "userA":
    msg
.setRecipients(MimeMessage.RecipientType.TO, InternetAddress.parse("use...@domain.com"));
 
break;
 
case "userB":
    msg
.setRecipients(MimeMessage.RecipientType.TO, InternetAddress.parse("use...@domain.com"));
 
break;
}

The setRecipients command overwrites any existing recipient. Thus the recipient list or other email configuration can be set as a fallback for if the user is not recognised. As added debugging, including the username in the body might help.

If the script fails, stack traces should be printed to the console log output of the test, and the build pass/fail shouldn't be affected, but the normal email address setup will be used instead. In stack traces look for lines with Script() in them, as that's the container which evaluates the Groovy script.

Slide

unread,
Jun 1, 2016, 10:07:09 AM6/1/16
to Jenkins Users

The developers list should work for this, it is what I use with git.


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/36c22ac1-0f20-4ce0-9d17-cb8ed116e458%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mike Lehan

unread,
Jun 1, 2016, 11:50:18 AM6/1/16
to Jenkins Users
Sadly not; the developers list will email everyone with a commit made in the current branch. Thus if the developer has recently merged in master and then pushes, everyone who committed into the most recent diff between their working copy and master will receive an email, when they don't care because it's not their work.

Slide

unread,
Jun 1, 2016, 1:30:01 PM6/1/16
to Jenkins Users
Ok, I understand the problem now. The list of authors is pulled from the change sets for the build. You could create your own recipient provider plugin, it is an extension point.

On Wed, Jun 1, 2016 at 8:50 AM Mike Lehan <infern...@gmail.com> wrote:
Sadly not; the developers list will email everyone with a commit made in the current branch. Thus if the developer has recently merged in master and then pushes, everyone who committed into the most recent diff between their working copy and master will receive an email, when they don't care because it's not their work.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages