Deploying Automatically with Github WebHooks

May 7, 2013

I recently had to set up a super simple auto-deploy routine for a dev team working on a responsive website for a large U.S. publisher. Since we are using Github to host our code repo, it was pretty easy to set up. Everyone has been digging the workflow so I thought I'd share the details here.

The Basics

  1. Set up a web page on your dev server that is accessible at a public URL and has read/write permission to the dev directory on the dev server. See below for a stupidly simple example using PHP.
  2. On, navigate to the WebHook URL settings for your repo:

    YOUR_REPO > Settings > Service Hooks > WebHook URLs

    Enter the URL to the page you created in step 1. When you push to Github, Github will send a POST request with a bunch of info about the push to the URL you provided.

    NOTE: Only the repo owner has access to the settings area.

Tricky Bits

  • The web page needs to have permission to read/write to the dev directory on the dev server. It can be tough to do this is a way that is secure.
  • If the repo on github is private, you have to monkey around to grant the dev server access to the repo on github.
  • Debugging sucks. If the WebHook isn't working, it's pretty hard to figure out why.

Related Links

PHP Example


// Init vars
$LOCAL_ROOT         = "/var/www/my_new_site";
$LOCAL_REPO_NAME    = "public_html";
$REMOTE_REPO        = "";
$DESIRED_BRANCH     = "dev";

// Delete local repo if it exists
if (file_exists($LOCAL_REPO)) {
    shell_exec("rm -rf {$LOCAL_REPO}");

// Clone fresh repo from github using desired local repo name and checkout the desired branch
echo shell_exec("cd {$LOCAL_ROOT} && git clone {$REMOTE_REPO} {$LOCAL_REPO_NAME} && cd {$LOCAL_REPO} && git checkout {$BRANCH}");

die("done " . mktime());

comments powered by Disqus

iPhone book Android book PHP book

Jonathan Stark is a mobile strategy consultant who helps CEOs transition their business to mobile. Unlike other mobile strategy consultants, clients work directly with Jonathan and not a group of junior employees who are learning on the job.

Join Jonathan’s Mailing List »

Jonathan is the author of three books on mobile and web development, most notably O'Reilly's Building iPhone Apps with HTML, CSS, and JavaScript which is available in seven languages.

His Jonathan's Card experiment made international headlines by combining mobile payments with social giving to create a "pay it forward" coffee movement at Starbucks locations all over the U.S.

Hear Jonathan speak, watch his talk show, listen to his podcast (co-hosted with the incomparable @kellishaver), join the mailing list, or connect online: