Month: June 2019

Auto-updating Shared Hosting from Git

I’ve found that when I update my website, it’s tedious to manually copy the files up to my CPanel hosting platform. And my website source code is already on Github version control, so I wanted to be able to automatically update the static files when I push my HTML up to Github.

The only way to achieve this is to utilise Git’s post-receive hook (and Github’s webhooks) and trigger some code on my site which could a perform git pull.

Git hooks are actions that can run your own scripts when events happen to your repository – commit or pull for example – then do whatever task is handy for you. Webhooks are HTTP payloads sent from one server to another, when some event occurs, to trigger off another action in turn on the receiving server.

Some research led me to oxguy3’s deploy.php Github Gist which receives Github’s post-receive webhook and initiates a git pull on on the web host:

Oxguy3’s deploy.php script.

Set up a webhook on your repository in the settings of the Github repository and give it a password. Really I just followed oxguy3’s directions, it’s well documented.

Their PHP script is quite good, it even uses Github’s authorisation headers to prevent randoms from triggering the deploy script.

But oxguy3’s script had to ssh to their server (inside the script) since their host had blocked PHP shell_exec. But my web host hasn’t blocked shell_exec so I could take out those pieces of code. So my forked gist becomes:

My deploy.php script

The script also emails me when it’s complete and the output. If you started from an empty public_html folder you’d be right to run the script now. (I wasn’t though, read on if interested.)

Continue reading →
Posted by Anthony, 0 comments