#!/bin/bash # Apply uploaded backend build on test-api / test-api1. set -euo pipefail APP=/opt/ai-fresh-delivery STAGING="$HOME/deploy-staging" SERVER="$APP/server" if [ ! -d "$STAGING/dist" ]; then echo "Missing $STAGING/dist — upload deploy-staging first" exit 1 fi echo "===== backup marker =====" date -Iseconds > "$STAGING/.last_deploy" echo "===== sync server code =====" rm -rf "$SERVER/dist" cp -r "$STAGING/dist" "$SERVER/dist" if [ -f "$STAGING/package.json" ]; then cp "$STAGING/package.json" "$SERVER/package.json" echo "===== npm install (production deps) =====" if [ -d "$SERVER/node_modules" ]; then echo "SKIP npm install (node_modules already present; CVM cannot reach npm registry)" else if ! (cd "$SERVER" && npm install --omit=dev --no-audit --no-fund); then echo "FATAL: npm install failed and node_modules missing — deploy aborted." >&2 exit 1 fi fi fi if [ -d "$STAGING/data" ]; then mkdir -p "$SERVER/data" cp -r "$STAGING/data/"* "$SERVER/data/" 2>/dev/null || true fi # 迁移文件:兼容两种部署包结构 # A) 官方打包(scripts/tencent-prod-env.ps1: -C migrations .)→ *.sql 落在包/暂存根目录 # B) 带 migrations/ 子目录的包 # 历史事故:本段原先只查 $STAGING/migrations,而官方打包是 A 结构 → # `cp … || true` 把「找不到文件」的错误吞掉 → **迁移静默不执行但部署显示成功**, # 导致线上代码依赖的新列不存在(2026-09-14 看板报 column sk.low_stock_alert does not exist)。 # 故此处不再吞错:找不到任何迁移文件即中止,且校验落地文件数与暂存一致。 migrations_src="$STAGING/migrations" if [ ! -d "$migrations_src" ] && compgen -G "$STAGING/*.sql" >/dev/null; then migrations_src="$STAGING" fi if ! compgen -G "$migrations_src/*.sql" >/dev/null; then echo "FATAL: no migration .sql found in $STAGING or $STAGING/migrations — deploy aborted." >&2 echo " (期望打包含 migrations;见 docs/design/20260914-deploy-migration-path-fix.md)" >&2 exit 1 fi mkdir -p "$APP/migrations" cp "$migrations_src/"*.sql "$APP/migrations/" staged_count=$(ls -1 "$migrations_src"/*.sql | wc -l | tr -d ' ') applied_count=$(ls -1 "$APP/migrations"/*.sql 2>/dev/null | wc -l | tr -d ' ') if [ "$applied_count" -lt "$staged_count" ]; then echo "FATAL: migrations copy incomplete ($applied_count < $staged_count) — deploy aborted." >&2 exit 1 fi echo "migrations staged: $staged_count file(s) from $migrations_src" cd "$SERVER" export MIGRATIONS_DIR="$APP/migrations" export SKIP_DEMO_SEED="${SKIP_DEMO_SEED:-1}" export SKIP_E2E_SEED="${SKIP_E2E_SEED:-1}" if [ "${SKIP_DB_MIGRATE:-0}" = "1" ]; then echo "===== database migrate SKIPPED (SKIP_DB_MIGRATE=1) =====" else echo "===== database migrate =====" if ! node dist/migrate.js; then echo "" >&2 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" >&2 echo "!! FATAL: database migration failed !!" >&2 echo "!! Deploy aborted — API was NOT restarted. !!" >&2 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" >&2 exit 1 fi fi if [ -f "$STAGING/deploy/nginx.api-proxy.conf" ]; then mkdir -p "$APP/deploy" "$APP/scripts" cp "$STAGING/deploy/nginx.api-proxy.conf" "$APP/deploy/" 2>/dev/null || true cp "$STAGING/scripts/setup-prod-api-nginx.sh" "$APP/scripts/" 2>/dev/null || true chmod +x "$APP/scripts/setup-prod-api-nginx.sh" 2>/dev/null || true sudo bash "$APP/scripts/setup-prod-api-nginx.sh" "$APP/deploy/nginx.api-proxy.conf" || true fi echo "===== LLM env =====" echo "keep existing server/.env (regular deploy does not upsert LLM_* / SPEECH_API_KEY)" echo "===== restart api (clear :3001 orphans first) =====" # 旧手工 node dist/index.js 若占着 3001,systemctl 会 EADDRINUSE 起不来,健康检查却仍 200(假活) sudo systemctl stop ai-fresh-api || true sleep 1 # 杀掉仍监听 3001 的残留 node(含非 systemd 孤儿) if command -v fuser >/dev/null 2>&1; then sudo fuser -k 3001/tcp 2>/dev/null || true elif command -v ss >/dev/null 2>&1; then for pid in $(ss -lptn 'sport = :3001' | sed -n 's/.*pid=\([0-9]*\).*/\1/p' | sort -u); do echo "killing orphan pid=$pid on :3001" sudo kill -TERM "$pid" 2>/dev/null || true done sleep 1 for pid in $(ss -lptn 'sport = :3001' | sed -n 's/.*pid=\([0-9]*\).*/\1/p' | sort -u); do sudo kill -KILL "$pid" 2>/dev/null || true done fi sleep 1 sudo systemctl start ai-fresh-api active=0 for i in $(seq 1 40); do if sudo systemctl is-active --quiet ai-fresh-api; then active=1 break fi sleep 1 done sudo systemctl is-active ai-fresh-api if [ "$active" != "1" ]; then echo "FATAL: ai-fresh-api not active after restart" >&2 sudo systemctl status ai-fresh-api --no-pager -l | head -n 40 >&2 || true exit 1 fi # 确认监听进程属于当前 systemd cgroup(避免又被孤儿顶替) main_pid="$(systemctl show -p MainPID --value ai-fresh-api 2>/dev/null || true)" listen_pid="$(ss -lptn 'sport = :3001' 2>/dev/null | sed -n 's/.*pid=\([0-9]*\).*/\1/p' | head -1 || true)" echo "ai-fresh-api MainPID=${main_pid:-?} listen_pid=${listen_pid:-?}" if [ -n "${main_pid:-}" ] && [ "${main_pid}" != "0" ] && [ -n "${listen_pid:-}" ] && [ "$listen_pid" != "$main_pid" ]; then echo "WARN: :3001 pid ($listen_pid) != systemd MainPID ($main_pid) — may still be orphan" >&2 fi echo "===== health =====" ok=0 for i in $(seq 1 20); do if curl -sf http://127.0.0.1:3001/api/health >/dev/null; then ok=1 break fi sleep 1 done if [ "$ok" != "1" ]; then echo "FATAL: local :3001 health failed" >&2 exit 1 fi curl -sf http://127.0.0.1:3001/api/health && echo curl -sf http://127.0.0.1/api/health && echo || true echo "===== login smoke test =====" if [ -z "${SUPERADMIN_PASSWORD:-}" ]; then echo "WARN: SUPERADMIN_PASSWORD unset — skip login smoke (health already OK)" elif [ "${SKIP_LOGIN_SMOKE:-0}" = "1" ]; then echo "SKIP_LOGIN_SMOKE=1 — skip login smoke" else curl -sS -m 10 -X POST 'http://127.0.0.1:3001/api/auth/token' \ -H 'Content-Type: application/json' \ --data-raw "$(printf '{"email":"%s","password":"%s"}' "${SUPERADMIN_EMAIL:-superadmin@meoo.local}" "$SUPERADMIN_PASSWORD")" | head -c 120 echo fi echo "===== backend deploy done ====="