From 054150c7d063d6f1b4cc078576de0865abccd40d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AF=92=E5=AF=92?= <2596194220@qq.com> Date: Sun, 24 May 2026 23:59:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9B=B4=E6=96=B0=E6=97=B6=E5=85=88=20d?= =?UTF-8?q?own=20=E6=97=A7=E5=AE=B9=E5=99=A8=E5=86=8D=20up=20-d=EF=BC=8C?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=E7=AB=AF=E5=8F=A3=E5=8D=A0=E7=94=A8=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=9A=84=E5=8D=A1=E6=AD=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/update.ts | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/update.ts b/lib/update.ts index a05e2b3..790b097 100644 --- a/lib/update.ts +++ b/lib/update.ts @@ -318,8 +318,8 @@ export async function executeUpdate( onLog("开始更新流程"); // 0. 检查 Docker 是否可用 - onLog("[0/4] 检查 Docker 环境"); - consoleLog("执行", "步骤 0/4 — 检查 Docker 环境"); + onLog("[0/5] 检查 Docker 环境"); + consoleLog("执行", "步骤 0/5 — 检查 Docker 环境"); const dockerCheck = checkDockerAvailable(); if (!dockerCheck.ok) { onLog(`✗ ${dockerCheck.message}`); @@ -330,8 +330,8 @@ export async function executeUpdate( // 1. 拉取最新代码 const url = getAuthenticatedRemoteUrl(); - onLog("[1/4] 克隆最新代码"); - consoleLog("执行", "步骤 1/4 — 克隆代码"); + onLog("[1/5] 克隆最新代码"); + consoleLog("执行", "步骤 1/5 — 克隆代码"); execSyncAndLog( `git clone --depth 1 "${url}" "${tmpDir}"`, "git-clone", @@ -340,8 +340,8 @@ export async function executeUpdate( onLog("✓ 代码拉取完成"); // 2. 生成 .env 文件 - onLog("[2/4] 准备环境变量"); - consoleLog("执行", "步骤 2/4 — 准备环境变量"); + onLog("[2/5] 准备环境变量"); + consoleLog("执行", "步骤 2/5 — 准备环境变量"); const envContent = [ `DB_HOST=${process.env.DB_HOST || "127.0.0.1"}`, `DB_PORT=${process.env.DB_PORT || "3306"}`, @@ -358,8 +358,8 @@ export async function executeUpdate( onLog("✓ 环境变量已准备"); // 3. 构建新镜像 - onLog("[3/4] 构建新 Docker 镜像"); - consoleLog("执行", "步骤 3/4 — 构建镜像(可能耗时较长)"); + onLog("[3/5] 构建新 Docker 镜像"); + consoleLog("执行", "步骤 3/5 — 构建镜像(可能耗时较长)"); execSyncAndLog( `cd "${tmpDir}" && docker-compose -p recipe_tool build`, "docker-build", @@ -367,15 +367,30 @@ export async function executeUpdate( ); onLog("✓ 镜像构建完成"); - // 4. 重启服务 - onLog("[4/4] 重启服务"); - consoleLog("执行", "步骤 4/4 — 重启服务"); + // 4. 停止旧容器(释放端口) + onLog("[4/5] 停止旧容器"); + consoleLog("执行", "步骤 4/5 — 停止旧容器"); + // 忽略 down 的错误(首次运行可能没有旧容器) + try { + execSyncAndLog( + `cd "${tmpDir}" && docker-compose -p recipe_tool down`, + "docker-down", + { timeout: 30000, stdio: ["ignore", "pipe", "pipe"] } + ); + } catch { + consoleLog("执行", "旧容器停止(可能不存在,忽略)"); + } + onLog("✓ 旧容器已停止"); + + // 5. 启动新容器 + onLog("[5/5] 启动新容器"); + consoleLog("执行", "步骤 5/5 — 启动新容器"); execSyncAndLog( `cd "${tmpDir}" && docker-compose -p recipe_tool up -d`, "docker-up", { timeout: 60000, stdio: ["ignore", "pipe", "pipe"] } ); - onLog("✓ 服务已重启"); + onLog("✓ 新容器已启动"); consoleLog("执行", "=== 更新成功 ==="); return { success: true, message: "更新完成" };