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.

Let me know what you think on Mastodon, or BlueSky (or Twitter X if you must).

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...
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...
01 Oct 2013

How to Use Bitbucket with Github for Mac

I like BitBucket – all the cool kids use Github, but for small businesses BitBucket is great. Why? Because it has free private repositories for teams of 5 or smaller. And that’s me right there.Currently I am using BitBucket for...
06 Nov 2014

Updating SourceTree Git Login Credentials

At work we use Git for versioning our code. We also use the suite of Atlassian products for managing tasks, code, and deployment. As such I use SourceTree a lot to manage my code. We’re also security conscious and so...
21 Oct 2023

Creating Jekyll Plugins (and modern image formats)

When I first started in web design (in the late 90’s), there was a lot of emphasis on learning about file formats, file naming conventions, and other technical aspects that don’t seem to be talked about these days. It’s easy...
26 Jun 2014

New TimThumb Exploit Found

It’s been reported today that there is a new TimThumb exploit found. Unfortunately nobody told me about this before the exploit was announced – in fact I found out about the bug through wptavern.com so I haven’t been able to...