diff --git a/deploy-auto.sh b/deploy-auto.sh new file mode 100644 index 0000000..08ec8ed --- /dev/null +++ b/deploy-auto.sh @@ -0,0 +1,52 @@ +#!/bin/bash +# ─── EnFlow Auto-Deploy Script ─────────────────────────────────────────────── +# Called by Gitea post-receive hook or manually +# Usage: /opt/cirux/deploy-auto.sh + +set -e + +APP_DIR="/opt/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:3000/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 diff --git a/gitea-post-receive-hook.sh b/gitea-post-receive-hook.sh new file mode 100644 index 0000000..a70bbc5 --- /dev/null +++ b/gitea-post-receive-hook.sh @@ -0,0 +1,37 @@ +#!/bin/bash +# ─── Gitea Post-Receive Hook ──────────────────────────────────────────────── +# Location: /home/ubuntu/gitea/gitea/git/repositories/silvioalzate/cirux.git/hooks/post-receive +# Owner: ubuntu (run as gitea service user) +# Make executable: chmod +x post-receive + +set -e + +APP_DIR="/opt/cirux" +LOG_FILE="/var/log/cirux-deploy.log" +BRANCH="main" +DEPLOY_SCRIPT="/opt/cirux/deploy-auto.sh" + +echo "$(date '+%Y-%m-%d %H:%M:%S') - Push received, starting deploy..." >> "$LOG_FILE" + +# Read refs from stdin (standard git hook input) +while read oldrev newrev refname; do + # Only deploy on main branch pushes + if [ "$refname" = "refs/heads/$BRANCH" ]; then + echo "$(date '+%Y-%m-%d %H:%M:%S') - Deploying branch: $BRANCH" >> "$LOG_FILE" + + cd "$APP_DIR" + + # Run deploy script + /opt/cirux/deploy-auto.sh >> "$LOG_FILE" 2>&1 + + # Verify health + sleep 5 + if curl -sf http://localhost:3000/api/health > /dev/null 2>&1; then + echo "$(date '+%Y-%m-%d %H:%M:%S') - Deploy successful ✅" >> "$LOG_FILE" + else + echo "$(date '+%Y-%m-%d %H:%M:%S') - Health check failed ⚠️" >> "$LOG_FILE" + fi + fi +done + +echo "$(date '+%Y-%m-%d %H:%M:%S') - Hook finished" >> "$LOG_FILE" diff --git a/setup-vps-deploy.sh b/setup-vps-deploy.sh new file mode 100644 index 0000000..0072889 --- /dev/null +++ b/setup-vps-deploy.sh @@ -0,0 +1,96 @@ +#!/bin/bash +# ─── EnFlow VPS Auto-Deploy Setup ─────────────────────────────────────────── +# Run this ONCE on your VPS as root or with sudo +# This configures Gitea hook + Docker deploy automation + +set -e + +APP_DIR="/opt/cirux" +GITEA_USER="ubuntu" +GITEA_REPO="/home/ubuntu/gitea/gitea/git/repositories/silvioalzate/cirux.git" +HOOK_DIR="$GITEA_REPO/hooks" +LOG_FILE="/var/log/cirux-deploy.log" +BRANCH="main" + +echo "🚀 EnFlow Auto-Deploy Setup" +echo " App Dir: $APP_DIR" +echo " Gitea User: $GITEA_USER" +echo " Hook Dir: $HOOK_DIR" +echo "" + +# ─── 1. Verify Gitea user ───────────────────────────────────────────────── +echo "📋 Verifying Gitea user..." +if ! id "$GITEA_USER" &>/dev/null; then + echo "❌ User $GITEA_USER does not exist!" + echo " Check: ps aux | grep gitea" + exit 1 +fi + +# ─── 2. Add user to docker group ──────────────────────────────────────────── +echo "🔧 Adding $GITEA_USER to docker group..." +if ! groups "$GITEA_USER" | grep -q docker; then + sudo usermod -aG docker "$GITEA_USER" + echo " ✅ Added. IMPORTANT: You MUST run 'newgrp docker' or logout/login for this to take effect." +fi + +# ─── 3. Create app directory ───────────────────────────────────────────────── +echo "📁 Setting up app directory..." +if [ ! -d "$APP_DIR" ]; then + echo " Cloning repository..." + sudo -u "$GITEA_USER" git clone "$GITEA_REPO" "$APP_DIR" +else + echo " App directory exists. Updating remote..." + cd "$APP_DIR" + sudo -u "$GITEA_USER" git remote set-url origin "$GITEA_REPO" || true + sudo -u "$GITEA_USER" git fetch origin + sudo -u "$GITEA_USER" git reset --hard "origin/$BRANCH" +fi + +# Set ownership +sudo chown -R "$GITEA_USER":"$GITEA_USER" "$APP_DIR" +sudo chmod -R 755 "$APP_DIR" + +# ─── 4. Create log file with correct permissions ───────────────────────────── +echo "📝 Creating log file..." +sudo touch "$LOG_FILE" +sudo chown "$GITEA_USER":"$GITEA_USER" "$LOG_FILE" +sudo chmod 644 "$LOG_FILE" + +# ─── 5. Install post-receive hook ──────────────────────────────────────────── +echo "🔌 Installing Gitea post-receive hook..." +if [ ! -d "$HOOK_DIR" ]; then + echo "❌ Hook directory not found: $HOOK_DIR" + exit 1 +fi + +sudo cp "$APP_DIR/gitea-post-receive-hook.sh" "$HOOK_DIR/post-receive" +sudo chown "$GITEA_USER":"$GITEA_USER" "$HOOK_DIR/post-receive" +sudo chmod +x "$HOOK_DIR/post-receive" + +# ─── 6. Make deploy-auto.sh executable ───────────────────────────────────────── +echo "🔧 Setting up deploy script..." +sudo chmod +x "$APP_DIR/deploy-auto.sh" + +# ─── 7. Verify Docker access ───────────────────────────────────────────────── +echo "🐳 Verifying Docker access..." +if sudo -u "$GITEA_USER" docker ps > /dev/null 2>&1; then + echo " ✅ Docker access OK" +else + echo " ⚠️ Docker access failed. Run: newgrp docker (or logout/login)" +fi + +# ─── 8. Test deploy ────────────────────────────────────────────────────────── +echo "🧪 Testing deploy..." +cd "$APP_DIR" +sudo -u "$GITEA_USER" bash "$APP_DIR/deploy-auto.sh" + +echo "" +echo "─────────────────────────────────────────" +echo "✅ Setup Complete!" +echo "" +echo " Push to main branch → Auto deploy triggers" +echo " Logs: tail -f $LOG_FILE" +echo " App: http://localhost:3000" +echo "" +echo " If Docker failed: logout and login again, or run 'newgrp docker'" +echo "─────────────────────────────────────────"