-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-cloudflare.sh
More file actions
executable file
·116 lines (100 loc) · 3.77 KB
/
deploy-cloudflare.sh
File metadata and controls
executable file
·116 lines (100 loc) · 3.77 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
# Manual Cloudflare R2 deployment script
# Usage: ./deploy-cloudflare.sh [version]
# Example: ./deploy-cloudflare.sh 1.4.0
set -e
VERSION=${1:-$(node -p "require('./package.json').version")}
BUCKET=${CLOUDFLARE_R2_BUCKET_NAME:-"widget-cdn"}
echo "🚀 Deploying widget v${VERSION} to Cloudflare R2 bucket: ${BUCKET}"
# Check if Wrangler is installed
if ! command -v wrangler &> /dev/null; then
echo "📦 Installing Wrangler CLI..."
npm install -g wrangler
fi
# Check if dist files exist
if [ ! -f "dist/widget-${VERSION}.js" ]; then
echo "❌ Build files not found. Run 'npm run build' first."
exit 1
fi
# Check for required environment variables
if [ -z "$CLOUDFLARE_API_TOKEN" ] || [ -z "$CLOUDFLARE_ACCOUNT_ID" ]; then
echo "❌ Missing required environment variables:"
echo " CLOUDFLARE_API_TOKEN"
echo " CLOUDFLARE_ACCOUNT_ID"
echo ""
echo "Set them with:"
echo " export CLOUDFLARE_API_TOKEN='your-token'"
echo " export CLOUDFLARE_ACCOUNT_ID='your-account-id'"
exit 1
fi
# Authenticate with Cloudflare
echo "🔐 Authenticating with Cloudflare..."
wrangler login --api-token "$CLOUDFLARE_API_TOKEN" || {
echo "❌ Authentication failed. Check your API token."
exit 1
}
# Upload versioned files (immutable, cache forever)
echo "📤 Uploading versioned files..."
wrangler r2 object put ${BUCKET}/widget-${VERSION}.js \
--file=dist/widget-${VERSION}.js \
--content-type="application/javascript" \
--custom-metadata="cache-control=public, max-age=31536000, immutable" || {
echo "❌ Failed to upload widget-${VERSION}.js"
exit 1
}
wrangler r2 object put ${BUCKET}/widget-${VERSION}.css \
--file=dist/widget-${VERSION}.css \
--content-type="text/css" \
--custom-metadata="cache-control=public, max-age=31536000, immutable" || {
echo "❌ Failed to upload widget-${VERSION}.css"
exit 1
}
# Upload latest files (cache 1 hour)
echo "📤 Uploading latest files..."
wrangler r2 object put ${BUCKET}/widget.js \
--file=dist/widget.js \
--content-type="application/javascript" \
--custom-metadata="cache-control=public, max-age=3600" || {
echo "❌ Failed to upload widget.js"
exit 1
}
wrangler r2 object put ${BUCKET}/widget.css \
--file=dist/widget.css \
--content-type="text/css" \
--custom-metadata="cache-control=public, max-age=3600" || {
echo "❌ Failed to upload widget.css"
exit 1
}
echo "✅ Upload complete!"
echo ""
echo "Files uploaded to R2 bucket: ${BUCKET}"
echo " - widget-${VERSION}.js"
echo " - widget-${VERSION}.css"
echo " - widget.js (latest)"
echo " - widget.css (latest)"
echo ""
# Show access URLs
CUSTOM_DOMAIN=${CLOUDFLARE_CUSTOM_DOMAIN:-"cdn.ezcontactform.com"}
echo "Access URLs:"
echo " - https://${CUSTOM_DOMAIN}/widget-${VERSION}.js"
echo " - https://${CUSTOM_DOMAIN}/widget-${VERSION}.css"
echo " - https://${CUSTOM_DOMAIN}/widget.js"
echo " - https://${CUSTOM_DOMAIN}/widget.css"
# Purge cache if zone ID is set
if [ -n "$CLOUDFLARE_ZONE_ID" ] && [ -n "$CLOUDFLARE_CUSTOM_DOMAIN" ]; then
echo ""
echo "🔄 Purging Cloudflare cache..."
curl -X POST "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/purge_cache" \
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
-H "Content-Type: application/json" \
--data "{
\"files\": [
\"https://${CLOUDFLARE_CUSTOM_DOMAIN}/widget-${VERSION}.js\",
\"https://${CLOUDFLARE_CUSTOM_DOMAIN}/widget-${VERSION}.css\",
\"https://${CLOUDFLARE_CUSTOM_DOMAIN}/widget.js\",
\"https://${CLOUDFLARE_CUSTOM_DOMAIN}/widget.css\"
]
}" > /dev/null 2>&1 && echo "✅ Cache purged" || echo "⚠️ Cache purge failed (check zone ID)"
fi
echo ""
echo "🎉 Deployment complete!"