From c3c1251dfd37789e544f9edb6adca57f4dbf8053 Mon Sep 17 00:00:00 2001 From: Lewis Dale Date: Fri, 26 Apr 2024 12:07:36 +0100 Subject: [PATCH] Add post about automatically purging the cache with Bunny, and add the other purge calls to the action --- .gitea/workflows/build-and-copy.yaml | 22 +++---- .../posts/2024/4/auto-purge-bunny-cache.md | 63 +++++++++++++++++++ 2 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 src/blog/posts/2024/4/auto-purge-bunny-cache.md diff --git a/.gitea/workflows/build-and-copy.yaml b/.gitea/workflows/build-and-copy.yaml index 5ece68d..dc14223 100644 --- a/.gitea/workflows/build-and-copy.yaml +++ b/.gitea/workflows/build-and-copy.yaml @@ -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 }}' \ No newline at end of file + - 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 }}' \ No newline at end of file diff --git a/src/blog/posts/2024/4/auto-purge-bunny-cache.md b/src/blog/posts/2024/4/auto-purge-bunny-cache.md new file mode 100644 index 0000000..105cb83 --- /dev/null +++ b/src/blog/posts/2024/4/auto-purge-bunny-cache.md @@ -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=&async=false' \ + --header 'AccessKey: ' +``` + +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: ' +``` + +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 }}' +```