Github pull requests status with Jenkins

October 24, 2012

Setting up Jenkins to update your commit statuses on Github.

At work we recently updated our testing infrastructure to make Jenkins update the commit statuses when builds are run. We use github for git hosting and run Jenkins servers on our infrastructure.

Jenkins has a github plugin with a currently open issue to support github commit statuses. We didn't want to wait so the solution explained here is implemented outside of a github-specific plugin. Instead, this is a combination of the gcli command-line tool and the post build task Jenkins plugin.

This guide also assumes you only run your builds using the "execute shell" build step in Jenkins.

Server setup

On the jenkins server, install the post build task plugin.

Install the gcli tool:

sudo gem install github_cli --no-rdoc --no-ri

Create a token allowed to update the commit statuses:

curl -u 'username' -d '{"scopes":["repo:status"], \
                        "note":"Jenkins build statuses", \
                        "note_url":""}' \

Setup gcli with the newly created token. As the user which runs your jenkins processes, run:

gcli init --global

And paste the token you just created.

Build configuration

For each repository, create two builds:

  • One which builds your default branch (master) and sends you notifications of all sorts (email, irc, etc.)
  • One which builds all branches (put ** in the branch specifier) and doesn't notify.

All builds will update commmit statuses but with two builds with separate notifications handling you can keep a clean build for your master branch. You don't want to get an email each time a builds fails in an experimental branch. Also, with a single build Jenkins is unable to tell the build status of each branch. It'll just consider the last build as the status for your repository, so having two separate builds makes it easier to determine whether you have something critical to fix or not.

You can also create a custom view to only display 'master' builds on the Jenkins homepage.

On top of the "execute shell" script, put the following line (replace organization and repository with your own information):

gcli status create <organization> <repository> $GIT_COMMIT \
     --params=state:pending \
              target_url:$BUILD_URL \
              description:"Build #$BUILD_NUMBER is running" \
     -f csv

And at the very bottom, add the following line:

gcli status create <organization> <repository> $GIT_COMMIT \
     --params=state:success \
              target_url:$BUILD_URL \
              description:"Build #$BUILD_NUMBER passed" \
     -f csv

If your script is run with -xe, a failure stops the build and the last line is never executed. To avoid builds staying in the pending state, add a post build task.

The post build task plugin searches for text content in the build output, so you'll need to grep for a chunk of text that's only outputted in case of a test failure. In my case I use a combination of tests failed OR FAILED. In the script text field, put the following content:

gcli status create <organization> <repository> $GIT_COMMIT \
     --params=state:failure \
              target_url:$BUILD_URL \
              description:"Build #$BUILD_NUMBER failed" \
     -f csv

This will be much easier to achieve once the github plugin adds native support for this but works very nicely in the meantime.


Add a comment

Comments are closed for this entry.