Nikolaj Have

Configure auto-deploy with Github webhooks

I'm running this site on a 5$ pr month DigitalOcean server.

I first tried out their new Apps / serverless setup, where you merely specify your Github project to be deployed and the required build commands. Worked like a charm.

This is - however - a 6$ pr app pr month solution, and yes, I'm that cheap 😆 ... I do plan to add multiple sites to this single server.

Anyways, I soon realized that my static site setup required more git pulling than I had hoped for, so decided to configure automatic deployment with a Github webhook.

The process is quite simple

  1. Add webhook to Github (Repository settings > Webhooks)
  2. Create endpoint on server that performs the update

In PHP you could do something like:

// TODO: Handle secret from Github

if ( $_POST['payload'] ) {
    shell_exec('cd /path/to/your/site && git pull');

However, you need to allow Apache to perform the update:

sudo usermod -s /bin/bash www-data
sudo su - www-data

Then upload the public key to Github.

It's incredibly fast! After doing a push, I see the change on the site in less than 5 seconds! 😲

Not fan of the shell_exec()?

Me neither ... 😅

Got a better way to do it? Please let me know in the comments.

Other posts

2021-10-04   PhpStorm & The Yellow Project Pane of Death
Show comments
Published 2nd of Oct, 2021
Content updated 1 year ago