Skip to content

Commit

Permalink
Merge pull request #48 from dcSpark/feature/cache-invalidation-r2
Browse files Browse the repository at this point in the history
Implements R2 cache invalidation for tools.
  • Loading branch information
guillevalin authored Jan 11, 2025
2 parents d659c3c + 5ed22ff commit fa25a72
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build_tools.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@ jobs:
R2_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }}
R2_BUCKET: ${{ secrets.R2_BUCKET }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_ZONE: ${{ secrets.CLOUDFLARE_ZONE }}
CLOUDFLARE_TOKEN: ${{ secrets.CLOUDFLARE_CACHE_INVALIDATION_TOKEN }}
run: |
export AWS_ACCESS_KEY_ID=${R2_ACCESS_KEY_ID}
export AWS_SECRET_ACCESS_KEY=${R2_SECRET_ACCESS_KEY}
export AWS_ENDPOINT_URL=https://${CLOUDFLARE_ACCOUNT_ID}.r2.cloudflarestorage.com
aws s3 sync ./packages/ s3://${R2_BUCKET}/tools/ \
--endpoint-url https://${CLOUDFLARE_ACCOUNT_ID}.r2.cloudflarestorage.com \
--only-show-errors
deno run --allow-read --allow-env --allow-net scripts/invalidate_cache.ts
45 changes: 45 additions & 0 deletions scripts/invalidate_cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Make this a module
export {};

interface DirectoryEntry {
file: string;
}

const CLOUDFLARE_ZONE = Deno.env.get("CLOUDFLARE_ZONE");
const CLOUDFLARE_TOKEN = Deno.env.get("CLOUDFLARE_TOKEN");

if (!CLOUDFLARE_TOKEN) {
console.error("CLOUDFLARE_TOKEN environment variable is required");
Deno.exit(1);
}

try {
const directoryContent = await Deno.readTextFile("packages/directory.json");
const directory: DirectoryEntry[] = JSON.parse(directoryContent);

const files = directory.map(entry => entry.file);
console.log(`URLs to invalidate: ${JSON.stringify(files, null, 2)}`);

const response = await fetch(
`https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE}/purge_cache`,
{
method: "POST",
headers: {
"Authorization": `Bearer ${CLOUDFLARE_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ files }),
}
);

const result = await response.json();
console.log(`Cache invalidation response (${response.status}):`, JSON.stringify(result, null, 2));

if (!result.success) {
console.error("Failed to invalidate cache");
Deno.exit(1);
}
} catch (error) {
console.error("Error:", error);
Deno.exit(1);
}

0 comments on commit fa25a72

Please sign in to comment.