#!/bin/bash # Deploy staging environment for cmc-sales # Usage: ./deploy-stg.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 staging deployment for cmc-sales..." echo "Setting variables..." SERVER="cmc" REPO="git@code.springupsoftware.com:cmc/cmc-sales.git" BRANCH="stg" STAGING_DIR="cmc-sales-staging" echo "Connecting to server $SERVER via SSH..." # Pass variables into SSH session ssh $SERVER \ "SERVER=$SERVER REPO='$REPO' BRANCH='$BRANCH' STAGING_DIR='$STAGING_DIR' USE_CACHE='$USE_CACHE' bash -s" << 'ENDSSH' set -e echo "Connected to $SERVER." cd /home/cmc # Clone or update staging branch if [ -d "$STAGING_DIR" ]; then echo "Updating existing staging directory $STAGING_DIR..." cd "$STAGING_DIR" git fetch origin git checkout $BRANCH git reset --hard origin/$BRANCH else echo "Cloning repository $REPO to $STAGING_DIR..." git clone -b $BRANCH $REPO $STAGING_DIR cd "$STAGING_DIR" fi # Create .env file for go-app if it doesn't exist ENV_PATH="/home/cmc/$STAGING_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 staging (no cache)..." docker compose -f docker-compose.stg.yml build --no-cache docker compose -f docker-compose.stg.yml up -d --remove-orphans else echo "Building and starting docker compose for staging (using cache)..." docker compose -f docker-compose.stg.yml build docker compose -f docker-compose.stg.yml up -d --remove-orphans fi echo "Checking running containers..." echo "Deployment complete." ENDSSH