fix(ci): actually purge BunnyCDN cache in purge_cdn_cache.sh #78

Merged
pat-s merged 1 commit from pr/purge-cdn-cache-fixes into main 2026-06-08 08:31:03 +00:00

View file

@ -1,64 +1,44 @@
#!/bin/bash
#!/usr/bin/env bash
#
# Purge BunnyCDN edge cache for the index files of one ARCH+OS_ID
# combination across one or more domains.
#
# Usage:
# purge_cdn_cache.sh <BUNNYNET_API_KEY> <ARCH> <OS_ID> <domain> [<domain>...]
#
set -euo pipefail
set -o pipefail
set -x
if (( $# < 4 )); then
echo "usage: $0 <api_key> <arch> <os_id> <domain> [<domain>...]" >&2
exit 2
fi
# Function to URL-encode a string using Python
urlencode() {
python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1], safe=''))" "$1"
api_key="$1"
arch="$2"
os_id="$3"
shift 3
resources=(
"src/contrib/PACKAGES"
"src/contrib/PACKAGES.gz"
"src/contrib/PACKAGES.rds"
"src/contrib/PACKAGES.db"
"src/contrib/Meta/archive.rds"
)
purge_one() {
local target="$1"
# --data-urlencode encodes value of `url=` for us; -G forces GET.
curl -sS -G -X POST \
-H "AccessKey: ${api_key}" \
--data-urlencode "url=${target}" \
--data "async=false" \
"https://api.bunny.net/purge"
echo
}
# Function to send the purge request using the provided API key
purge() {
local api_key="$1"
local url="$2"
curl -vL -X POST -H "AccessKey: $api_key" "$url"
}
# Function to generate the purge URLs for a given domain, arch, and OS ID
generate_urls() {
local domain="$1"
local arch="$2"
local os_id="$3"
# Array of resource paths (can be modified or extended as needed)
local resources=(
"src/contrib/PACKAGES"
"src/contrib/PACKAGES.gz"
"src/contrib/PACKAGES.rds"
"src/contrib/PACKAGES.db"
)
local urls=()
for domain in "$@"; do
for resource in "${resources[@]}"; do
local target_url="https://${domain}/${arch}/${os_id}/latest/${resource}"
local encoded
encoded=$(urlencode "$target_url")
urls+=( "https://api.bunny.net/purge?url=${encoded}&async=false" )
purge_one "https://${domain}/${arch}/${os_id}/latest/${resource}"
done
echo "${urls[@]}"
}
# Function to process multiple domains.
# Usage: process_domains <BUNNYNET_API_KEY> <ARCH> <OS_ID> domain1 [domain2 ... domainN]
purge_cdn_cache() {
local api_key="$1"
local arch="$2"
local os_id="$3"
shift 3 # Remove the first three arguments, leaving the domains
for domain in "$@"; do
# Generate the list of URLs for the current domain
local urls
urls=($(generate_urls "$domain" "$arch" "$os_id"))
# Purge each URL
for url in "${urls[@]}"; do
purge "$api_key" "$url"
done
done
}
# Example usage:
# purge_cdn_cache "$BUNNYNET_API_KEY" "$ARCH" "$OS_ID" "domain1.com" "domain2.com"
done