#!/bin/bash set -e export DOCKER_BUILDKIT=1 export COMPOSE_DOCKER_CLI_BUILD=1 BRANCH="${1:-develop}" echo "Starting Deployment for branch: ${BRANCH}" if [ "$BRANCH" == "develop" ]; then COMPOSE_FILE="compose.stage.yml" TARGET_DIR="/opt/services/canina/stage" BACKEND_HOST="backend_stage" APP_HOST="stage.canina.ir" ADMIN_HOST="stageadmin.canina.ir" API_URL="https://stageapi.canina.ir" CONTAINER_PREFIX="canino_backend_stage" else COMPOSE_FILE="compose.prod.yml" TARGET_DIR="/opt/services/canina/prod" BACKEND_HOST="backend_prod" APP_HOST="canina.ir" ADMIN_HOST="admin.canina.ir" API_URL="https://api.canina.ir" CONTAINER_PREFIX="canino_backend_prod" fi mkdir -p "$TARGET_DIR" cd "$TARGET_DIR" if [ ! -d ".git" ]; then echo "Cloning repository..." git clone -b "$BRANCH" https://git.parsaaghayi.ir/parsa/canina.git . else echo "Pulling latest changes..." git fetch origin "$BRANCH" git reset --hard "origin/$BRANCH" fi echo "Fixing .dockerignore..." sed -i '/node_modules/d' .dockerignore 2>/dev/null || true sed -i '/node_modules/d' backend/.dockerignore 2>/dev/null || true sed -i '/^backend$/d' .dockerignore 2>/dev/null || true echo "Disabling TS checks for fast deploy..." sed -i 's|tsc -b \&\& vite build|vite build|g' frontend/admin-panel/package.json 2>/dev/null || true sed -i 's|tsc \&\& vite build|vite build|g' frontend/admin-panel/package.json 2>/dev/null || true # Copy scientificTerms.ts if needed cp frontend/application/lib/data/scientificTerms.ts backend/prisma/scientificTerms.ts 2>/dev/null || true # Prepare docker-compose.yml in TARGET_DIR cp "/opt/services/canina/$COMPOSE_FILE" "$TARGET_DIR/docker-compose.yml" sed -i 's/- REDIS_PORT: 6379/- REDIS_PORT=6379/g' "$TARGET_DIR/docker-compose.yml" 2>/dev/null || true # Prepare nginx.conf for current environment echo "Patching nginx.conf for environment..." cp nginx.conf nginx.conf.tmp sed -i "s/__BACKEND_HOST__/${BACKEND_HOST}/g" nginx.conf.tmp sed -i "s/__APP_HOST__/${APP_HOST}/g" nginx.conf.tmp sed -i "s/__ADMIN_HOST__/${ADMIN_HOST}/g" nginx.conf.tmp mv nginx.conf.tmp nginx.conf echo "Building Docker images..." docker compose build --build-arg NEXT_PUBLIC_API_URL="${API_URL}/api" --build-arg VITE_API_URL="${API_URL}" echo "Starting containers..." docker compose up -d echo "Waiting for backend to start..." sleep 15 echo "Running migrations and seeding database..." docker exec "$CONTAINER_PREFIX" npx prisma migrate deploy || true docker exec "$CONTAINER_PREFIX" npx prisma db seed || true echo "Deployment for $BRANCH completed successfully!"