2025-07-23 03:00:06 -07:00
|
|
|
#!/bin/bash
|
|
|
|
|
# Deploy staging environment for cmc-sales
|
|
|
|
|
|
2025-07-23 04:22:50 -07:00
|
|
|
# Usage: ./deploy-stg.sh [--no-cache] [--no-sync]
|
2025-07-23 03:40:54 -07:00
|
|
|
|
|
|
|
|
USE_CACHE=true
|
2025-07-23 04:22:50 -07:00
|
|
|
RUN_SYNC=true
|
|
|
|
|
for arg in "$@"; do
|
|
|
|
|
if [[ "$arg" == "--no-cache" ]]; then
|
|
|
|
|
USE_CACHE=false
|
|
|
|
|
echo "No cache flag detected: will rebuild images without cache."
|
|
|
|
|
fi
|
|
|
|
|
if [[ "$arg" == "--no-sync" ]]; then
|
|
|
|
|
RUN_SYNC=false
|
|
|
|
|
echo "No-sync flag detected: will NOT run sync_prod_to_staging.sh after deployment."
|
|
|
|
|
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-07-23 03:00:06 -07:00
|
|
|
SERVER="cmc"
|
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-07-23 04:22:50 -07:00
|
|
|
"SERVER=$SERVER REPO='$REPO' BRANCH='$BRANCH' STAGING_DIR='$STAGING_DIR' USE_CACHE='$USE_CACHE' RUN_SYNC='$RUN_SYNC' 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-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
|
|
|
|
|
git pull origin $BRANCH
|
|
|
|
|
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"
|
|
|
|
|
if [ ! -f "$ENV_PATH" ]; then
|
2025-07-23 03:17:47 -07:00
|
|
|
echo "Creating .env file for go-app..."
|
2025-07-23 03:00:06 -07:00
|
|
|
cat > "$ENV_PATH" <<'ENVEOF'
|
|
|
|
|
# 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
|
|
|
|
|
fi
|
|
|
|
|
|
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
|
|
|
|
|
docker compose -f docker-compose.stg.yml up -d
|
|
|
|
|
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
|
|
|
|
|
fi
|
2025-07-23 03:00:06 -07:00
|
|
|
|
2025-07-23 03:17:47 -07:00
|
|
|
echo "Checking running containers..."
|
2025-07-23 03:00:06 -07:00
|
|
|
# Check containers
|
2025-07-23 03:40:54 -07:00
|
|
|
docker compose -f docker-compose.stg.yml ps
|
2025-07-23 03:00:06 -07:00
|
|
|
|
2025-07-23 03:17:47 -07:00
|
|
|
echo "Creating sync script for staging directories..."
|
2025-07-23 03:00:06 -07:00
|
|
|
# Create sync script for staging dirs
|
|
|
|
|
cat > /home/cmc/sync_prod_to_staging.sh <<'EOF'
|
|
|
|
|
#!/bin/bash
|
2025-07-23 04:39:01 -07:00
|
|
|
rsync -a --delete --omit-dir-times --no-perms /mnt/vault/pdf/ /home/cmc/cmc-sales-staging/app/webroot/pdf/
|
|
|
|
|
rsync -a --delete --omit-dir-times --no-perms /mnt/vault/attachments_files/ /home/cmc/cmc-sales-staging/app/webroot/attachments_files/
|
|
|
|
|
rsync -a --delete --omit-dir-times --no-perms /mnt/vault/emails/ /home/cmc/cmc-sales-staging/vault/emails/
|
|
|
|
|
rsync -a --delete --omit-dir-times --no-perms /mnt/vault/vaultmsgs/ /home/cmc/cmc-sales-staging/vault/vaultmsgs/
|
2025-07-23 03:00:06 -07:00
|
|
|
EOF
|
|
|
|
|
chmod +x /home/cmc/sync_prod_to_staging.sh
|
|
|
|
|
|
2025-07-23 03:17:47 -07:00
|
|
|
echo "Ensuring cron job for sync script is present..."
|
2025-07-23 03:00:06 -07:00
|
|
|
# Add cron job if not present
|
|
|
|
|
(crontab -l 2>/dev/null | grep -q '/home/cmc/sync_prod_to_staging.sh') || \
|
|
|
|
|
(crontab -l 2>/dev/null; echo '0 3 * * * /home/cmc/sync_prod_to_staging.sh') | crontab -
|
2025-07-23 04:22:50 -07:00
|
|
|
|
|
|
|
|
if [[ "$RUN_SYNC" == "true" ]]; then
|
|
|
|
|
echo "Running sync_prod_to_staging.sh now..."
|
|
|
|
|
/home/cmc/sync_prod_to_staging.sh
|
2025-07-23 04:37:02 -07:00
|
|
|
echo "Restoring database from latest backup in /backups..."
|
|
|
|
|
LATEST_BACKUP=$(ls -t ./backups/backup_*.sql.gz | head -n1)
|
|
|
|
|
if [ -f "$LATEST_BACKUP" ]; then
|
|
|
|
|
gunzip < "$LATEST_BACKUP" | mariadb -h 127.0.0.1 -u cmc -p"xVRQI&cA?7AU=hqJ!%au" cmc
|
|
|
|
|
echo "Database restore complete."
|
|
|
|
|
else
|
|
|
|
|
echo "No backup file found in /backups. Skipping database restore."
|
|
|
|
|
fi
|
2025-07-23 04:22:50 -07:00
|
|
|
fi
|
|
|
|
|
|
2025-07-23 03:17:47 -07:00
|
|
|
echo "Deployment complete."
|
2025-07-23 03:00:06 -07:00
|
|
|
ENDSSH
|