I published a blog post about LinkedIn and Twitter metrics recently and went to check the result on blog.samrhea.com
. I noticed that the content on the page was missing the new post - the older version was cached.
I said out loud “oops, need to purge the cache.” I logged into the Cloudflare dashboard on my phone and punched the Purge Everything
button. And then I thought “what if my phone could hear that and do it for me?” So today I’m building an iOS Shortcut to allow me to do exactly that.
🎯 I have a few goals for this project:
🗺️ This walkthrough covers how to:
main
or when a dispatch event occurs.⏲️Time to complete: ~10 minutes
👔 I work there. I work at Cloudflare. Several of my posts on this blog that discuss Cloudflare focus on building things with Cloudflare Workers. I’m a Workers customer and pay my invoice to use it.
I’m going to start by creating a workflow file that relies on a virtual machine (VM) for the sole purpose of having a place to run a curl
command. This is the same command you’d run from your command line on your laptop; the workflow file just prepares GitHub to run it for me on certain triggers.
I thoroughly exercised GitHub Actions to modify Cloudflare settings in previous blog posts; including dynamically updating the workflow configuration file to use new values from Cloudflare. You can read more about those more complex cases here. Today I’m more excited about the Siri integration and my use case is simple so I will try not to overengineer it.
First, I need to create a Cloudflare API token that will allow GitHub to authenticate and make changes to my account.
I’m going to scope it to just be allowed to control cache purge settings and only for the site where my blog lives.
Since this repository is public, I don’t want to add the token to my workflow file. Even if it was private, that would be bad practice. Instead, I’ll use a GitHub secret to securely hold on to my token for me in a way that I can reference in the file.
I’m also going to go ahead and do that for my email address, too.
I can now create a workflow file in the same GitHub repository that is home to my blog. The file configures the GitHub Action to just run a curl
to the public Cloudflare API. I’m using environment variables for all of the inputs needed.
I’ll go ahead and save that file and merge it to my repository.
The GitHub workflow file is part of this repository and when I merge these changes that qualifies as a push to main
and I conveniently trigger the workflow. I can see that the GitHub Action run was successful.
Now to verify at Cloudflare. I’ll go to the Audit Logs section of the Cloudflare dashboard and make sure the GitHub Action did trigger the purge event.
Isn’t this enough?
It could be. By configuring this workflow I can purge the cache for samrhea.com
anytime I push new content to main
. However, there are some circumstances where I might not want that in the future:
samrhea.com
for several different sites and don’t want to purge everything for all of them anytime I push to main. Note - I could also do that with more selective control over what is purged.The file above only triggers based on a code push to the main
branch of my repository. My goal is also to be able to do this ad-hoc from a manual trigger. To do that, I need to add a second trigger criteria - workflow_dispatch
.
This will allow the GitHub action to receive and respond to an external trigger like an iOS Shortcut.
name: Purge Cache
on:
push:
branches:
- main
workflow_dispatch:
env:
ENDPOINT: '-X POST "https://api.cloudflare.com/client/v4/zones/629e8bfaf0bb8becf800b692bb4a5443/purge_cache"'
EMAIL: '-H "X-Auth-Email: ${{ SECRETS.EMAIL }}"'
CURL_AUTH: '-H "Authorization: Bearer ${{ SECRETS.PURGE_CACHE }}"'
CURL_CONTENT: '-H "Content-Type: application/json"'
CURL_DATA: --data '{"purge_everything":true}'
jobs:
purge:
name: purge cache
runs-on: ubuntu-latest
steps:
- name: purge cache
run: |
curl ${{ env.ENDPOINT }} ${{ env.email }} ${{ env.CURL_AUTH }} ${{ env.CURL_CONTENT }} ${{ env.CURL_DATA }}
I now have a GitHub Action that I know will purge the cache for my website served by Cloudflare. I can now configure an iOS Shortcut to fire off an external event to trigger the workflow.
I have the GitHub app installed on my iPhone and have logged into my account. The GitHub app makes certain actions available to iOS Shortcuts by default, including the GitHub Action feature itself.
I’ll select Dispatch Workflow
which maps up with the trigger configuration above.
Next, I’ll configure the Shortcut with the details of this particular repository and workflow file. I’ll also name the Shortcut.
I can now test running the Shortcut by pushing the icon in the Shortcut homepage.
The Shortcut ran without error and I can go ahead and confirm that Cloudflare also agrees that it worked. Everything looks good once again. Now, for the final test - asking Siri politely. By speaking into my phone, “Hey Siri, purge cache” I can now purge the Cloudflare cache for samrhea.com
.
A few weeks ago, I wrote a blog post documenting how to use OpenAI’s ChatGPT tool to create a site that runs on Cloudflare Workers. In that post, I mentioned that someday ChatGPT might write all tutorials better than me.
I’m curious how close we are to that. I’m considering adding a new section to these posts where I evaluate how ChatGPT would have performed.
I asked ChatGPT to first create a workflow file for a GitHub Action that would purge the Cloudflare cache.
curl
to solve this. ChatGPT seems to have gotten a little lost between steps 3 and 4. The curl
also excludes the inclusion of an email, which still works, but odd.DELETE
. Cloudflare recommends POST
. I tried to search the Internet to see examples where tutorials recommend DELETE
, curious where OpenAI learned this approach, and did find several.I nudged it to get back on track creating the file.
main
, like my first phase above, which is fine - I haven’t told it I want to manually call this.I asked it to update the file to be able to trigger on a manual dispatch event.
I tried once more and, in a creepy sign of (maybe random) progress, the output skipped the curl
step and also defined all of the environment variables - including the email. Still missing the dispatch configuration, though.
Overall, pretty close.