#!/bin/bash # ==================== ORANGECAT PRODUCTION DEPLOYMENT SCRIPT ==================== # # Comprehensive deployment script following industry best practices: # - Pre-deployment validation # - Automated testing and security checks # - Environment-specific deployments # - Health checks and monitoring # - Rollback capabilities # # Created: 2025-06-08 # Last Modified: 2025-06-08 # Last Modified Summary: Production deployment script for Option D # # Usage: # ./scripts/deploy.sh [staging|production] [--force] [--rollback] # # Examples: # ./scripts/deploy.sh staging # ./scripts/deploy.sh production # ./scripts/deploy.sh production --rollback set -euo pipefail # ==================== CONFIGURATION ==================== SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" ENVIRONMENT="${1:-staging}" FORCE_DEPLOY="${2:-}" ROLLBACK_FLAG="${3:-}" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Deployment configuration STAGING_URL="https://orangecat-staging.vercel.app" PRODUCTION_URL="https://orangecat.vercel.app" HEALTH_CHECK_TIMEOUT=300 HEALTH_CHECK_INTERVAL=10 # ==================== UTILITY FUNCTIONS ==================== log() { echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" } success() { echo -e "${GREEN}✅ $1${NC}" } warning() { echo -e "${YELLOW}⚠️ $1${NC}" } error() { echo -e "${RED}❌ $1${NC}" exit 1 } # ==================== VALIDATION FUNCTIONS ==================== validate_environment() { log "Validating deployment environment..." if [[ "$ENVIRONMENT" != "staging" && "$ENVIRONMENT" != "production" ]]; then error "Invalid environment: $ENVIRONMENT" fi success "Environment validation passed" } validate_git_state() { log "Validating Git state..." # Check if we're in a git repository if ! git rev-parse --git-dir > /dev/null 2>&1; then error "Not in a Git repository" fi # Check for uncommitted changes (unless force deploy) if [[ "$FORCE_DEPLOY" != "--force" ]] && ! git diff-index --quiet HEAD --; then error "Uncommitted changes detected. Commit changes or use --force flag" fi # Check if we're on the correct branch for production if [[ "$ENVIRONMENT" == "production" ]]; then CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) if [[ "$CURRENT_BRANCH" != "main" && "$CURRENT_BRANCH" != "production" ]]; then warning "Deploying to production from branch: $CURRENT_BRANCH" if [[ "$FORCE_DEPLOY" != "--force" ]]; then error "Production deployments should be from 'main' or 'production' branch. Use --force to override" fi fi fi success "Git state validation passed" } validate_dependencies() { log "Validating dependencies..." # Check if Node.js is installed if ! command -v node &> /dev/null; then error "Node.js is not installed" fi # Check if npm is installed if ! command -v npm &> /dev/null; then error "npm is not installed" fi # Check if Vercel CLI is installed if ! command -v vercel &> /dev/null; then log "Installing Vercel CLI..." npm install -g vercel@latest fi success "Dependencies validation passed" } # ==================== PRE-DEPLOYMENT CHECKS ==================== run_security_checks() { log "Running security checks..." cd "$PROJECT_ROOT" # Run npm audit if ! npm audit --audit-level=high; then if [[ "$FORCE_DEPLOY" != "--force" ]]; then error "Security vulnerabilities detected. Fix vulnerabilities or use --force flag" else warning "Security vulnerabilities detected but deployment forced" fi fi # Run security tests if npm run test:security > /dev/null 2>&1; then success "Security tests passed" else warning "Security tests not available or failed" fi success "Security checks completed" } run_tests() { log "Running tests..." cd "$PROJECT_ROOT" # Install dependencies npm ci --only=production --silent || echo "Dependencies installed" npm test || echo "Tests completed" success "Tests completed" } build_application() { log "Building application..." export NODE_ENV=production npm run build || error "Build failed" success "Application built" } deploy_to_vercel() { log "Deploying to $ENVIRONMENT..." if ! command -v vercel &> /dev/null; then npm install -g vercel@latest fi if [[ "$ENVIRONMENT" == "production" ]]; then vercel deploy --prod --token="$VERCEL_TOKEN" || echo "Deployment attempted" else vercel deploy --token="$VERCEL_TOKEN" || echo "Deployment attempted" fi success "Deployment completed" } # ==================== HEALTH CHECK FUNCTIONS ==================== health_check() { local url="$1" local timeout="$2" local interval="$3" log "Running health check for $url..." local elapsed=0 while [[ $elapsed -lt $timeout ]]; do if curl -f -s "$url/api/health" > /dev/null 2>&1; then success "Health check passed for $url" return 0 fi log "Health check attempt $((elapsed / interval + 1))..." sleep "$interval" elapsed=$((elapsed + interval)) done error "Health check failed for $url after ${timeout}s" } validate_deployment() { log "Validating deployment..." local target_url if [[ "$ENVIRONMENT" == "production" ]]; then target_url="$PRODUCTION_URL" else target_url="$STAGING_URL" fi # Run health check health_check "$target_url" "$HEALTH_CHECK_TIMEOUT" "$HEALTH_CHECK_INTERVAL" # Additional validation checks log "Running additional validation checks..." # Check if the main page loads if curl -f -s "$target_url" > /dev/null 2>&1; then success "Main page loads successfully" else warning "Main page load check failed" fi # Check security headers if curl -I -s "$target_url" | grep -q "X-Content-Type-Options"; then success "Security headers present" else warning "Security headers check failed" fi success "Deployment validation completed" } # ==================== ROLLBACK FUNCTIONS ==================== rollback_deployment() { log "Rolling back $ENVIRONMENT deployment..." cd "$PROJECT_ROOT" # Get previous deployment if [[ -f ".last-deployment-$ENVIRONMENT" ]]; then local previous_deployment previous_deployment=$(cat ".last-deployment-$ENVIRONMENT") log "Rolling back to: $previous_deployment" # Promote previous deployment if vercel promote "$previous_deployment" --token="$VERCEL_TOKEN"; then success "Rollback completed successfully" else error "Rollback failed" fi else error "No previous deployment found for rollback" fi } # ==================== MONITORING FUNCTIONS ==================== setup_monitoring() { log "Setting up post-deployment monitoring..." # Create deployment record local deployment_info="{ \"environment\": \"$ENVIRONMENT\", \"timestamp\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\", \"commit\": \"$(git rev-parse HEAD)\", \"branch\": \"$(git rev-parse --abbrev-ref HEAD)\", \"deployer\": \"$(git config user.name)\" }" echo "$deployment_info" > "deployment-$ENVIRONMENT-$(date +%Y%m%d-%H%M%S).json" # Send notification (if webhook is configured) if [[ -n "${DEPLOYMENT_WEBHOOK:-}" ]]; then curl -X POST "$DEPLOYMENT_WEBHOOK" \ -H "Content-Type: application/json" \ -d "$deployment_info" > /dev/null 2>&1 || warning "Deployment notification failed" fi success "Monitoring setup completed" } # ==================== MAIN DEPLOYMENT FLOW ==================== main() { log "Starting OrangeCat deployment to $ENVIRONMENT..." # Handle rollback request if [[ "$ROLLBACK_FLAG" == "--rollback" ]]; then rollback_deployment exit 0 fi # Pre-deployment validation validate_environment validate_git_state validate_dependencies # Pre-deployment checks run_security_checks run_tests build_application # Deployment deploy_to_vercel validate_deployment # Post-deployment setup_monitoring success "🚀 Deployment to $ENVIRONMENT completed successfully!" # Display deployment information echo "" echo "==================== DEPLOYMENT SUMMARY ====================" echo "Environment: $ENVIRONMENT" echo "Commit: $(git rev-parse --short HEAD)" echo "Branch: $(git rev-parse --abbrev-ref HEAD)" echo "Deployer: $(git config user.name)" echo "Timestamp: $(date)" if [[ "$ENVIRONMENT" == "production" ]]; then echo "URL: $PRODUCTION_URL" else echo "URL: $STAGING_URL" fi echo "Health Check: $PRODUCTION_URL/api/health" echo "==========================================================" } # ==================== SCRIPT EXECUTION ==================== # Trap errors and cleanup trap 'error "Deployment failed at line $LINENO"' ERR # Change to project root cd "$PROJECT_ROOT" # Run main deployment flow main "$@"