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.
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 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.
Ben is a lifelong Nintendo fan who also likes to build websites, and develop games. He also buys way too much Lego.