LD
Change your colour scheme

Auto-purge my blogs Bunny cache with Actions

Published:

I’m using Bunny.net (referral link) for my CDN. As part of that, I cache certain files that get hit a lot but aren’t updated frequently, such as images, CSS, and my RSS feeds.

I deploy my blog to my VPS using Gitea Actions, and so after I’ve made some changes, or written a new post, I’d like to purge the cache on my feed files and my CSS. I can do that using the purge endpoint on the Bunny API.

Purging the cache

The call to do this is fairly simple, and authenticate the request using an API key from my Bunny account settings:

 curl --request POST \
--url 'https://api.bunny.net/purge?url=<my cached file url>&async=false' \
--header 'AccessKey: <My access key>'

For the file URL, I can pass a wildcard in to target all directories, or files of a certain type, such as CSS:

 curl --request POST \
--url 'https://api.bunny.net/purge?url=https%3A%2F%2Flewisdale.dev%2F%2A.xml&async=false' \
--header 'AccessKey: <My access key>'

And this works nicely - the downside here is that unless I want to purge my entire cache (I don’t), I have to make a separate call for each file type I want to remove.

The Gitea Action

I’ve now added three purge calls to my Gitea actions, which run after I successfully deploy my site to my VPS:

name: Build and copy to prod
on:
push:
schedule:
- cron: '@hourly'
jobs:
build-and-copy:
runs-on: ubuntu-latest
steps:
...
- name: Purge XML files from cache
run: |
curl --request POST \
--url 'https://api.bunny.net/purge?url=https%3A%2F%2Flewisdale.dev%2F%2A.xml&async=false' \
--header 'AccessKey: $'

- name: Purge JSON files from cache
run: |
curl --request POST \
--url 'https://api.bunny.net/purge?url=https%3A%2F%2Flewisdale.dev%2F%2A.json&async=false' \
--header 'AccessKey: $'

- name: Purge CSS files from cache
run: |
curl --request POST \
--url 'https://api.bunny.net/purge?url=https%3A%2F%2Flewisdale.dev%2F%2A.css&async=false' \
--header 'AccessKey: $'

Tags:

About the author

My face

I'm Lewis Dale, a software engineer and web developer based in the UK. I write about writing software, silly projects, and cycling. A lot of cycling. Too much, maybe.

Responses