Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·282 lines (235 loc) · 7.74 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·282 lines (235 loc) · 7.74 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/bin/bash
# Frontend Deployment Script for CUDly
# Supports AWS (S3+CloudFront), Azure (Blob+CDN), and GCP (Cloud Storage+CDN)
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Print colored message
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Usage function
usage() {
cat <<EOF
Usage: $0 [OPTIONS]
Deploy CUDly frontend to cloud storage and CDN.
OPTIONS:
-p, --provider PROVIDER Cloud provider (aws|azure|gcp) [required]
-e, --environment ENV Environment (dev|staging|prod) [default: dev]
-b, --bucket BUCKET Bucket/storage account name [required]
-d, --distribution ID Distribution/CDN ID [required for cache invalidation]
-r, --region REGION AWS region or Azure location [default: us-east-1]
-h, --help Show this help message
EXAMPLES:
# AWS deployment
$0 -p aws -e prod -b cudly-frontend-prod -d E1234567890ABC
# Azure deployment
$0 -p azure -e prod -b cudlyfrontendprod -d cudly-cdn-endpoint
# GCP deployment
$0 -p gcp -e prod -b cudly-frontend-prod -d cudly-url-map
EOF
exit 1
}
# Parse arguments
PROVIDER=""
ENVIRONMENT="dev"
BUCKET=""
DISTRIBUTION=""
REGION="us-east-1"
while [[ $# -gt 0 ]]; do
case $1 in
-p|--provider)
PROVIDER="$2"
shift 2
;;
-e|--environment)
ENVIRONMENT="$2"
shift 2
;;
-b|--bucket)
BUCKET="$2"
shift 2
;;
-d|--distribution)
DISTRIBUTION="$2"
shift 2
;;
-r|--region)
REGION="$2"
shift 2
;;
-h|--help)
usage
;;
*)
log_error "Unknown option: $1"
usage
;;
esac
done
# Validate required arguments
if [[ -z "$PROVIDER" ]]; then
log_error "Provider is required"
usage
fi
if [[ -z "$BUCKET" ]]; then
log_error "Bucket/storage account name is required"
usage
fi
# Validate provider
if [[ ! "$PROVIDER" =~ ^(aws|azure|gcp)$ ]]; then
log_error "Provider must be one of: aws, azure, gcp"
exit 1
fi
# Build frontend
log_info "Building frontend for $ENVIRONMENT environment..."
npm run build
if [[ ! -d "dist" ]]; then
log_error "Build failed: dist/ directory not found"
exit 1
fi
log_info "Build completed successfully"
# Deploy based on provider
case $PROVIDER in
aws)
log_info "Deploying to AWS S3: $BUCKET"
# Check AWS CLI
if ! command -v aws &> /dev/null; then
log_error "AWS CLI not found. Please install it first."
exit 1
fi
# Upload all files except index.html with long cache
log_info "Uploading static assets..."
aws s3 sync dist/ "s3://${BUCKET}/" \
--region "$REGION" \
--delete \
--cache-control "public,max-age=31536000,immutable" \
--exclude "index.html" \
--exclude "*.map"
# Upload index.html with short cache
log_info "Uploading index.html..."
aws s3 cp dist/index.html "s3://${BUCKET}/index.html" \
--region "$REGION" \
--cache-control "public,max-age=300" \
--content-type "text/html"
log_info "Upload completed"
# Invalidate CloudFront cache if distribution provided
if [[ -n "$DISTRIBUTION" ]]; then
log_info "Creating CloudFront invalidation..."
INVALIDATION_ID=$(aws cloudfront create-invalidation \
--distribution-id "$DISTRIBUTION" \
--paths "/*" \
--query 'Invalidation.Id' \
--output text)
log_info "Invalidation created: $INVALIDATION_ID"
log_info "Cache invalidation may take 5-10 minutes"
else
log_warn "No distribution ID provided, skipping cache invalidation"
fi
;;
azure)
log_info "Deploying to Azure Blob Storage: $BUCKET"
# Check Azure CLI
if ! command -v az &> /dev/null; then
log_error "Azure CLI not found. Please install it first."
exit 1
fi
# Upload files to $web container
log_info "Uploading files..."
az storage blob upload-batch \
--account-name "$BUCKET" \
--destination '$web' \
--source dist/ \
--overwrite \
--content-cache-control "public, max-age=31536000, immutable" \
--pattern "*" \
--exclude-pattern "index.html"
# Upload index.html separately
log_info "Uploading index.html..."
az storage blob upload \
--account-name "$BUCKET" \
--container-name '$web' \
--name index.html \
--file dist/index.html \
--overwrite \
--content-cache-control "public, max-age=300" \
--content-type "text/html"
log_info "Upload completed"
# Purge CDN cache if endpoint provided
if [[ -n "$DISTRIBUTION" ]]; then
log_info "Purging CDN cache..."
# Extract resource group and profile from tags or use defaults
RESOURCE_GROUP="${RESOURCE_GROUP:-cudly-${ENVIRONMENT}-rg}"
CDN_PROFILE="${CDN_PROFILE:-cudly-cdn-profile}"
az cdn endpoint purge \
--resource-group "$RESOURCE_GROUP" \
--profile-name "$CDN_PROFILE" \
--name "$DISTRIBUTION" \
--content-paths "/*"
log_info "CDN cache purged"
else
log_warn "No CDN endpoint provided, skipping cache purge"
fi
;;
gcp)
log_info "Deploying to Google Cloud Storage: $BUCKET"
# Check gcloud CLI
if ! command -v gsutil &> /dev/null; then
log_error "gsutil not found. Please install Google Cloud SDK first."
exit 1
fi
# Upload files
log_info "Uploading files..."
gsutil -m rsync -r -d \
-x ".*\.map$" \
dist/ "gs://${BUCKET}/"
# Set cache metadata for static assets
log_info "Setting cache metadata..."
gsutil -m setmeta \
-h "Cache-Control:public, max-age=31536000, immutable" \
"gs://${BUCKET}/js/**" 2>/dev/null || true
gsutil -m setmeta \
-h "Cache-Control:public, max-age=31536000, immutable" \
"gs://${BUCKET}/css/**" 2>/dev/null || true
# Set short cache for index.html
gsutil setmeta \
-h "Cache-Control:public, max-age=300" \
"gs://${BUCKET}/index.html"
log_info "Upload completed"
# Invalidate CDN cache if URL map provided
if [[ -n "$DISTRIBUTION" ]]; then
log_info "Invalidating Cloud CDN cache..."
gcloud compute url-maps invalidate-cdn-cache "$DISTRIBUTION" \
--path "/*" \
--async
log_info "Cache invalidation initiated"
else
log_warn "No URL map provided, skipping cache invalidation"
fi
;;
esac
log_info "✅ Deployment completed successfully!"
log_info ""
log_info "Summary:"
log_info " Provider: $PROVIDER"
log_info " Environment: $ENVIRONMENT"
log_info " Bucket: $BUCKET"
if [[ -n "$DISTRIBUTION" ]]; then
log_info " CDN ID: $DISTRIBUTION"
fi
# Show next steps
log_info ""
log_info "Next steps:"
log_info " 1. Wait for CDN cache invalidation to complete (5-10 minutes)"
log_info " 2. Test the frontend at your CDN URL"
log_info " 3. Check browser console for any errors"
log_info " 4. Verify API calls are working correctly"