Polling Must Die: Triggering Jenkins Builds from a Git Hook

Written by: Kohsuke Kawaguchi
curl http://yourserver/jenkins/git/notifyCommit?url=<b><url git="" of="" repository="" the=""></url> </b> 

This will scan all the jobs that's configured to check out the specified URL, and if they are also configured with polling, it'll immediately trigger the polling (and if that finds a change worth a build, a build will be triggered, in turn). This allows a script to remain the same when jobs come and go in Jenkins. Or if you have multiple repositories under a single repository host application (such as Gitosis), you can share a single post-receive hook script with all the repositories. Finally, this URL doesn't require authentication even for secured Jenkins, because the server doesn't directly use anything that the client is sending. It runs polling to verify that there is a change before it actually starts a build.

One more final note — in Git, unlike Subversion, a repository does not have its own identity, and a single repository sometimes has multiple URLs to access it. In such a case, simply execute the curl commands multiple times with all the different URLs.

That's it. I hope this will help reduce the use of polling and have more people switch to push notifications. It really is addictive to see the build start in a split second after you push a change.

-- Kohsuke

Stay up to date

We'll never share your email address and you can opt out at any time, we promise.