cmc-sales/deploy/deploy-stg.sh

85 lines
2.8 KiB
Bash
Executable file

#!/bin/bash
# Deploy staging environment for cmc-sales
# Usage: ./deploy-stg.sh [--no-cache]
USE_CACHE=true
if [[ "$1" == "--no-cache" ]]; then
USE_CACHE=false
echo "No cache flag detected: will rebuild images without cache."
else
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."
# 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 pull 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"
if [ ! -f "$ENV_PATH" ]; then
echo "Creating .env file for go-app..."
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
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
echo "Checking running containers..."
# Check containers
docker compose -f docker-compose.stg.yml ps
echo "Creating sync script for staging directories..."
# Create sync script for staging dirs
cat > /home/cmc/sync_prod_to_staging.sh <<'EOF'
#!/bin/bash
rsync -a --delete /mnt/vault/pdf/ /home/cmc/cmc-sales-staging/app/webroot/pdf/
rsync -a --delete /mnt/vault/attachments_files/ /home/cmc/cmc-sales-staging/app/webroot/attachments_files/
rsync -a --delete /mnt/vault/emails/ /home/cmc/cmc-sales-staging/vault/emails/
rsync -a --delete /mnt/vault/vaultmsgs/ /home/cmc/cmc-sales-staging/vault/vaultmsgs/
EOF
chmod +x /home/cmc/sync_prod_to_staging.sh
echo "Ensuring cron job for sync script is present..."
# 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 -
echo "Deployment complete."
ENDSSH