Skip to content

Commit 3e6ce0e

Browse files
committed
feat: add purge cache tool
1 parent 80feb84 commit 3e6ce0e

5 files changed

Lines changed: 88 additions & 5 deletions

File tree

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CF_ZONE_ID=''
2+
CF_TOKEN=''

.github/workflows/cd_purge.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: '🗑️ CD (Website) — Purge Cache'
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
deploy:
8+
runs-on: ubuntu-latest
9+
timeout-minutes: 15
10+
name: Deploy
11+
steps:
12+
- name: Actions - Checkout
13+
uses: actions/checkout@v4
14+
15+
- name: Actions - Setup Node.js
16+
uses: actions/setup-node@v6
17+
with:
18+
check-latest: true
19+
node-version: lts/*
20+
21+
- name: Cache Dependencies
22+
uses: actions/cache@v4
23+
with:
24+
path: ~/.npm
25+
key: npm-${{ hashFiles('package-lock.json') }}
26+
restore-keys: npm-
27+
28+
- name: Installing Dependencies
29+
run: npm ci
30+
31+
- name: Purge Cache
32+
run: npm run purge
33+
env:
34+
CF_ZONE_ID: ${{ secrets.CF_ZONE_ID }}
35+
CF_TOKEN: ${{ secrets.CF_TOKEN }}

.github/workflows/ci_test.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ jobs:
1111
name: Test
1212
steps:
1313
- name: Actions - Checkout
14-
uses: actions/checkout@v4
14+
uses: actions/checkout@v6
1515

1616
- name: Actions - Setup Node.js
17-
uses: actions/setup-node@v4
17+
uses: actions/setup-node@v6
1818
with:
19-
node-version: 22
19+
check-latest: true
20+
node-version: lts/*
2021

2122
- name: Cache Dependencies
22-
uses: actions/cache@v4
23+
uses: actions/cache@v5
2324
with:
2425
path: ~/.npm
2526
key: npm-${{ hashFiles('package-lock.json') }}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"lint": "prettier --check .",
2121
"lint:fix": "prettier --write .",
2222
"update": "pu && npm i && npm update && (npm audit fix || true)",
23-
"postupdate": "npm run lint:fix"
23+
"postupdate": "npm run lint:fix",
24+
"purge": "tsx --env-file-if-exists=.env tools/purge-cache.mts"
2425
},
2526
"devDependencies": {
2627
"@docusaurus/core": "^3.9.2",

tools/purge-cache.mts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { env, exit } from 'node:process';
2+
3+
const { CF_ZONE_ID, CF_TOKEN, CI } = env;
4+
5+
if (!CF_ZONE_ID || !CF_TOKEN) {
6+
console.log('Skipping: Cloudflare not set.');
7+
exit(0);
8+
}
9+
10+
const url = `https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID.trim()}/purge_cache`;
11+
const body = { purge_everything: true };
12+
const headers = {
13+
Authorization: `Bearer ${CF_TOKEN.trim()}`,
14+
'Content-Type': 'application/json',
15+
};
16+
17+
try {
18+
const response = await fetch(url, {
19+
method: 'POST',
20+
headers,
21+
body: JSON.stringify(body),
22+
});
23+
24+
if (!response.ok) {
25+
const error = await response.json();
26+
const errorMessage =
27+
typeof error === 'object' &&
28+
error !== null &&
29+
'errors' in error &&
30+
Array.isArray(error.errors) &&
31+
error.errors[0]?.message
32+
? error.errors[0].message
33+
: response.statusText;
34+
throw new Error(`Failed to purge cache: ${errorMessage}`);
35+
}
36+
37+
await response.json();
38+
console.log('Cache purged successfully');
39+
} catch (error) {
40+
console.error('Error purging cache.');
41+
42+
if (!CI) throw error;
43+
else exit(1);
44+
}

0 commit comments

Comments
 (0)