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):