53 lines
1.7 KiB
Bash
53 lines
1.7 KiB
Bash
#!/bin/bash
|
|
# ─── EnFlow Auto-Deploy Script ───────────────────────────────────────────────
|
|
# Called by Gitea post-receive hook or manually
|
|
# Usage: /home/ubuntu/cirux/deploy-auto.sh
|
|
|
|
set -e
|
|
|
|
APP_DIR="/home/ubuntu/cirux"
|
|
LOG_FILE="/var/log/cirux-deploy.log"
|
|
BRANCH="main"
|
|
|
|
echo "─────────────────────────────────────────" >> "$LOG_FILE"
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') - Starting deploy" >> "$LOG_FILE"
|
|
|
|
cd "$APP_DIR"
|
|
|
|
# Pull latest code
|
|
echo "Pulling latest code..." >> "$LOG_FILE"
|
|
git fetch origin >> "$LOG_FILE" 2>&1
|
|
git reset --hard "origin/$BRANCH" >> "$LOG_FILE" 2>&1
|
|
|
|
# Install deps (if package.json changed)
|
|
if git diff --name-only HEAD~1 HEAD | grep -q "package.json\|package-lock.json"; then
|
|
echo "Dependencies changed, running npm ci..." >> "$LOG_FILE"
|
|
npm ci >> "$LOG_FILE" 2>&1
|
|
fi
|
|
|
|
# Rebuild Docker containers
|
|
echo "Rebuilding Docker containers..." >> "$LOG_FILE"
|
|
docker compose down >> "$LOG_FILE" 2>&1
|
|
docker compose build --no-cache >> "$LOG_FILE" 2>&1
|
|
docker compose up -d >> "$LOG_FILE" 2>&1
|
|
|
|
# Cleanup old images
|
|
docker image prune -f >> "$LOG_FILE" 2>&1
|
|
|
|
# Health check
|
|
echo "Running health check..." >> "$LOG_FILE"
|
|
sleep 10
|
|
|
|
RETRIES=5
|
|
while [ $RETRIES -gt 0 ]; do
|
|
if curl -sf http://localhost:3020/api/health > /dev/null 2>&1; then
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') - Deploy successful ✅" >> "$LOG_FILE"
|
|
exit 0
|
|
fi
|
|
sleep 3
|
|
RETRIES=$((RETRIES - 1))
|
|
done
|
|
|
|
echo "$(date '+%Y-%m-%d %H:%M:%S') - Health check failed after deploy ⚠️" >> "$LOG_FILE"
|
|
exit 1
|