79 lines
2.2 KiB
Bash
Executable file
79 lines
2.2 KiB
Bash
Executable file
#!/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-sales"
|
|
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 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="172.17.0.1"
|
|
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)..."
|
|
docker compose -f docker-compose.prod.yml build --no-cache
|
|
docker compose -f docker-compose.prod.yml up -d --remove-orphans
|
|
else
|
|
echo "Building and starting docker compose for production (using cache)..."
|
|
docker compose -f docker-compose.prod.yml build
|
|
docker compose -f docker-compose.prod.yml up -d --remove-orphans
|
|
fi
|
|
|
|
echo "Checking running containers..."
|
|
echo "Production deployment complete."
|
|
ENDSSH
|