Add post about automatically purging the cache with Bunny, and add the other purge calls to the action
All checks were successful
Build and copy to prod / build-and-copy (push) Successful in 2m5s

This commit is contained in:
Lewis Dale 2024-04-26 12:07:36 +01:00
parent da167b3fba
commit c3c1251dfd
2 changed files with 73 additions and 12 deletions

View File

@ -40,15 +40,13 @@ jobs:
curl --request POST \
--url 'https://api.bunny.net/purge?url=https%3A%2F%2Flewisdale.dev%2F%2A.xml&async=false' \
--header 'AccessKey: ${{ secrets.BUNNY_ACCESS_KEY }}'
# - 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: ${{ secrets.BUNNY_ACCESS_KEY }}'
# - 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: ${{ secrets.BUNNY_ACCESS_KEY }}'
- 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: ${{ secrets.BUNNY_ACCESS_KEY }}'
- 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: ${{ secrets.BUNNY_ACCESS_KEY }}'

View File

@ -0,0 +1,63 @@
---
title: Auto-purge my blogs Bunny cache with Actions
date: 2024-04-26T12:30:00.0Z
excerpt: How to automatically purge certain files from the Bunny.net cache after deploying my Eleventy blog
tags:
- eleventy
---
I'm using [Bunny.net (referral link)](https://bunny.net?ref=6x27cy5y27) 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](https://docs.bunny.net/reference/purgepublic_indexpost) 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:
```bash
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:
```bash
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:
```yaml
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: ${{ secrets.BUNNY_ACCESS_KEY }}'
- 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: ${{ secrets.BUNNY_ACCESS_KEY }}'
- 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: ${{ secrets.BUNNY_ACCESS_KEY }}'
```