Simple automated website deployment via GitHub

Here is a very simple way to add automatic deployment via GitHub. This assumes

  • You have shell access to your web host
  • Your website is in a repository on GitHub
  • CGI scripts are enabled for your website
  • That git (and optionally composer if you use that) is installed

Initial pull

Log on to your web host and do the initial pull.

$ cd path/to/website
$ git clone https://github.com/<you>/<website>.git .

Deployment script

Put the following script in a file named for example deploy.cgi in your website root.

#!/bin/bash
echo Content-type: text/plain
echo

export PATH=~/bin/:$PATH

git pull
composer.phar install
echo DONE
I’m on Dreamhost and the export line is so the cgi script will use the composer.phar and php symlink located in ~/bin. See this post for more info.

Add execute rights to the script.

$ chmod u+x deploy.cgi

You should now be able to visit http://<your-website>/deploy.cgi in your browser and see output like this:

Already up-to-date.
Loading composer repositories with package information
Installing dependencies from lock file
Nothing to install or update
Generating autoload files
DONE

Set up GitHub service hook

Go to https://github.com/<you>/<website>/settings/hooks, click on WebHook URLs and enter the full URL to your script, http://<your-website>/deploy.cgi

Test it out

With this set up you should be able to make a change at your dev machine, commit it and push it to GitHub. The script should then be executed shortly after and your website should be updated automatically. Pretty cool :)

This is of course ultra simple, and if this is a critical site you should probably add some security of some sort. A simple version could be to use a cryptic name like a guid for the script and of course not have this script checked in at GitHub. You might also perhaps want to only pull from a certain branch or things like that, but for a small simple site this works pretty well as long as you remember to only push to GitHub when you have a working set of commits ;)