name: Deploy Website on: push: branches: [main] jobs: build-and-deploy: runs-on: ubuntu-latest steps: - name: Checkout latest code run: | if [ -d /tmp/web-projects ]; then cd /tmp/web-projects && git pull else git clone http://localhost:3000/suche-Hermes/web-projects.git /tmp/web-projects fi - name: Build Astro site run: | docker run --rm \ -v /tmp/web-projects:/app \ -w /app \ node:22-alpine sh -c "npm install && npm run build" - name: Install Nginx config (HTTP) run: | mkdir -p /etc/nginx/conf.d 2>/dev/null cp /tmp/web-projects/nginx/longde.cloud.conf /etc/nginx/conf.d/longde.cloud.conf mkdir -p /etc/nginx/sites-available /etc/nginx/sites-enabled 2>/dev/null cp /tmp/web-projects/nginx/longde.cloud.conf /etc/nginx/sites-available/longde.cloud.conf 2>/dev/null ln -sf /etc/nginx/sites-available/longde.cloud.conf /etc/nginx/sites-enabled/longde.cloud.conf 2>/dev/null - name: Deploy to web root run: | rm -rf /var/www/longde.cloud/* cp -r /tmp/web-projects/dist/* /var/www/longde.cloud/ - name: Ensure API backend is running run: | set -x # Clone or pull api-server repo for docker-compose.yml if [ -d /tmp/api-server ]; then cd /tmp/api-server && git pull 2>/dev/null || true else git clone http://localhost:3000/suche-Hermes/api-server.git /tmp/api-server 2>/dev/null || true fi # Create shared network if missing docker network inspect zhixi-net >/dev/null 2>&1 || docker network create zhixi-net # Start MySQL + Redis via docker compose if [ -f /tmp/api-server/docker-compose.yml ]; then cd /tmp/api-server && docker compose up -d mysql redis 2>&1 || true fi # Wait for MySQL to be ready sleep 5 # Check current state echo "=== Container status ===" docker ps -a --format 'table {{.Names}}\t{{.Status}}' 2>/dev/null | grep -iE 'zhixi|mysql|redis' || true # If API container exists but stopped, start it if docker ps -a --format '{{.Names}}' | grep -q '^zhixi-api$'; then if ! docker ps --format '{{.Names}}' | grep -q '^zhixi-api$'; then echo "[deploy] zhixi-api exists but stopped, starting..." docker start zhixi-api 2>&1 || true sleep 8 fi else # No container — create one echo "[deploy] zhixi-api not found, building and creating..." cd /tmp/api-server && docker build -t zhixi-api:latest . 2>&1 || true ENV_FILE="" [ -f /etc/zhixi/.env.production ] && ENV_FILE="--env-file /etc/zhixi/.env.production" docker run -d \ --name zhixi-api \ --network zhixi-net \ --restart unless-stopped \ -p 3001:3000 \ $ENV_FILE \ zhixi-api:latest 2>&1 || true sleep 8 fi # Health check if curl -sf http://localhost:3001/health; then echo "[deploy] Backend health OK" else echo "[deploy] WARNING: Backend health check failed" echo "=== API container logs (tail 40) ===" docker logs zhixi-api --tail 40 2>&1 || true fi set +x - name: Install Nginx config (HTTPS) run: | if [ -f /etc/letsencrypt/live/longde.cloud/fullchain.pem ]; then cp /tmp/web-projects/nginx/longde.cloud-ssl.conf /etc/nginx/conf.d/longde.cloud-ssl.conf else echo "[deploy] No SSL cert found, skipping HTTPS config" rm -f /etc/nginx/conf.d/longde.cloud-ssl.conf fi - name: Dump debug info run: | { echo "=== Deploy $(date) ===" echo "" echo "=== docker ps -a ===" docker ps -a --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}' 2>&1 echo "" echo "=== zhixi-api logs (tail 50) ===" docker logs zhixi-api --tail 50 2>&1 || echo "(no logs)" echo "" echo "=== docker network ls ===" docker network ls 2>&1 echo "" echo "=== port 3001 ===" ss -tlnp | grep 3001 2>/dev/null || netstat -tlnp 2>/dev/null | grep 3001 || echo "(no listener on 3001)" echo "" echo "=== nginx -t ===" nginx -t 2>&1 } > /var/www/longde.cloud/deploy-status.txt 2>&1 - name: Reload Nginx run: nginx -s reload || nginx -t 2>&1