Branch-based Git push notification on Jenkins

17. Juli 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

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

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.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert