cmc-sales/deploy/deploy-prod.sh

92 lines
2.7 KiB
Bash
Raw Normal View History

2025-08-22 02:38:13 -07:00
#!/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..."
2025-11-19 04:20:42 -08:00
SERVER="cmc"
2025-08-22 02:38:13 -07:00
REPO="git@code.springupsoftware.com:cmc/cmc-sales.git"
2025-08-22 03:37:15 -07:00
BRANCH="prod"
2025-08-22 02:38:13 -07:00
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
2025-08-22 02:38:13 -07:00
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
2025-09-13 05:40:19 -07:00
SMTP_HOST=postfix
2025-08-22 02:38:13 -07:00
SMTP_PORT=25
SMTP_USER=""
SMTP_PASS=""
SMTP_FROM="CMC Sales <sales@cmctechnologies.com.au>"
ENVEOF
if [[ "$USE_CACHE" == "false" ]]; then
echo "Building and starting docker compose for production (no cache)..."
2025-11-22 14:26:51 -08:00
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
2025-08-22 02:38:13 -07:00
else
echo "Building and starting docker compose for production (using cache)..."
2025-11-22 14:26:51 -08:00
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
2025-08-22 02:38:13 -07:00
fi
echo "Checking running containers..."
echo "Production deployment complete."
ENDSSH