Branch-based Git push notification on Jenkins

17. July 2014 - Computer Science / Continuous Integration

The Jenkins continuous integration server is not only able to poll a Git repository regularly for changes, but since version 1.1.14 of the Git Plugin you can invoke the poll by calling a specific url.

In a first version it would be totally fine to simply run all matching Jenkins project for any commit done in the Git repository.

curl -s "http://JENKINS/git/notifyCommit?url=https://GIT/PROJECT"

But assuming that there are many feature branches and multiple Git repositories in your company, you might want to only start polling the repository for project that are actually effected by the last commit. There is an optional branch parameter to restrict the projects further.

curl -s "http://JENKINS/git/notifyCommit?url=https://GIT/PROJECT&branches=$branch"

Finally we need to put all our thoughts to the post-receive hook of our remote repository

#!/bin/bash
echo "Notifying Jenkins ..."
while read oldrev newrev refname
do
  branch=$(git rev-parse --symbolic --abbrev-ref $refname)
  echo "== $branch =="
  curl -s "http://JENKINS/git/notifyCommit?url=https://GIT/PROJECT&branches=$branch"
done

That’s it. Now you can increase the polling period of your Jenkins projects to once a day and beside polling less often, we are now even polling faster after our commit.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*