From 2f21f4b1c5ca1766ff3638e1cf3686861114eaa3 Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 12 Sep 2026 10:50:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(deploy):=20ipass-agent=20=E5=9B=9E=E6=BB=9A?= =?UTF-8?q?=E7=82=B9=E5=88=A4=E9=87=8D=E6=94=B9=E6=8C=89=E9=95=9C=E5=83=8F?= =?UTF-8?q?=20id=EF=BC=9B=E5=9B=9E=E6=BB=9A=E6=94=B9=E6=8C=89=20tag=20?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=88=B3=E9=80=89=E6=9C=80=E8=BF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 两个缺陷都只在「同一个 tag 被覆盖推送」这个常态场景下暴露(如 ipass-1.0.0 迭代): 1. 回滚点被静默跳过 原逻辑比较 old_image != image,两边都是 **tag 名**。同 tag 覆盖推送时 tag 名相同 → 判为「相同」→ 跳过打标签,只在日志留一句不起眼的「跳过」,等于悄悄失去回滚能力。 改为按**镜像内容 id** 判重(新增 image_id()),并用旧 id 打标签。 实测:同名 tag 但内容不同时,正确产出 ipass-gateway:rollback-(指向旧内容)。 2. 自动回滚可能选错镜像 原来按 {{.CreatedAt}} 取最新,而 CreatedAt 是镜像的**构建时间**;回滚标签是事后用 image id 补打的,CreatedAt 会很旧(实测标签时间 10:49:54、CreatedAt 10:45:45) → 「选最近」反而可能选到旧镜像。改为按 tag 名里的时间戳排序(字典序即时间序)。 验证:本机造「同名 tag、不同内容」的旧容器 → update 正确产出回滚标签并打印回滚命令。 --- deploy/ipass-agent.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/deploy/ipass-agent.py b/deploy/ipass-agent.py index 626e17b..fba42ad 100644 --- a/deploy/ipass-agent.py +++ b/deploy/ipass-agent.py @@ -244,6 +244,16 @@ def image_exists(image): return rc == 0 +def image_id(image): + """镜像内容 id(`sha256:…`)。 + + 用于「同一个 tag 被覆盖推送」时判重 —— 那是常态(如 `ipass-1.0.0` 迭代), + 只比 tag 名会把「内容已变」误判成「相同」,于是悄悄跳过回滚标签。 + """ + rc, out = docker(["image", "inspect", "--format", "{{.Id}}", image]) + return out.strip() if rc == 0 else "" + + # ------------------------------------------------------------------ 从 inspect 复刻启动参数 def extract_ports(cfg, main_port_override=None): @@ -551,16 +561,21 @@ def op_update(job, p): # ---- 3 旧镜像留回滚标签 jlog(job, "\n3/7 旧镜像留回滚标签") rollback_tag = None - if old_image and old_image != image: + # ★ 按**镜像内容 id** 判重,不能按 tag 名:同一个 tag 被覆盖推送是常态 + # (如 ipass-1.0.0 迭代),比 tag 名会把「内容已变」误判成「相同」→ + # 跳过回滚标签,且只在日志里留一句不起眼的「跳过」,等于悄悄失去回滚点。 + old_id = cfg.get("Image") or "" + new_id = image_id(image) + if old_id and old_id != new_id: rollback_tag = "%s%s" % (ROLLBACK_PREFIX, ts_tag()) - rc, out = docker(["tag", old_image, rollback_tag]) + rc, out = docker(["tag", old_id, rollback_tag]) if rc == 0: job["rollback_tag"] = rollback_tag - jlog(job, " %s → %s" % (old_image, rollback_tag)) + jlog(job, " %s (%s) → %s" % (old_image, old_id[:19], rollback_tag)) else: jlog(job, " ! 打标签失败:%s" % out.strip()) else: - jlog(job, " (旧镜像与新镜像相同或未知,跳过)") + jlog(job, " (旧容器镜像与新镜像内容一致,无需回滚点)") # ---- 4 删旧容器 jlog(job, "\n4/7 停止并删除旧容器") @@ -639,7 +654,10 @@ def op_rollback(job, p): cands.append(parts) if not cands: raise RuntimeError("没有 rollback-* 镜像可回滚") - cands.sort(key=lambda x: x[1], reverse=True) + # 按 **tag 名里的时间戳** 排序(`rollback-YYYYMMDD-HHMMSS` 的字典序即时间序)。 + # 不要按 {{.CreatedAt}}:那是镜像的**构建时间**,而回滚标签是事后用 image id + # 补打的 —— 用它排序会把「刚打的、指向旧内容的」标签排到最后(甚至选错)。 + cands.sort(key=lambda x: x[0], reverse=True) tag = cands[0][0] jlog(job, "自动选中最近的回滚镜像:%s" % tag) if not image_exists(tag):