fix(deploy): ipass-agent 回滚点判重改按镜像 id;回滚改按 tag 时间戳选最近

两个缺陷都只在「同一个 tag 被覆盖推送」这个常态场景下暴露(如 ipass-1.0.0 迭代):

1. 回滚点被静默跳过
   原逻辑比较 old_image != image,两边都是 **tag 名**。同 tag 覆盖推送时 tag 名相同
   → 判为「相同」→ 跳过打标签,只在日志留一句不起眼的「跳过」,等于悄悄失去回滚能力。
   改为按**镜像内容 id** 判重(新增 image_id()),并用旧 id 打标签。
   实测:同名 tag 但内容不同时,正确产出 ipass-gateway:rollback-<ts>(指向旧内容)。

2. 自动回滚可能选错镜像
   原来按 {{.CreatedAt}} 取最新,而 CreatedAt 是镜像的**构建时间**;回滚标签是事后用
   image id 补打的,CreatedAt 会很旧(实测标签时间 10:49:54、CreatedAt 10:45:45)
   → 「选最近」反而可能选到旧镜像。改为按 tag 名里的时间戳排序(字典序即时间序)。

验证:本机造「同名 tag、不同内容」的旧容器 → update 正确产出回滚标签并打印回滚命令。
This commit is contained in:
admin committed 2026-09-12 10:50:22 +08:00
1 parent 6a5387b8ce
commit 2f21f4b1c5
1 file changed
+23 -5
+23 -5
View File
@@ -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):