#!/bin/bash # Deploy production environment for cmc-sales # Usage: ./deploy-prod.sh [--no-cache] USE_CACHE=true for arg in "$@"; do if [[ "$arg" == "--no-cache" ]]; then USE_CACHE=false echo "No cache flag detected: will rebuild images without cache." fi done if [[ "$USE_CACHE" == "true" ]]; then echo "Using cached layers for docker build." fi echo "Starting production deployment for cmc-sales..." echo "Setting variables..." SERVER="cmc" REPO="git@code.springupsoftware.com:cmc/cmc-sales.git" BRANCH="prod" PROD_DIR="cmc-sales-prod" echo "Connecting to server $SERVER via SSH..." # Pass variables into SSH session ssh $SERVER \ "SERVER=$SERVER REPO='$REPO' BRANCH='$BRANCH' PROD_DIR='$PROD_DIR' USE_CACHE='$USE_CACHE' bash -s" << 'ENDSSH' set -e echo "Connected to $SERVER." cd /home/cmc # Clone or update production branch if [ -d "$PROD_DIR" ]; then echo "Updating existing production directory $PROD_DIR..." cd "$PROD_DIR" git fetch origin git checkout $BRANCH git reset --hard origin/$BRANCH else echo "Cloning repository $REPO to $PROD_DIR..." git clone -b $BRANCH $REPO $PROD_DIR cd "$PROD_DIR" fi # Create .env.prod file for docker-compose if it doesn't exist COMPOSE_ENV_PATH="/home/cmc/$PROD_DIR/.env.prod" if [ ! -f "$COMPOSE_ENV_PATH" ]; then echo "Creating .env.prod file for docker-compose..." cat > "$COMPOSE_ENV_PATH" <<'COMPOSEENVEOF' # SMTP Configuration for postfix relay SMTP_USERNAME=sales SMTP_PASSWORD=S%s'mMZ})MGsg$k!5N|mPSQ> COMPOSEENVEOF else echo ".env.prod already exists, skipping creation..." fi # Create .env file for go-app if it doesn't exist ENV_PATH="/home/cmc/$PROD_DIR/go-app/.env" echo "(Re)creating .env file for go-app..." cat > "$ENV_PATH" <<'ENVEOF' # Database configuration DB_HOST=db DB_PORT=3306 DB_USER=cmc DB_PASSWORD=xVRQI&cA?7AU=hqJ!%au DB_NAME=cmc # Root database password (for dbshell-root) DB_ROOT_PASSWORD=secureRootPassword # Environment variables for Go app mail configuration SMTP_HOST=postfix SMTP_PORT=25 SMTP_USER="" SMTP_PASS="" SMTP_FROM="CMC Sales " ENVEOF if [[ "$USE_CACHE" == "false" ]]; then echo "Building and starting docker compose for production (no cache)..." docker compose --env-file .env.prod -f docker-compose.prod.yml build --no-cache docker compose --env-file .env.prod -f docker-compose.prod.yml up -d --remove-orphans else echo "Building and starting docker compose for production (using cache)..." docker compose --env-file .env.prod -f docker-compose.prod.yml build docker compose --env-file .env.prod -f docker-compose.prod.yml up -d --remove-orphans fi echo "Checking running containers..." echo "Production deployment complete." ENDSSH