Add /api/health and harden Docker deploy script for API route rollout.

404 on /api/ai indicates stale container; deploy script now uses --no-cache, stops PM2/port conflicts, and verifies /api/health.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-10 22:42:28 +08:00
parent dba0245cb1
commit 38bbc7145a
4 changed files with 46 additions and 6 deletions
+27 -6
View File
@@ -14,22 +14,43 @@ if [[ ! -f .env.local ]]; then
exit 1
fi
echo "==> 当前 commit: $(git rev-parse --short HEAD)"
echo "==> 拉取最新代码..."
git pull origin main
echo "==> 更新后 commit: $(git rev-parse --short HEAD)"
echo "==> 停止旧 PM2 进程(若存在)..."
echo "==> 停止旧 PM2 进程(若存在,避免占用 ${APP_PORT}..."
pm2 stop zhimingge 2>/dev/null || true
pm2 delete zhimingge 2>/dev/null || true
echo "==> 构建 Docker 镜像..."
docker compose build
if command -v fuser >/dev/null 2>&1; then
fuser -k "${APP_PORT}/tcp" 2>/dev/null || true
fi
echo "==> 构建 Docker 镜像(无缓存)..."
docker compose build --no-cache
echo "==> 启动容器..."
docker compose up -d
docker compose up -d --force-recreate
echo "==> 状态"
docker compose ps
sleep 2
curl -s -o /dev/null -w "HTTP %{http_code}\n" "http://127.0.0.1:${APP_PORT}" || true
echo "==> 等待服务就绪..."
for i in $(seq 1 30); do
if curl -sf "http://127.0.0.1:${APP_PORT}/api/health" >/dev/null 2>&1; then
echo "OK /api/health"
curl -s "http://127.0.0.1:${APP_PORT}/api/health"
echo ""
break
fi
if [[ "$i" -eq 30 ]]; then
echo "ERROR: /api/health 未响应,请检查:docker compose logs zhimingge"
exit 1
fi
sleep 1
done
curl -s -o /dev/null -w "首页 HTTP %{http_code}\n" "http://127.0.0.1:${APP_PORT}/" || true
echo "==> 部署完成。日志:docker compose logs -f zhimingge"