|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Eurosky fork: upload the static web build to a Bunny.net Edge Storage zone. |
| 3 | +# |
| 4 | +# Reads from the environment (or ./.env if not already set): |
| 5 | +# BUNNY_STORAGE_ZONE full base URL incl. zone name, e.g. |
| 6 | +# https://storage.bunnycdn.com/eurosky-app |
| 7 | +# BUNNY_STORAGE_PASSWORD storage zone password (sent as the AccessKey header) |
| 8 | +# |
| 9 | +# Usage: ./bunny_upload.sh [build-dir] (default: web-build) |
| 10 | +# |
| 11 | +# This only uploads files. It does not delete stale objects or purge the pull |
| 12 | +# zone cache - that comes with the CI step. Hashed assets are immutable so |
| 13 | +# leftover old chunks are harmless; index.html overwrites in place. |
| 14 | +set -euo pipefail |
| 15 | + |
| 16 | +DIR="${1:-web-build}" |
| 17 | + |
| 18 | +# Load .env only if the vars aren't already provided by the environment (CI). |
| 19 | +if [[ -z "${BUNNY_STORAGE_ZONE:-}" || -z "${BUNNY_STORAGE_PASSWORD:-}" ]]; then |
| 20 | + if [[ -f .env ]]; then |
| 21 | + set -a; . ./.env; set +a |
| 22 | + fi |
| 23 | +fi |
| 24 | +: "${BUNNY_STORAGE_ZONE:?set BUNNY_STORAGE_ZONE}" |
| 25 | +: "${BUNNY_STORAGE_PASSWORD:?set BUNNY_STORAGE_PASSWORD}" |
| 26 | + |
| 27 | +BASE="${BUNNY_STORAGE_ZONE%/}" # strip any trailing slash |
| 28 | +[[ -d "$DIR" ]] || { echo "no $DIR/ - build first (./pages_build.sh)"; exit 1; } |
| 29 | + |
| 30 | +total=$(find "$DIR" -type f | wc -l | tr -d ' ') |
| 31 | +echo "Uploading $total files from $DIR/ -> $BASE ..." |
| 32 | + |
| 33 | +export BASE BUNNY_STORAGE_PASSWORD DIR |
| 34 | + |
| 35 | +# Upload a single file; print only on failure. Bunny returns 201 on success. |
| 36 | +upload_one() { |
| 37 | + local file="$1" |
| 38 | + local rel="${file#"$DIR"/}" |
| 39 | + local code |
| 40 | + code=$(curl -sS -o /dev/null -w '%{http_code}' -X PUT \ |
| 41 | + "$BASE/$rel" \ |
| 42 | + -H "AccessKey: $BUNNY_STORAGE_PASSWORD" \ |
| 43 | + --data-binary @"$file") || { echo "ERR $rel (curl failed)"; return 1; } |
| 44 | + if [[ "$code" != "201" && "$code" != "200" ]]; then |
| 45 | + echo "FAIL $code $rel" |
| 46 | + return 1 |
| 47 | + fi |
| 48 | +} |
| 49 | +export -f upload_one |
| 50 | + |
| 51 | +fails=0 |
| 52 | +find "$DIR" -type f -print0 \ |
| 53 | + | xargs -0 -P 8 -I{} bash -c 'upload_one "$@"' _ {} \ |
| 54 | + || fails=1 |
| 55 | + |
| 56 | +if [[ "$fails" != "0" ]]; then |
| 57 | + echo "Completed WITH ERRORS (see FAIL/ERR lines above)." >&2 |
| 58 | + exit 1 |
| 59 | +fi |
| 60 | +echo "Done: uploaded $total files to $BASE" |
| 61 | + |
| 62 | +# Optional: purge the pull zone cache so the new build is served immediately. |
| 63 | +# Needs the account-level API key (NOT the storage password) and the pull |
| 64 | +# zone id. Skipped silently if either is missing (e.g. plain local upload). |
| 65 | +if [[ -n "${BUNNY_API_KEY:-}" && -n "${BUNNY_PULLZONE_ID:-}" ]]; then |
| 66 | + echo "Purging pull zone $BUNNY_PULLZONE_ID cache ..." |
| 67 | + pcode=$(curl -sS -o /dev/null -w '%{http_code}' -X POST \ |
| 68 | + "https://api.bunny.net/pullzone/${BUNNY_PULLZONE_ID}/purgeCache" \ |
| 69 | + -H "AccessKey: ${BUNNY_API_KEY}") || { echo "purge: curl failed" >&2; exit 1; } |
| 70 | + if [[ "$pcode" == "204" || "$pcode" == "200" ]]; then |
| 71 | + echo "Purged." |
| 72 | + else |
| 73 | + echo "purge returned HTTP $pcode" >&2 |
| 74 | + exit 1 |
| 75 | + fi |
| 76 | +else |
| 77 | + echo "(skipping cache purge: set BUNNY_API_KEY + BUNNY_PULLZONE_ID to enable)" |
| 78 | +fi |
0 commit comments