feat(ci): 组件条目 valuesFiles 多文件——一个镜像散在多个 values 时一次全替换(如 postgres 在 4 个文件)
逐文件替换,客户目录没有的文件跳过并记录,全部未命中才 fail; from 汇总各文件旧值(多数相同,不同则如实并列)。
This commit is contained in:
1 parent
defe711ce6
commit
86b4aa0b27
1 file changed
+35
-28
+35
-28
@@ -242,15 +242,15 @@ 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()
|
||||
vfiles = [str(x).strip() for x in (e.get("valuesFiles") or []) if str(x).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 not (name and tag and vfiles and iname):
|
||||
die(f"组件条目缺字段(name/acrTag/valuesFiles/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})
|
||||
"acrTag": tag, "valuesFiles": vfiles, "imageName": iname})
|
||||
continue
|
||||
svc = e.get("service")
|
||||
if svc not in ALL:
|
||||
@@ -596,38 +596,45 @@ if not CHART_ONLY:
|
||||
else:
|
||||
print("[promote] CHART_ONLY=true:image-version.yaml 不动")
|
||||
|
||||
# ── 1.5) 系统组件:精确替换 values 文件里该镜像的 tag ──
|
||||
# ── 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:
|
||||
hits = [] # (文件, 旧tag)
|
||||
for vf in c["valuesFiles"]:
|
||||
vpath = os.path.join(envdir, vf)
|
||||
if not os.path.exists(vpath):
|
||||
print(f"[promote] 组件 {c['name']}: {vf} 在客户目录不存在,跳过")
|
||||
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"
|
||||
lines = open(vpath, encoding="utf-8").readlines()
|
||||
old_tag, hit_idx = None, None
|
||||
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 = m.group(2), idx
|
||||
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 = mb.group(2), j
|
||||
lines[j] = mb.group(1) + c["acrTag"] + "\n"
|
||||
break
|
||||
if hit_idx is not None:
|
||||
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})")
|
||||
open(vpath, "w", encoding="utf-8").write("".join(lines))
|
||||
hits.append((vf, old_tag))
|
||||
print(f"[promote] 组件 {c['name']}: {vf} {c['imageName']} {old_tag} → {c['acrTag']}")
|
||||
if not hits:
|
||||
sys.exit(f"[promote] 错误: 组件 {c['name']}({c['imageName']})在 {', '.join(c['valuesFiles'])} 中均未找到引用")
|
||||
from_tags[c["name"]] = " / ".join(t for _, t in hits)
|
||||
to_tags[c["name"]] = c["acrTag"]
|
||||
|
||||
# ── 2) applications chart targetRevision:只处理非 keep 服务对应的文件 ──
|
||||
# (语义等价于按文件 sed,用 python 实现以便与 PLAN 共用服务→文件映射)
|
||||
|
||||
Reference in new issue
Block a user