Clear Cloudflare’s Cache Automatically when Pushing from Git to Github Pages

Do you ever find that you have to clear Cloudflare’s cache when you update your site on Github Pages? Well git has a few nice hooks that run when you do various things, like commit something, or push your updates.

So we are going to get use that to our advantage today, and make it all happen on one command!

Setup

First, you will need to get your Cloudflare API key. You’ll find this on your account page under the heading “Global API Key.”

You should get a hash like 123456789abceffce48392ed8493dd

Referencing the Cloudflare API, this is the command that we use to purge the cache:

curl -X DELETE "https://api.cloudflare.com/client/v4/zones/<site-id>/purge_cache" \
     -H "X-Auth-Email: <email>" \
     -H "X-Auth-Key: <apikey>" \
     -H "Content-Type: application/json" \
     --data '{"purge_everything":true}')"

We need that id though, which is the id of the site. We do that by running this script/command, setting the email and apikey to the right values:

apikey=<apikey>
email=<email used to login to cloudflare>
curl -X GET "https://api.cloudflare.com/client/v4/zones" \
    -H "X-Auth-Email: $email" \
    -H "X-Auth-Key: $apikey" \
    -H "Content-Type: application/json" | sed 's/,/\n/g'

Look for the site you want:

{"id":"cd7d0123e3012345da9420df9514dad0"
"name":"supertechcrew.com"

Git

Now that we have the id, we can get git up and going.

First, you need to make a ~/.cloudflarerc file in your home directory. I did this so the apikey (which is like a password) doesn’t get in the repo.

apikey=<apikey>
email=<email used to login to cloudflare>
id=<your site's id>

Fill out the info, and save it.

Now, in your .git folder in your repo, there is a ‘hooks’ directory. We will put this file into the pre-push hook:

.git/hooks/pre-push

#!/bin/bash

if ! [ -f ~/.cloudflarerc ] ; then
  echo "No ~/.cloudflarerc file found. Cloudflare clear cache SKIPPED."
  exit 0
fi

. ~/.cloudflarerc

echo -n "Clearing cloudflare cache ....."

ret="$(curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/$id/purge_cache" \
     -H "X-Auth-Email: $email" \
     -H "X-Auth-Key: $apikey" \
     -H "Content-Type: application/json" \
     --data '{"purge_everything":true}')"

if [ -n "$(echo $ret | grep success)" ] ; then
  echo " Success!"
else
  echo " *** FAILED ***"
  echo "Could not clear cloudflare's cache. Update will not proceed."
  # exit with 1, so the update does not proceed, so we will know
  exit 1
fi

I had it fail with a 1 so that if the script couldn’t clear Cloudflare’s cache, the git push would also fail, letting me know when something had happened, rather than letting me just go on thinking it had worked.

Make it executable

chmod +x .git/hooks/pre-push

Now, when you push the git repo, you should see:

Pushing to [email protected]:supertechcrew/supertechcrew.github.io.git

Clearing cloudflare cache ..... Success!
To [email protected]:supertechcrew/supertechcrew.github.io.git
 = [up to date]      master -> master
updating local tracking ref 'refs/remotes/origin/master'
Everything up-to-date

All set for awesomeness!

Other Options

If the above doesn’t work for you, you may consider adding this to your git config file (in the repo):

[alias]
  xpush = !git push $1 $2 && my-script.sh

Then when you run git xpush it will push it and then run my-script.sh. Originally found here.

Or you can have an alias/wrapper that will run git push and then your script.

Resources