Files
SupplierSystem/scripts/prod-apply-backend.sh
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

163 lines
6.2 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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 ====="