Using Git Hooks to Clear Cloudflares Cache

I have a couple of sites powered by Jekyll on Github pages, and I use Cloudflare to help make them nice and speedy (and to get around the github pages traffic limits). However, this means that each time I update one of the websites I had to log into Cloudflare and clear the cache so that the changes show up – or I had to wait for the changes to appear organically.

I like to automate things, so having to manually log in and clear the cache was a little frustrating.

I had seen that Cloudflare has an API for doing things like cache clearing but had never used it. Since I don’t update the sites that often I didn’t bother changing things. However I was making some changes today and I thought I’d see if there was a way to automate the process of pushing something to Github and clearing the Cloudflare cache.

It literally took me 20 seconds to find someone who had done this, and shared the code on Github.

Git Hooks

The code makes use of the Git hooks system. This is something I have been aware of, but never used. I was under the impression I had to use some sort of continuous integration (CI) system to make use of Git hooks – so was overjoyed when I realised how simple it is.

The hooks live in the .git directory – and they fire automatically when certain actions are performed. The hooks do not get committed to Git, which means private things like API keys do not get shared. It also means that if you’re working in a team environment then the hooks would need to be stored on a remote server (or use some other method for sharing), but since I am the primary project users & owner having them on my computer works well for me – I just have to remember to back things up.

The Codes

The code I found is below. It’s super easy to make use of, and even includes instructions.

  • You have to make sure to change the items that are in angled brackets.
  • The “branch” value mentioned is on line 14. Change the value in the quotes, not the $branch variable itself.
  • So that I could access the .git directory (which contains the hooks directory) I had to show hidden files on MacOS. I found a nice tutorial that explains how to do this, and how to add some commands so that you can type ‘showFiles’ or ‘hideFiles’ in the terminal and have them magically show and hide in Finder.

How was it for you? Let me know on BlueSky or Mastodon

(Please) Link to this page

Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.

Related Posts

23 Mar 2017

New Adventures in Jekyll

I use WordPress a lot, but a couple of weeks ago I decided to rebuild one of my older sites with Jekyll (a static site editor) so that I could host it on Github pages. As I have explained before...
20 Feb 2013

The Amazing Art of Flipping Websites

Recently I have looked at expanding my internet empire. I have all sorts of ideas and never enough time – so I thought I would see if I could buy some websites relatively cheaply, improve them, and then either flip...
04 Nov 2016

Automating WordPress Development with Gulp, Bash, and PHP

When I wrote about the things I had learned from releasing 20 WordPress themes, I mentioned that I had automated as much as I could. One of the things I automated was the build and deployment process for my themes...
12 Jun 2023

Rebuilding the Binary Moon Website

I have recently rebuilt the Binary Moon website. It’s been something I’ve been thinking about for a while, and a recent issue with my web host pushed me to finally make a start. In this post, I’ll share some of...
10 Sep 2016

Using Github for Copywriters (via the Github website)

I’m currently working on a new WordPress course (MasterWP – first email is sent on Monday 12th September) that helps to teach things beyond the basics – and to do it I need a website. I’m making the course with...