#!/bin/bash
# Pre-deployment validation script
# Run this before deploying to catch configuration issues
set -e
echo "π Validating deployment configuration..."
echo ""
ERRORS=0
# Check 1: Verify workflow doesn't have --set-env-vars or --update-secrets
echo "β Checking GitHub Actions workflow..."
if grep -q "\-\-set-env-vars" .github/workflows/deploy-frontend.yml; then
echo "β ERROR: deploy-frontend.yml contains --set-env-vars"
echo " This will REPLACE all environment variables and break production!"
echo " Remove this flag - the workflow should only update the image."
((ERRORS++))
fi
if grep -q "\-\-update-secrets" .github/workflows/deploy-frontend.yml; then
echo "β ERROR: deploy-frontend.yml contains --update-secrets"
echo " This will REPLACE secret references and break production!"
echo " Remove this flag - the workflow should only update the image."
((ERRORS++))
fi
if [ $ERRORS -eq 0 ]; then
echo " β Workflow only updates image (correct)"
fi
echo ""
# Check 2: Verify all NEXT_PUBLIC_ build args are in workflow
echo "β Checking NEXT_PUBLIC_ build arguments..."
DOCKERFILE="packages/frontend/Dockerfile"
WORKFLOW=".github/workflows/deploy-frontend.yml"
# Extract ARG NEXT_PUBLIC_ from Dockerfile
DOCKERFILE_ARGS=$(grep "^ARG NEXT_PUBLIC_" $DOCKERFILE | sed 's/ARG //' | sort)
# Extract --build-arg NEXT_PUBLIC_ from workflow
WORKFLOW_ARGS=$(grep "\-\-build-arg NEXT_PUBLIC_" $WORKFLOW | sed 's/.*--build-arg //' | sed 's/=.*//' | sort)
if [ "$DOCKERFILE_ARGS" != "$WORKFLOW_ARGS" ]; then
echo "β ERROR: Mismatch between Dockerfile and workflow build args"
echo ""
echo "Dockerfile ARGs:"
echo "$DOCKERFILE_ARGS"
echo ""
echo "Workflow --build-args:"
echo "$WORKFLOW_ARGS"
echo ""
echo "These must match exactly!"
((ERRORS++))
else
echo " β All NEXT_PUBLIC_ variables present in both"
fi
echo ""
# Check 3: Verify design-system build step exists
echo "β Checking design-system build step..."
if ! grep -q "pnpm --filter @canadagpt/design-system build" $WORKFLOW; then
echo "β ERROR: Workflow missing design-system build step"
echo " Docker build will fail without packages/design-system/dist"
((ERRORS++))
else
echo " β Design-system build step present"
fi
echo ""
# Check 4: TypeScript compilation
echo "β Checking TypeScript..."
cd packages/frontend
if ! pnpm type-check > /dev/null 2>&1; then
echo "β ERROR: TypeScript errors in frontend"
echo " Run 'pnpm --filter @canadagpt/frontend type-check' to see details"
((ERRORS++))
else
echo " β No TypeScript errors"
fi
cd ../..
echo ""
# Check 5: Verify Cloud Run service exists
echo "β Checking Cloud Run service..."
if ! gcloud run services describe canadagpt-frontend --region=us-central1 > /dev/null 2>&1; then
echo "β οΈ WARNING: Cannot reach Cloud Run service (may not be authenticated)"
else
echo " β Cloud Run service accessible"
# Check current environment variables
CURRENT_ENV_VARS=$(gcloud run services describe canadagpt-frontend --region=us-central1 --format=yaml | grep -E "name: (AUTH_TRUST_HOST|NEXTAUTH_URL|NODE_ENV)" | wc -l | tr -d ' ')
if [ "$CURRENT_ENV_VARS" -lt 3 ]; then
echo "β οΈ WARNING: Expected environment variables may be missing from Cloud Run"
echo " Check with: gcloud run services describe canadagpt-frontend --region=us-central1"
else
echo " β Core environment variables present"
fi
fi
echo ""
# Summary
echo "βββββββββββββββββββββββββββββββββββββββββ"
if [ $ERRORS -eq 0 ]; then
echo "β All checks passed! Safe to deploy."
echo "βββββββββββββββββββββββββββββββββββββββββ"
exit 0
else
echo "β $ERRORS error(s) found. DO NOT DEPLOY."
echo "βββββββββββββββββββββββββββββββββββββββββ"
echo ""
echo "Fix these issues before deploying to production."
echo "See DEPLOYMENT.md for troubleshooting guidance."
exit 1
fi