#How to deploy your code from GitHub automatically

I love GitHub. The more I use it, the more wonderful features I discover. I’d like to share one I just came across today, Service Hooks, and tell you how you can leverage them to have your production web site update to the latest code automatically, whenever you push a new version.

For this to work, your site has to be served directly from a checked out copy of the GitHub repository, and HTTP requests must be served by the same user as owns the repository. I’m also going to assuming you’re running PHP, although that part should be easy enough for you to customize.

All we’re going to do is add an endpoint on your site that does a git pull, and then we’ll set GitHub up to fetch that endpoint whenever a push happens.

  1. Add a file to your web server called github_update.php (or whatever you’d like). Put this code in it:
    <?php `git pull`; ?>

  2. Go to Repository Administration for your repo (http://github.com/username/repository/admin).

  3. Click Service Hooks, and you’ll see a list of available services. Select Post-Receive URL.

  4. Enter the URL for your update script (e.g. http://example.com/github_update.php) and click Update Settings.

That’s it! Whenever anyone pushes to the repository, GitHub will fetch that URL, which will cause your server to fetch the latest code. Automatic, immediate deploys. Slick.

I love GitHub.