-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·640 lines (550 loc) · 22.5 KB
/
deploy.sh
File metadata and controls
executable file
·640 lines (550 loc) · 22.5 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#!/bin/bash
# CI/CD Deployment Script
# Automatically deploys backend to Render FIRST, then frontend to Vercel
set -e # Exit on error
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Print colored output
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
sanitize_vercel_url() {
local raw="$1"
local cleaned
cleaned=$(printf '%s' "$raw" | tr -d '\r' | sed -E 's/(Queued|Ready|Building|Initializing|Deploying|Error)$//' | sed 's/[[:space:]]*$//')
printf '%s' "$cleaned"
}
# Banner
echo ""
echo "=========================================="
echo " CI/CD Deployment Script"
echo "=========================================="
echo ""
# Load environment variables from .env file if it exists
if [ -f ".env" ]; then
print_info "Loading environment variables from .env file..."
export $(cat .env | grep -v '^#' | grep -v '^[[:space:]]*$' | xargs)
print_success "Environment variables loaded from .env"
echo ""
fi
# Default flags (can be overridden via CLI arguments)
SKIP_RENDER=false
SKIP_RENDER_DEPLOY=false
SKIP_VERCEL_DEPLOY=false
OVERRIDE_BRANCH=""
# Parse optional arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--skip-render)
SKIP_RENDER=true
;;
--skip-render-deploy)
SKIP_RENDER_DEPLOY=true
;;
--skip-vercel-deploy)
SKIP_VERCEL_DEPLOY=true
;;
--branch)
if [ -z "$2" ]; then
print_error "Missing value for --branch"
exit 1
fi
OVERRIDE_BRANCH="$2"
shift
;;
*)
print_warning "Unknown argument '$1' (ignored)."
;;
esac
shift
done
# Step 1: Detect current branch (or use override)
if [ -n "$OVERRIDE_BRANCH" ]; then
BRANCH="$OVERRIDE_BRANCH"
print_info "Using override branch: $BRANCH"
else
print_info "Detecting current git branch..."
BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
if [ $? -ne 0 ] || [ -z "$BRANCH" ]; then
print_error "Failed to detect git branch. Are you in a git repository?"
exit 1
fi
fi
print_success "Current branch: $BRANCH"
echo ""
# Step 2: Check for required CLI tools
print_info "Checking for required CLI tools..."
# Check for Vercel CLI
if ! command -v vercel &> /dev/null; then
print_error "Vercel CLI not found. Please install it:"
echo " npm install -g vercel"
exit 1
fi
print_success "Vercel CLI found"
# Check for Render CLI (optional now that we use the API)
if ! command -v render &> /dev/null; then
print_warning "Render CLI not found. Render deployments will use the API only."
else
print_success "Render CLI found"
fi
echo ""
# Step 3: Check authentication
print_info "Checking authentication..."
# Check Vercel authentication - prioritize local login over token
print_info "Checking Vercel authentication..."
# First check if we're logged in locally
if vercel whoami &> /dev/null; then
VERCEL_USER=$(vercel whoami 2>/dev/null | head -1)
print_success "Logged in to Vercel as: $VERCEL_USER"
print_info "Using local Vercel credentials (not using VERCEL_TOKEN)"
# Unset VERCEL_TOKEN to force use of local credentials
unset VERCEL_TOKEN
elif [ -n "$VERCEL_TOKEN" ]; then
# Try to validate the token
print_info "Found VERCEL_TOKEN, validating..."
if vercel whoami --token "$VERCEL_TOKEN" &> /dev/null; then
VERCEL_USER=$(vercel whoami --token "$VERCEL_TOKEN" 2>/dev/null | head -1)
print_success "VERCEL_TOKEN is valid for user: $VERCEL_USER"
else
print_error "VERCEL_TOKEN is invalid"
print_info "Please either:"
echo " 1. Run 'vercel login' to authenticate locally, OR"
echo " 2. Update VERCEL_TOKEN in .env file with a valid token from:"
echo " https://vercel.com/account/tokens"
exit 1
fi
else
print_warning "Not authenticated with Vercel"
print_info "Please login to Vercel..."
vercel login
if ! vercel whoami &> /dev/null; then
print_error "Failed to authenticate with Vercel"
exit 1
fi
VERCEL_USER=$(vercel whoami 2>/dev/null | head -1)
print_success "Successfully logged in as: $VERCEL_USER"
fi
# Check Render authentication
if [ "$SKIP_RENDER" = false ]; then
if [ -z "$RENDER_API_KEY" ]; then
print_warning "RENDER_API_KEY not found in environment"
print_info "Please set your Render API key:"
echo " export RENDER_API_KEY=your_api_key_here"
echo ""
print_info "Get your API key from: https://dashboard.render.com/u/settings#api-keys"
echo ""
read -p "Do you want to continue without deploying to Render? (y/n) " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
SKIP_RENDER=true
else
print_success "RENDER_API_KEY found"
fi
fi
echo ""
# Step 4: Deploy Backend to Render (FIRST, so we can get the URL for frontend)
# Initialize backend URL - will be set from Render deployment or use default
BACKEND_URL=""
# Determine expected backend service name based on branch
case "$BRANCH" in
prod|production|main)
EXPECTED_SERVICE_NAME="cicd-demo-backend-prod"
;;
gamma)
EXPECTED_SERVICE_NAME="cicd-demo-backend-gamma"
;;
beta)
EXPECTED_SERVICE_NAME="cicd-demo-backend-beta"
;;
*)
# Sanitize branch name for service name
SANITIZED_BRANCH=$(echo "$BRANCH" | sed 's/[^a-zA-Z0-9]/-/g' | tr '[:upper:]' '[:lower:]')
EXPECTED_SERVICE_NAME="cicd-demo-backend-$SANITIZED_BRANCH"
;;
esac
# Set default backend URL based on expected service name
DEFAULT_BACKEND_URL="https://${EXPECTED_SERVICE_NAME}.onrender.com"
if [ "$SKIP_RENDER" = false ]; then
print_info "Deploying backend to Render..."
cd backend
if ! command -v jq &> /dev/null; then
print_error "'jq' is not installed, but is required to use the Render API."
print_info "Please install it to continue (e.g., 'brew install jq' on macOS)."
exit 1
fi
# Determine service name based on branch
case "$BRANCH" in
prod|production|main)
SERVICE_NAME="cicd-demo-backend-prod"
;;
gamma)
SERVICE_NAME="cicd-demo-backend-gamma"
;;
beta)
SERVICE_NAME="cicd-demo-backend-beta"
;;
*)
# Sanitize branch name for service name (replace special chars with hyphens)
SANITIZED_BRANCH=$(echo "$BRANCH" | sed 's/[^a-zA-Z0-9]/-/g' | tr '[:upper:]' '[:lower:]')
SERVICE_NAME="cicd-demo-backend-$SANITIZED_BRANCH"
;;
esac
print_info "Service name: $SERVICE_NAME"
if [ "$SKIP_RENDER_DEPLOY" = true ]; then
print_info "Skipping Render deployment (flag set). Fetching service info via Render CLI..."
if ! command -v render &> /dev/null; then
print_error "Render CLI is required when using --skip-render-deploy."
exit 1
fi
SERVICE_RESPONSE_FILE=$(mktemp)
if ! render services --output json --confirm > "$SERVICE_RESPONSE_FILE" 2>/dev/null; then
print_error "Failed to query services via Render CLI. Ensure you're logged in (render login) and have a workspace selected (render workspace set)."
rm -f "$SERVICE_RESPONSE_FILE"
exit 1
fi
SERVICE_JSON=$(jq -c --arg name "$SERVICE_NAME" '
(map(
if has("service") then .service else .
end
) | map(select(.name == $name)) | first) // empty
' "$SERVICE_RESPONSE_FILE")
rm -f "$SERVICE_RESPONSE_FILE"
if [ -z "$SERVICE_JSON" ] || [ "$SERVICE_JSON" = "null" ]; then
print_error "Render CLI did not return a service named '$SERVICE_NAME'."
exit 1
fi
SERVICE_ID=$(echo "$SERVICE_JSON" | jq -r '.id // empty')
SERVICE_DASHBOARD_URL=$(echo "$SERVICE_JSON" | jq -r '.dashboardUrl // ""')
BACKEND_URL=$(echo "$SERVICE_JSON" | jq -r '.serviceDetails.url // empty')
if [ -z "$BACKEND_URL" ] || [ "$BACKEND_URL" = "null" ]; then
BACKEND_URL="$DEFAULT_BACKEND_URL"
fi
print_success "Found existing Render service via CLI (ID: $SERVICE_ID)"
if [ -n "$SERVICE_DASHBOARD_URL" ] && [ "$SERVICE_DASHBOARD_URL" != "null" ]; then
print_info "Dashboard URL: $SERVICE_DASHBOARD_URL"
fi
print_info "Backend URL from Render CLI: $BACKEND_URL"
else
RENDER_API_BASE_URL="https://api.render.com/v1"
SERVICE_JSON=""
NEXT_CURSOR=""
print_info "Checking if Render service '$SERVICE_NAME' exists..."
while true; do
SERVICE_LIST_URL="${RENDER_API_BASE_URL}/services?limit=100"
if [ -n "$NEXT_CURSOR" ]; then
SERVICE_LIST_URL="${SERVICE_LIST_URL}&cursor=${NEXT_CURSOR}"
fi
SERVICE_RESPONSE_FILE=$(mktemp)
SERVICE_HTTP_STATUS=$(curl -sS -H "Authorization: Bearer $RENDER_API_KEY" -o "$SERVICE_RESPONSE_FILE" -w "%{http_code}" "$SERVICE_LIST_URL")
CURL_EXIT_CODE=$?
if [ $CURL_EXIT_CODE -ne 0 ]; then
SERVICE_HTTP_STATUS=0
fi
if [ "$SERVICE_HTTP_STATUS" -ne 200 ]; then
print_error "Failed to query Render services (HTTP $SERVICE_HTTP_STATUS)."
if [ -s "$SERVICE_RESPONSE_FILE" ]; then
print_warning "Render API response:"
sed 's/^/ /' "$SERVICE_RESPONSE_FILE"
fi
rm -f "$SERVICE_RESPONSE_FILE"
exit 1
fi
SERVICE_JSON=$(jq -c --arg name "$SERVICE_NAME" 'map(select(.service.name == $name)) | first // empty' "$SERVICE_RESPONSE_FILE")
if [ -n "$SERVICE_JSON" ] && [ "$SERVICE_JSON" != "null" ]; then
rm -f "$SERVICE_RESPONSE_FILE"
break
fi
NEXT_CURSOR=$(jq -r '.[-1].cursor // empty' "$SERVICE_RESPONSE_FILE")
rm -f "$SERVICE_RESPONSE_FILE"
if [ -z "$NEXT_CURSOR" ]; then
break
fi
done
if [ -z "$SERVICE_JSON" ] || [ "$SERVICE_JSON" = "null" ]; then
print_warning "Service '$SERVICE_NAME' not found on Render."
print_error "Backend service must be created manually (one-time setup)."
echo ""
print_info "Please create the Render service manually from your dashboard:"
echo " 1. Go to: https://dashboard.render.com/create?type=web"
echo " 2. Connect your GitHub repository: acm-industry/cicd-demo"
echo " 3. Configure the service with these exact settings:"
echo " - Name: $SERVICE_NAME"
echo " - Branch: $BRANCH"
echo " - Root Directory: backend"
echo " - Runtime: Python 3"
echo " - Build Command: pip install -r requirements.txt"
echo " - Start Command: python server.py"
echo " - Plan: Free"
echo " 4. Add Environment Variables:"
echo " - FLASK_ENV=production"
echo " - PORT=8080"
echo " - PYTHON_VERSION=3.11.0"
echo " 5. Set Health Check Path: /get-test"
echo ""
print_info "After creating the service, re-run this script to deploy."
echo ""
exit 1
else
SERVICE_ID=$(echo "$SERVICE_JSON" | jq -r '.service.id')
SERVICE_DASHBOARD_URL=$(echo "$SERVICE_JSON" | jq -r '.service.dashboardUrl // ""')
BACKEND_URL=$(echo "$SERVICE_JSON" | jq -r '.service.serviceDetails.url // empty')
if [ -z "$BACKEND_URL" ] || [ "$BACKEND_URL" = "null" ]; then
BACKEND_URL="$DEFAULT_BACKEND_URL"
fi
print_success "Service '$SERVICE_NAME' found with ID: $SERVICE_ID"
if [ -n "$SERVICE_DASHBOARD_URL" ]; then
print_info "Dashboard URL: $SERVICE_DASHBOARD_URL"
fi
print_info "Triggering deployment via Render API..."
DEPLOY_PAYLOAD=$(jq -n --arg clear "do_not_clear" '{"clearCache":$clear}')
DEPLOY_RESPONSE_FILE=$(mktemp)
DEPLOY_HTTP_STATUS=$(curl -sS -X POST \
-H "Authorization: Bearer $RENDER_API_KEY" \
-H "Content-Type: application/json" \
-o "$DEPLOY_RESPONSE_FILE" \
-w "%{http_code}" \
-d "$DEPLOY_PAYLOAD" \
"${RENDER_API_BASE_URL}/services/${SERVICE_ID}/deploys")
CURL_EXIT_CODE=$?
if [ $CURL_EXIT_CODE -ne 0 ]; then
DEPLOY_HTTP_STATUS=0
fi
if [ "$DEPLOY_HTTP_STATUS" -lt 200 ] || [ "$DEPLOY_HTTP_STATUS" -ge 300 ]; then
print_error "Failed to trigger Render deployment (HTTP $DEPLOY_HTTP_STATUS)."
if [ -s "$DEPLOY_RESPONSE_FILE" ]; then
print_warning "Render API response:"
sed 's/^/ /' "$DEPLOY_RESPONSE_FILE"
fi
rm -f "$DEPLOY_RESPONSE_FILE"
exit 1
fi
DEPLOY_ID=$(jq -r '.id // empty' "$DEPLOY_RESPONSE_FILE")
rm -f "$DEPLOY_RESPONSE_FILE"
if [ -z "$DEPLOY_ID" ]; then
print_error "Render API response did not include a deploy ID."
exit 1
fi
print_info "Triggered Render deploy (ID: $DEPLOY_ID). Waiting for completion..."
MAX_WAIT_SECONDS=${RENDER_DEPLOY_TIMEOUT:-900}
POLL_INTERVAL=5
ELAPSED=0
DEPLOY_STATUS=""
DEPLOY_FAILED=false
while [ $ELAPSED -lt $MAX_WAIT_SECONDS ]; do
STATUS_RESPONSE_FILE=$(mktemp)
STATUS_HTTP_STATUS=$(curl -sS \
-H "Authorization: Bearer $RENDER_API_KEY" \
-o "$STATUS_RESPONSE_FILE" \
-w "%{http_code}" \
"${RENDER_API_BASE_URL}/services/${SERVICE_ID}/deploys/${DEPLOY_ID}")
CURL_EXIT_CODE=$?
if [ $CURL_EXIT_CODE -ne 0 ]; then
STATUS_HTTP_STATUS=0
fi
if [ "$STATUS_HTTP_STATUS" -ne 200 ]; then
print_warning "Could not fetch Render deploy status (HTTP $STATUS_HTTP_STATUS). Retrying..."
rm -f "$STATUS_RESPONSE_FILE"
sleep $POLL_INTERVAL
ELAPSED=$((ELAPSED + POLL_INTERVAL))
continue
fi
DEPLOY_STATUS=$(jq -r '.status // empty' "$STATUS_RESPONSE_FILE")
rm -f "$STATUS_RESPONSE_FILE"
case "$DEPLOY_STATUS" in
live|deployed|succeeded|ready)
print_success "Backend deployed successfully (status: $DEPLOY_STATUS)"
break
;;
build_failed|failed|canceled|cancelled|timed_out|deactivated)
DEPLOY_FAILED=true
print_error "Render deployment failed (status: $DEPLOY_STATUS)"
break
;;
*)
print_info "Render deploy status: $DEPLOY_STATUS (waiting...)"
sleep $POLL_INTERVAL
ELAPSED=$((ELAPSED + POLL_INTERVAL))
;;
esac
done
if [ "$DEPLOY_FAILED" = true ]; then
exit 1
fi
if [ $ELAPSED -ge $MAX_WAIT_SECONDS ] && [ "$DEPLOY_STATUS" != "live" ] && [ "$DEPLOY_STATUS" != "ready" ] && [ "$DEPLOY_STATUS" != "deployed" ] && [ "$DEPLOY_STATUS" != "succeeded" ]; then
print_warning "Timed out waiting for Render deployment to finish."
print_warning "Continuing, but verify the backend deployment in the Render dashboard."
fi
print_info "Backend URL: $BACKEND_URL"
fi
fi
cd ..
else
print_warning "Skipping Render deployment"
print_info "Using default backend URL: $DEFAULT_BACKEND_URL"
BACKEND_URL="$DEFAULT_BACKEND_URL"
fi
# Ensure we have a backend URL for frontend deployment
if [ -z "$BACKEND_URL" ]; then
print_warning "No backend URL available, using default"
BACKEND_URL="$DEFAULT_BACKEND_URL"
fi
print_info "Backend URL for frontend: $BACKEND_URL"
echo ""
# Step 5: Deploy Frontend to Vercel (SECOND, using backend URL)
print_info "Deploying frontend to Vercel..."
cd frontend
# Determine deployment environment and project name based on branch
VERCEL_DEPLOY_FLAGS=()
case "$BRANCH" in
prod|production|main)
VERCEL_ENV_TARGET="production"
VERCEL_DEPLOY_FLAGS=("--prod")
PROJECT_NAME="prod-cicd-demo"
print_info "Deploying to PRODUCTION environment"
;;
gamma)
VERCEL_ENV_TARGET="preview"
PROJECT_NAME="gamma-cicd-demo"
print_info "Deploying to GAMMA (pre-production) environment as a Vercel PREVIEW"
;;
beta)
VERCEL_ENV_TARGET="preview"
PROJECT_NAME="beta-cicd-demo"
print_info "Deploying to BETA (staging) environment as a Vercel PREVIEW"
;;
*)
VERCEL_ENV_TARGET="preview"
# Sanitize branch name for project name (replace special chars with hyphens)
SANITIZED_BRANCH=$(echo "$BRANCH" | sed 's/[^a-zA-Z0-9]/-/g' | tr '[:upper:]' '[:lower:]')
PROJECT_NAME="${SANITIZED_BRANCH}-cicd-demo"
print_info "Deploying to PREVIEW environment for branch '$BRANCH'"
;;
esac
print_info "Project name: $PROJECT_NAME"
VERCEL_TOKEN_FLAG=()
if [ -n "$VERCEL_TOKEN" ]; then
VERCEL_TOKEN_FLAG=("--token" "$VERCEL_TOKEN")
fi
if [ "$SKIP_VERCEL_DEPLOY" = true ]; then
print_info "Skipping Vercel deployment (flag set). Fetching latest deployment URL..."
LIST_OUTPUT=$(mktemp)
LIST_CMD_ARGS=("vercel" "list" "$PROJECT_NAME" "--status" "READY" "--environment" "$VERCEL_ENV_TARGET" "--yes")
LIST_CMD_ARGS+=("${VERCEL_TOKEN_FLAG[@]}")
if env -u VERCEL_PROJECT_ID -u VERCEL_ORG_ID "${LIST_CMD_ARGS[@]}" > "$LIST_OUTPUT" 2>&1; then
VERCEL_URL=$(grep -Eo 'https://[^[:space:]]+' "$LIST_OUTPUT" | head -1)
if [ -n "$VERCEL_URL" ]; then
VERCEL_URL=$(sanitize_vercel_url "$VERCEL_URL")
print_success "Latest Vercel deployment detected: $VERCEL_URL"
else
print_warning "Could not parse deployment URL from Vercel CLI output."
VERCEL_URL="(unavailable)"
fi
else
print_error "Failed to list Vercel deployments."
sed 's/^/ /' "$LIST_OUTPUT"
rm -f "$LIST_OUTPUT"
cd ..
exit 1
fi
rm -f "$LIST_OUTPUT"
else
# Create or update vercel.json to set the project name
print_info "Configuring Vercel project name..."
if [ -f "vercel.json" ]; then
# Backup existing vercel.json
cp vercel.json vercel.json.bak
# Update the name field or add it if it doesn't exist
if grep -q '"name"' vercel.json; then
# Replace existing name
sed -i.tmp "s/\"name\"[[:space:]]*:[[:space:]]*\"[^\"]*\"/\"name\": \"$PROJECT_NAME\"/" vercel.json && rm -f vercel.json.tmp
else
# Add name field to JSON
sed -i.tmp 's/{/{\n "name": "'"$PROJECT_NAME"'",/' vercel.json && rm -f vercel.json.tmp
fi
else
# Create new vercel.json
cat > vercel.json <<EOF
{
"name": "$PROJECT_NAME"
}
EOF
fi
# Ensure this directory is linked to the desired Vercel project
if [ ! -f ".vercel/project.json" ]; then
print_info "Linking local directory to Vercel project '$PROJECT_NAME'..."
vercel link --project "$PROJECT_NAME" --yes "${VERCEL_TOKEN_FLAG[@]}"
fi
# After vercel.json is configured, link the backend URL as a project environment variable
if [ -n "$BACKEND_URL" ]; then
print_info "Setting NEXT_PUBLIC_API_URL for Vercel project '$PROJECT_NAME'..."
env -u VERCEL_PROJECT_ID -u VERCEL_ORG_ID \
vercel env rm NEXT_PUBLIC_API_URL "$VERCEL_ENV_TARGET" -y "${VERCEL_TOKEN_FLAG[@]}" 2>/dev/null || true
if printf '%s' "$BACKEND_URL" | env -u VERCEL_PROJECT_ID -u VERCEL_ORG_ID \
vercel env add NEXT_PUBLIC_API_URL "$VERCEL_ENV_TARGET" "${VERCEL_TOKEN_FLAG[@]}"; then
VERCEL_ENV_EXIT_CODE=0
else
VERCEL_ENV_EXIT_CODE=${PIPESTATUS[1]}
fi
if [ $VERCEL_ENV_EXIT_CODE -ne 0 ]; then
print_warning "Failed to add environment variable to Vercel project."
print_warning "The deployment will proceed, but git-based deployments may not have the correct backend URL."
else
print_success "Set NEXT_PUBLIC_API_URL for the '$VERCEL_ENV_TARGET' environment."
fi
fi
# Deploy to Vercel (will create project if it doesn't exist)
print_info "Deploying to Vercel..."
echo ""
# Create a temporary file to capture output while also displaying it
TEMP_OUTPUT=$(mktemp)
DEPLOY_CMD_ARGS=("vercel" "deploy" "--yes")
DEPLOY_CMD_ARGS+=("${VERCEL_DEPLOY_FLAGS[@]}")
DEPLOY_CMD_ARGS+=("-m" "githubDeployment=1" "-m" "githubCommitRef=$BRANCH")
DEPLOY_CMD_ARGS+=("--build-env" "NEXT_PUBLIC_API_URL=$BACKEND_URL" "--env" "NEXT_PUBLIC_API_URL=$BACKEND_URL")
DEPLOY_CMD_ARGS+=("${VERCEL_TOKEN_FLAG[@]}")
env -u VERCEL_PROJECT_ID -u VERCEL_ORG_ID "${DEPLOY_CMD_ARGS[@]}" 2>&1 | tee "$TEMP_OUTPUT"
DEPLOY_EXIT_CODE=${PIPESTATUS[0]}
echo ""
# Restore original vercel.json if we backed it up
if [ -f "vercel.json.bak" ]; then
mv vercel.json.bak vercel.json
fi
# Extract URL from the output
VERCEL_URL=$(grep -Eo 'https://[^ ]+' "$TEMP_OUTPUT" | tail -1)
if [ -n "$VERCEL_URL" ]; then
VERCEL_URL=$(sanitize_vercel_url "$VERCEL_URL")
fi
rm -f "$TEMP_OUTPUT"
if [ $DEPLOY_EXIT_CODE -ne 0 ] || [ -z "$VERCEL_URL" ]; then
print_error "Failed to deploy frontend to Vercel"
cd ..
exit 1
fi
print_success "Frontend deployed to: $VERCEL_URL"
fi
cd ..
echo ""
print_success "=========================================="
print_success " Deployment Complete!"
print_success "=========================================="
echo ""
print_info "Frontend URL: $VERCEL_URL"
if [ -n "$BACKEND_URL" ]; then
print_info "Backend URL: $BACKEND_URL"
fi
echo ""