feat(ci): promote.sh 支持系统组件条目——第三方镜像(postgres/redis/mqttx 等)随 PLAN 精确替换 values 里的 tag

PLAN 组件条目:{"component":true,"name":..,"acrTag":..,"valuesFile":..,"imageName":..}
- 登记制:映射随 PLAN 动态携带,流水线零改动即可认识新组件
- pin 精确替换客户 values 文件里该镜像的 tag,兼容整行(image: ...:tag)
  与拆分(repository:+相邻 tag:)两种 values 形态
- 组件也算正式发布:分配版本号+chart bump;release-notes 如实记 from→to
- CHART_ONLY 与组件互斥(防御)

本地 PROMOTE_DRY_RUN 双形态演练通过(mqttx latest→v2.0.0 拆分态、
redis 8.6→8.7 整行态,mxlexus)
This commit is contained in:
ximenchuifeng committed 2026-09-08 13:13:29 +08:00
1 parent ffbb985919
commit defe711ce6
1 file changed
+70 -9
+70 -9
View File
@@ -208,8 +208,8 @@ if raw_plan:
if not isinstance(plan, list):
die('PLAN 应为 JSON 数组,每服务一条 {"service":..., "action":...}')
for i, e in enumerate(plan):
if not isinstance(e, dict) or "service" not in e:
die(f"PLAN[{i}] 应为对象且含 service 字段")
if not isinstance(e, dict) or ("service" not in e and not e.get("component")):
die(f"PLAN[{i}] 应为对象且含 service 字段(或 component 组件条目)")
entries.append(dict(e))
else:
selected = {}
@@ -235,9 +235,23 @@ else:
# 全部缺省 = 6 服务全 deliver demo 当前 tag(行为与改造前一致)
entries = [{"service": s, "action": "deliver"} for s in ALL]
# ── 2. 校验 + 规范化为全量 6 服务计划 ──
# ── 2. 校验 + 规范化(六服务计划 + 组件条目)──
norm, seen = {}, set()
component_entries = []
for e in entries:
if e.get("component"):
name = str(e.get("name") or "").strip()
tag = str(e.get("acrTag") or "").strip()
vfile = str(e.get("valuesFile") or "").strip()
iname = str(e.get("imageName") or "").strip()
if not (name and tag and vfile and iname):
die(f"组件条目缺字段(name/acrTag/valuesFile/imageName): {e}")
if name in seen:
die(f"条目名重复: {name}")
seen.add(name)
component_entries.append({"component": True, "name": name, "action": "pin-acr",
"acrTag": tag, "valuesFile": vfile, "imageName": iname})
continue
svc = e.get("service")
if svc not in ALL:
die(f"非法服务名: {svc}(合法值: {', '.join(ALL)})")
@@ -266,12 +280,12 @@ for e in entries:
norm[svc] = n
plan_out = [norm.get(s) or {"service": s, "action": "keep",
"repos": REPOS[s], "keys": KEYS[s], "appFile": APPFILE[s]}
for s in ALL]
for s in ALL] + component_entries
has_deliver = any(e["action"] in ("deliver", "ci-now", "pin-acr") for e in plan_out)
# ── 3. DB_CHANGE 校验:逗号分隔服务名,须为本次非 keep 服务的子集 ──
db_change = [s.strip() for s in (os.environ.get("DB_CHANGE") or "").split(",") if s.strip()]
nonkeep = {e["service"] for e in plan_out if e["action"] != "keep"}
nonkeep = {e["service"] for e in plan_out if e["action"] != "keep" and not e.get("component")}
for s in db_change:
if s not in ALL:
die(f"DB_CHANGE 含非法服务名: {s}(合法值: {', '.join(ALL)})")
@@ -338,7 +352,9 @@ open(plantxt_path, "w", encoding="utf-8").write(raw_plan or json.dumps(plan_out,
for e in plan_out:
if e["action"] == "keep":
continue
print(f"[promote] 计划: {e['service']:<8} {e['action']:<8} {e.get('harborTag') or e.get('version') or e.get('acrTag') or ''}")
svc_label = e.get("service") or e.get("name") or ""
extra = "(组件)" if e.get("component") else ""
print(f"[promote] 计划: {svc_label:<10} {e['action']:<8} {extra}{e.get('harborTag') or e.get('version') or e.get('acrTag') or ''}")
print(f"[promote] DB 变更服务: {', '.join(db_change) or '无'}")
PY
. "$FACTS_FILE" # HAS_DELIVER=true|false
@@ -501,6 +517,9 @@ do_pin() {
git fetch -q origin release || die "release 分支不存在,请先推送初始 release 分支(改造清单 B1)"
git checkout -q -B release origin/release
if [ "$CHART_ONLY" = "true" ] && [ -s "$PLAN_FILE" ] && grep -q '"component": *true' "$PLAN_FILE"; then
die "CHART_ONLY 与组件条目互斥(仅 chart 发布不动任何镜像)"
fi
ENVDIR="environments/$CUSTOMER"
APPDIR="applications/$CUSTOMER"
@@ -525,7 +544,8 @@ NOTES = os.environ.get("RELEASE_NOTES", "")
DB_CHANGE = [s for s in os.environ.get("DB_CHANGE", "").split(",") if s]
plan = json.load(open(plan_path, encoding="utf-8"))
actions = [e for e in plan if e["action"] != "keep"]
actions = [e for e in plan if e["action"] != "keep" and not e.get("component")]
components = [e for e in plan if e.get("component")]
has_deliver = any(e["action"] in ("deliver", "ci-now") for e in plan)
# 平台 Application(infra/mqttx)不属于 PLAN 服务映射,跟随每次新 chart tag
@@ -576,6 +596,39 @@ if not CHART_ONLY:
else:
print("[promote] CHART_ONLY=true:image-version.yaml 不动")
# ── 1.5) 系统组件:精确替换 values 文件里该镜像的 tag ──
# 兼容两种 values 形态:A. image: <registry>/<imageName>:<tag>(整行)
# B. repository: <registry>/<imageName> + 相邻 tag: <tag>(拆分)
for c in components:
vpath = os.path.join(envdir, c["valuesFile"])
if not os.path.exists(vpath):
sys.exit(f"[promote] 错误: 缺少组件 values 文件 {vpath}")
lines = open(vpath, encoding="utf-8").readlines()
old_tag, hit_idx, hit_kind = None, None, None
pat_a = re.compile("(" + re.escape(c["imageName"]) + r"):([A-Za-z0-9][\w.\-]*)")
pat_b = re.compile(r"^(\s*tag:\s*)(\S+)\s*$")
for idx, line in enumerate(lines):
if c["imageName"] not in line:
continue
m = pat_a.search(line)
if m: # 形态 A:同行
old_tag, hit_idx, hit_kind = m.group(2), idx, "A"
lines[idx] = pat_a.sub(lambda _m: _m.group(1) + ":" + c["acrTag"], line)
break
for j in range(idx + 1, min(idx + 4, len(lines))): # 形态 B:邻近 tag: 字段
mb = pat_b.match(lines[j])
if mb:
old_tag, hit_idx, hit_kind = mb.group(2), j, "B"
lines[j] = mb.group(1) + c["acrTag"] + "\n"
break
if hit_idx is not None:
break
if hit_idx is None:
sys.exit(f"[promote] 错误: {vpath} 中未找到镜像 {c['imageName']} 的 tag 引用(整行或拆分形态)")
open(vpath, "w", encoding="utf-8").write("".join(lines))
from_tags[c["name"]], to_tags[c["name"]] = old_tag, c["acrTag"]
print(f"[promote] 组件 {c['name']}: {c['imageName']} {old_tag} → {c['acrTag']}({c['valuesFile']},{hit_kind})")
# ── 2) applications chart targetRevision:只处理非 keep 服务对应的文件 ──
# (语义等价于按文件 sed,用 python 实现以便与 PLAN 共用服务→文件映射)
chart_from, chart_to = {}, {}
@@ -635,8 +688,16 @@ def find_reuse_source(svc, version):
return "", ""
svc_entries, src_tags = [], {}
for e in actions:
svc, action = e["service"], e["action"]
for e in actions + components:
svc, action = e.get("service") or e.get("name"), e["action"]
if e.get("component"):
# 组件:替换 values 文件里的镜像 tag(已做),chart 无关,如实记账
frm, to = from_tags.get(svc, ""), to_tags.get(svc, "")
src_tags[svc] = e.get("acrTag", "")
svc_entries.append({"name": svc, "action": action, "from": frm, "to": to,
"sourceTag": e.get("acrTag", ""), "digest": "",
"dbChange": False, "component": True})
continue
if CHART_ONLY:
# CHART_ONLY 条目:from/to 记 chart tag 变化,sourceTag/digest 留空
frm, to = chart_from.get(svc, ""), chart_to.get(svc, "")