Over the last iteration we spent some time perfecting our continuous integration environment. We’ve started using Jenkins (the CI environment previously known as Hudson), and so far we’re pretty happy. One thing that vexed us for a while was getting a GitHub service hook to trigger our builds.
Here’s what we had to do:
1. Create a “GitHub” account with Jenkins.
The credentials from this “github” user account are required for GitHub to gain access to the build URL. We’re using the built-in Jenkins user database with matrix-based authorization. So we added a “github” account using the “sign up” functionality, and then gave that user “Overall: Read” and “Job: Read” permissions. We’re not totally sure why these particular permissions are required, but we are sure that the URL-based build trigger doesn’t work without them!

Creating a “github” account in Jenkins
2. Configure Remote Build Trigger
On the “Configure” section of the Jenkins job you’re setting up, check the “Trigger builds remotely” box (under the “Build Triggers” section), and set some long random authentication token:

Configuring Remote Build Trigger
3. Configure a Post-Receive URL at GitHub
Here’s what the URL for GitHub should look like:
http://GITUSER:GITPASSWORD@pathtojenkins.com/job/YourProjectName/build?token=LONG_RANDOM_TOKEN
GITUSER and GITPASSWORD pertain to the “github” user you created with Jenkins. Also, don’t forget to include the port Jenkins is running on if it’s not port 80. Add this to your GitHub repository by visiting the “Service Hooks” Admin section of the project you’re setting up. Here’s about what it should look like:

Configuring a Post-Receive URL at GitHub
Hopefully this saves you some time if you are looking to use Jenkins and want to use a GitHub service hook to trigger your builds.
And let us know if you have any questions, comments, or suggestions! Thanks.
Michael Johnston
on April 25, 2011 said:
Do you have any idea how to make jenkins only fire a build when the specified branch has changed? Right now it always runs the build job even when the commits that fired the github service hook were to a different branch than specified in the job.
Rafer Hazen
on May 10, 2011 said:
I don’t think there’s a good simple way to do this. GitHub doesn’t provide any way to specify when a post-receive hook is fired, and there’s no way (that we can find) to add logic to Jenkins’ build trigger functionality. If branch-only build triggers are important to you, here are a couple things you might try.
1. Add some simple middleware (like a Sinatra app) between GitHub and Jenkins. GitHub fires a post-receive hook to your app, which parses the accompanying JSON payload (documentation: http://help.github.com/post-receive-hooks/) and decides if it should trigger a build in Jenkins based on the “refs” attribute (see previous documentation).
2. Write a Jenkins plugin that conditionally triggers the build based on the JSON payload that GitHub submits. I don’t have any experience writing plugins for Jenkins, so you’d be on your own for this one.
The majority of our development is done on a single iteration branch and most of our tests suites are pretty fast, so the occasional unnecessary build doesn’t cause us too much grief. All the same, the functionality you described seems quite useful; let us know what you come up with!
Thanks!
Irene Haque
on May 12, 2011 said:
Thank you! Works like a charm. Surprisingly easy after the headache of installing Jenkins.
Rafer Hazen
on May 13, 2011 said:
Glad it helped!
Kohsuke Kawaguchi
on June 15, 2011 said:
I’ve released https://wiki.jenkins-ci.org/display/JENKINS/Github+Plugin that automates this. It also doesn’t involve sharing the secret with GitHub. I’m hoping that folks find it useful.
Rafer Hazen
on June 20, 2011 said:
That looks awesome. We’ll definitely give it a try.
Klokie
on September 21, 2011 said:
Hi, just wanted to let you know that my team is using the URL Change Trigger plug-in to get Hudson to see all new commits to a given branch using GitHub’s Atom feed for the project, e.g. https://github.com/orgname/projname/commits/master.atom
I don’t know if Hudson is polling the Atom feed (assuming it is, unless GitHub support PubSubHubbub), but it’s very responsive.
tchen
on May 7, 2012 said:
I wasn’t able to get the github plugin hook to work after a few tries, but this worked like a charm.
Stirling Olson
on May 14, 2012 said:
tchen, great to hear! Good luck!