2025-07-23 03:00:06 -07:00
|
|
|
#!/bin/bash
|
|
|
|
|
# Deploy staging environment for cmc-sales
|
|
|
|
|
|
2025-08-10 05:35:26 -07:00
|
|
|
# Usage: ./deploy-stg.sh [--no-cache]
|
2025-07-23 03:40:54 -07:00
|
|
|
|
|
|
|
|
USE_CACHE=true
|
2025-07-23 04:22:50 -07:00
|
|
|
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
|
2025-07-23 03:40:54 -07:00
|
|
|
echo "Using cached layers for docker build."
|
|
|
|
|
fi
|
|
|
|
|
|
2025-07-23 03:17:47 -07:00
|
|
|
echo "Starting staging deployment for cmc-sales..."
|
|
|
|
|
echo "Setting variables..."
|
2025-08-09 18:45:08 -07:00
|
|
|
SERVER="cmc-sales"
|
2025-07-23 03:17:47 -07:00
|
|
|
REPO="git@code.springupsoftware.com:cmc/cmc-sales.git"
|
2025-07-23 03:00:06 -07:00
|
|
|
BRANCH="stg"
|
|
|
|
|
STAGING_DIR="cmc-sales-staging"
|
2025-07-23 03:17:47 -07:00
|
|
|
|
|
|
|
|
echo "Connecting to server $SERVER via SSH..."
|
|
|
|
|
# Pass variables into SSH session
|
|
|
|
|
ssh $SERVER \
|
2025-08-10 05:35:26 -07:00
|
|
|
"SERVER=$SERVER REPO='$REPO' BRANCH='$BRANCH' STAGING_DIR='$STAGING_DIR' USE_CACHE='$USE_CACHE' bash -s" << 'ENDSSH'
|
2025-07-23 03:00:06 -07:00
|
|
|
set -e
|
2025-07-23 03:17:47 -07:00
|
|
|
echo "Connected to $SERVER."
|
2025-08-09 18:45:08 -07:00
|
|
|
cd /home/cmc
|
2025-07-23 03:00:06 -07:00
|
|
|
# Clone or update staging branch
|
|
|
|
|
if [ -d "$STAGING_DIR" ]; then
|
2025-07-23 03:17:47 -07:00
|
|
|
echo "Updating existing staging directory $STAGING_DIR..."
|
2025-07-23 03:00:06 -07:00
|
|
|
cd "$STAGING_DIR"
|
|
|
|
|
git fetch origin
|
|
|
|
|
git checkout $BRANCH
|
2025-08-10 05:37:11 -07:00
|
|
|
git reset --hard origin/$BRANCH
|
2025-07-23 03:00:06 -07:00
|
|
|
else
|
2025-07-23 03:17:47 -07:00
|
|
|
echo "Cloning repository $REPO to $STAGING_DIR..."
|
2025-07-23 03:00:06 -07:00
|
|
|
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"
|
2025-08-10 22:15:53 -07:00
|
|
|
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
|
|
|
|
|
|
2025-07-23 03:00:06 -07:00
|
|
|
# Environment variables for Go app mail configuration
|
|
|
|
|
SMTP_HOST="host.docker.internal"
|
|
|
|
|
SMTP_PORT=1025
|
|
|
|
|
SMTP_USER=""
|
|
|
|
|
SMTP_PASS=""
|
|
|
|
|
SMTP_FROM="CMC Sales <sales@cmctechnologies.com.au>"
|
|
|
|
|
ENVEOF
|
|
|
|
|
|
2025-07-23 03:40:54 -07:00
|
|
|
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
|
2025-07-23 04:55:10 -07:00
|
|
|
docker compose -f docker-compose.stg.yml up -d --remove-orphans
|
2025-07-23 03:40:54 -07:00
|
|
|
else
|
|
|
|
|
echo "Building and starting docker compose for staging (using cache)..."
|
|
|
|
|
docker compose -f docker-compose.stg.yml build
|
2025-07-23 04:55:10 -07:00
|
|
|
docker compose -f docker-compose.stg.yml up -d --remove-orphans
|
2025-07-23 03:40:54 -07:00
|
|
|
fi
|
2025-07-23 03:00:06 -07:00
|
|
|
|
2025-07-23 03:17:47 -07:00
|
|
|
echo "Checking running containers..."
|
|
|
|
|
echo "Deployment complete."
|
2025-07-23 03:00:06 -07:00
|
|
|
ENDSSH
|