-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitrise-navigation-deploy.sh
More file actions
executable file
·67 lines (54 loc) · 2.17 KB
/
bitrise-navigation-deploy.sh
File metadata and controls
executable file
·67 lines (54 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env bash
# Deploy bitrise-navigation assets to GCS and purge Cloudflare cache.
#
# Usage: ./bitrise-navigation-deploy.sh <gcp-sa-secret-b64> <cf-zone-id> <cf-api-token>
#
# gcp-sa-secret-b64 Base64-encoded GCP service account key JSON
# cf-zone-id Cloudflare zone ID for bitrise.io
# cf-api-token Cloudflare API token (needs only "Cache Purge" permission)
set -euo pipefail
GCP_SA_SECRET_B64="${1:?Usage: $0 <gcp-sa-secret-b64> <cf-zone-id> <cf-api-token>}"
CF_ZONE_ID="${2:?Usage: $0 <gcp-sa-secret-b64> <cf-zone-id> <cf-api-token>}"
CF_API_TOKEN="${3:?Usage: $0 <gcp-sa-secret-b64> <cf-zone-id> <cf-api-token>}"
DIST_DIR="${DIST_DIR:-/bitrise/src/dist}"
GCS_BUCKET="gs://web-cdn.bitrise.io"
CDN_BASE="https://web-cdn.bitrise.io"
FILES=(
bitrise-navigation-loader.js
bitrise-navigation.js
bitrise-navigation.html
)
# --- Authenticate with GCP ---------------------------------------------------
KEY_FILE=$(mktemp)
echo "${GCP_SA_SECRET_B64}" | base64 --decode > "${KEY_FILE}"
gcloud auth activate-service-account --key-file="${KEY_FILE}"
rm -f "${KEY_FILE}"
# --- Upload to GCS -----------------------------------------------------------
echo "Uploading ${#FILES[@]} files to ${GCS_BUCKET}:"
for FILE in "${FILES[@]}"; do
printf ' %s ... ' "${FILE}"
gcloud storage cp "${DIST_DIR}/${FILE}" "${GCS_BUCKET}/${FILE}"
echo "done"
done
# --- Purge Cloudflare cache ---------------------------------------------------
# Uses the per-URL purge API ("files" key) — only the listed URLs are
# invalidated, NOT the entire zone cache.
CDN_URLS=()
for FILE in "${FILES[@]}"; do
CDN_URLS+=("${CDN_BASE}/${FILE}")
done
JSON_FILES=$(printf '"%s",' "${CDN_URLS[@]}")
JSON_FILES="[${JSON_FILES%,}]"
echo "Purging Cloudflare cache for ${#CDN_URLS[@]} URLs:"
printf ' %s\n' "${CDN_URLS[@]}"
RESPONSE=$(curl -sf -X POST \
"https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/purge_cache" \
-H "Authorization: Bearer ${CF_API_TOKEN}" \
-H "Content-Type: application/json" \
--data "{\"files\":${JSON_FILES}}")
if echo "${RESPONSE}" | grep -q '"success":true'; then
echo "Cache purged successfully."
else
echo "Cache purge failed: ${RESPONSE}" >&2
exit 1
fi