#!/bin/bash # 鍦?test-h5 / test-api 涓?SSH 鐧诲綍鍚庢墽琛岋紙root 鎴?sudo锛? # 鐢ㄦ硶: bash scripts/prod-on-server.sh [h5|api|all] set -euo pipefail ROLE="${1:-all}" ROOT="$(cd "$(dirname "$0")/.." && pwd)" cd "$ROOT" echo "===== 1) 璇婃柇 =====" hostname docker ps -a 2>/dev/null || echo "(no docker)" ss -tlnp 2>/dev/null | grep -E ':80|:3001|:8080' || true echo "===== 2) 鎷変唬鐮?& 閲嶅缓 =====" if [ -d .git ]; then git pull --ff-only || git pull fi if [ -f docker-compose.prod.yml ]; then docker compose -f docker-compose.prod.yml --env-file .env.production up -d --build elif [ -f docker-compose.yml ]; then docker compose up -d --build else echo "鏈壘鍒?compose 鏂囦欢锛岃鎵嬪姩閮ㄧ讲 API" fi echo "===== 3) 绛夊緟鍋ュ悍妫€鏌?=====" for i in $(seq 1 30); do if curl -sf http://127.0.0.1:3001/api/health >/dev/null 2>&1; then echo "API 3001 OK" break fi if curl -sf http://127.0.0.1:8080/api/health >/dev/null 2>&1; then echo "API 8080 OK" break fi sleep 2 done echo "===== 4) 鏁版嵁搴撹縼绉伙紙bcrypt + 鍒楄ˉ榻愶級=====" CID=$(docker ps --format '{{.Names}}' | grep -E 'canteen|app-prod' | head -1 || true) if [ -n "$CID" ]; then docker exec "$CID" node dist/migrate.js else (cd server && npm run migrate) || true fi if [ "$ROLE" = "api" ] || [ "$ROLE" = "all" ]; then echo "===== 5) test-api: Nginx 鍙嶄唬 /api -> 3001 =====" chmod +x scripts/setup-prod-api-nginx.sh bash scripts/setup-prod-api-nginx.sh deploy/nginx.api-proxy.conf fi echo "===== 6) 鐧诲綍楠岃瘉 =====" for BASE in http://127.0.0.1:3001/api http://127.0.0.1:8080/api http://127.0.0.1/api; do CODE=$(curl -sS -o /tmp/login.json -w '%{http_code}' -X POST "$BASE/auth/token" \ -H 'Content-Type: application/json' \ -d "$(printf '{"email":"%s","password":"%s"}' "superadmin@meoo.local" "$SUPERADMIN_PASSWORD")" || echo 000) echo "$BASE/auth/token -> HTTP $CODE" head -c 200 /tmp/login.json 2>/dev/null || true echo done echo "===== 瀹屾垚 ====="