feat(scripts): add bump_image_versions.py for the image version bump

Factor the changed-path listing and the per-image input detection out of
check_image_versions.py (changed_paths, changed_images) and reuse them in a
new script that bumps the PATCH version of every image whose inputs changed.
It is meant to run later as a Renovate postUpgradeTasks command.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
Defame1297andClaude Sonnet 5.5 committed 2026-10-25 21:06:33 +00:00
1 parent ce0a98a471
commit ec15abf400
3 files changed
+311 -19

No files matched your search

+39 -19
View File
@@ -246,21 +246,29 @@ def check_pins(matrix, consumer_texts):
return errors
def check_bump(base_matrix, head_matrix, changed, read_head_file):
errors = []
def changed_images(head_matrix, changed, read_head_file):
"""{name: sorted touched inputs} for each image of the matrix whose inputs are in `changed`."""
result = {}
for name, entry in head_matrix.items():
# With a subpath the Dockerfile is relative to it and the whole directory is the
# context; only a repository-root context needs the Dockerfile read.
text = "" if entry["subpath"] else read_head_file(entry["dockerfile"])
dirs, files = inputs_of(entry, text)
hits = touched(changed, dirs, files)
if not hits:
continue
if hits:
result[name] = sorted(hits)
return result
def check_bump(base_matrix, head_matrix, changed, read_head_file):
errors = []
for name, hits in changed_images(head_matrix, changed, read_head_file).items():
entry = head_matrix[name]
old = base_matrix.get(name)
if old is None:
continue # a new image has no earlier version to collide with
if parse_version(entry["version"]) <= parse_version(old["version"]):
errors.append(f"image '{name}' changed ({', '.join(sorted(hits)[:5])}) but its version in "
errors.append(f"image '{name}' changed ({', '.join(hits[:5])}) but its version in "
f"{MATRIX_FILE} is still {entry['version']} (base: {old['version']}); bump it, "
"otherwise the build is pushed as sha-*/latest only")
for name, entry in head_matrix.items():
@@ -280,6 +288,31 @@ def git(*args, raw=False):
raise CheckError(f"git {args[0]} failed: {detail[-1] if detail else 'exit ' + str(exc.returncode)}") from exc
def changed_paths(base_ref, target="HEAD"):
"""(merge-base, changed paths) between base_ref and target; target None means the working tree."""
base = git("merge-base", "--end-of-options", base_ref, "HEAD").strip()
# --no-renames: a moved input file shows as a deletion at the old path and an addition
# at the new one, so the old path still counts as touched.
# -z: NUL-separated, unquoted paths, so a non-ASCII name (images/x/café) is compared as is
# and not as the "caf\303\251" quoted form.
changed = []
end = [target] if target else []
for raw_path in git("-c", "core.quotePath=false", "diff", "--name-only", "-z", "--no-renames",
"--end-of-options", base, *end, raw=True).split(b"\0"):
if not raw_path:
continue
try:
changed.append(raw_path.decode("utf-8"))
except UnicodeDecodeError as exc:
raise CheckError(f"a changed path is not valid UTF-8: {raw_path.decode('utf-8', 'backslashreplace')}") from exc
if not target: # the working tree also holds files git does not track yet
for raw_path in git("-c", "core.quotePath=false", "ls-files", "--others", "--exclude-standard",
"-z", raw=True).split(b"\0"):
if raw_path:
changed.append(raw_path.decode("utf-8", errors="backslashreplace"))
return base, changed
def run(argv):
if len(argv) >= 2 and argv[1] == "pins" and len(argv) == 2:
matrix = matrix_of(pathlib.Path(MATRIX_FILE).read_text())
@@ -291,20 +324,7 @@ def run(argv):
elif len(argv) == 3 and argv[1] == "bump":
if not REF_RE.fullmatch(argv[2]):
raise CheckError(f"{argv[2]!r} is not a plain ref name (letters, digits, . _ / -, no leading '-')")
base = git("merge-base", "--end-of-options", argv[2], "HEAD").strip()
# --no-renames: a moved input file shows as a deletion at the old path and an addition
# at the new one, so the old path still counts as touched.
# -z: NUL-separated, unquoted paths, so a non-ASCII name (images/x/café) is compared as is
# and not as the "caf\303\251" quoted form.
changed = []
for raw_path in git("-c", "core.quotePath=false", "diff", "--name-only", "-z", "--no-renames",
"--end-of-options", base, "HEAD", raw=True).split(b"\0"):
if not raw_path:
continue
try:
changed.append(raw_path.decode("utf-8"))
except UnicodeDecodeError as exc:
raise CheckError(f"a changed path is not valid UTF-8: {raw_path.decode('utf-8', 'backslashreplace')}") from exc
base, changed = changed_paths(argv[2])
try:
base_text = git("show", f"{base}:{MATRIX_FILE}")
except CheckError: