Auto-Deploy Using Git on Shared Hosting

Ported from Old Blog

This is my setup for getting this blog and other project hosted on HawkHost to be updated automatically every time I push the change to Github.

The idea: Set up a service hook on Github (or BitBucket) that will call a file on our sever to run a bash script which does a git pull to get the latest update from Github

This guide assumes that your host allows SSH access.

Step 1

Create a repo on Github

Step 2

Clone the repo on your remote server

Step 3

On the server, create 2 files. One is a bash script that execute git pull and the other one is a php file that will be called by Github hook.

cd /path/to/your/repo
git checkout -- .
git pull

Replace /path/to/your/repo with your own path. Mine is /home/pookin/blog.

<?php

exec('export PATH=/home/pookin/perl5/bin:/usr/local/jdk/bin:/usr/local/jdk/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/courier-imap/sbin:/usr/lib/courier-imap/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/usr/local/bin:/usr/X11R6/bin:/home/pookin/bin:/home/pookin/bin:/home/pookin/bin &&  /bin/bash /path/to/your/deploy.sh 2>&1', &$output, &$return_var);
print_r($output);
print $return_var;

?>

Replace pookin with your own directory name in the server. Most likely this would be your username used to log in to cpanel or access ftp.

Replace /path/to/your/deploy.sh with the path to your deploy.sh created earlier. This php file has to be publily acessible.

Step 4

Add a Github Post-Receive hook following this guide and put the url to your deploy.php as the webhook url.

You’re done! Try accessing your deploy.php on your browser. Mine is here. You’d see the response of git-pull.

Now every time you push to your github repo, your remote server will automatically do a pull and update your website to the latest version.

Chek out other guides on how to use git to manage your website here and here. They use a different approach from me.