ci(images): assign image versions in CI and open the pin PR right after a push
Build / build (pull_request) Canceled after 0s
Lint / lint (pull_request) Canceled after 0s
Molecule / molecule (pull_request) Canceled after 0s

The image.yml matrix holds a hand-edited base (MAJOR.MINOR) per image instead
of a version. On main, scripts/next_image_version.py compares a checksum of the
image's inputs with the inputs-sha256 label of the newest published <base>.N
and the job publishes <base>.N+1 when they differ, or builds nothing when they
match. A published tag is still never moved.

renovate.yml runs live on the successful completion of Images (workflow_run),
so the pin pull request opens minutes after a push instead of on the schedule.

Removed: bump_image_versions.py and its tests, check_image_versions.py bump,
Lint's bump step, Renovate's postUpgradeTasks, and the bot-diff allowance for
matrix version lines.

Closes #165

Assisted-by: Claude Code:claude-opus-5-5
This commit is contained in:
Defame1297 committed 2026-11-23 21:15:16 +00:00
1 parent 8da65bd08e
commit b12c990b6a
23 files changed
+484 -1064

No files matched your search

+69 -214
View File
@@ -23,14 +23,14 @@ jobs:
matrix:
include:
- name: mol
version: {mol}
base: "{mol}"
subpath: images/mol
dockerfile: Dockerfile
job_image: """
+ JOB
+ """
- name: ci
version: {ci}
base: "{ci}"
subpath: ""
dockerfile: images/ci/Dockerfile
job_image: """
@@ -126,7 +126,7 @@ class PureTest(unittest.TestCase):
both = text + f"image: git.rkdr.net/mortis/zzz:0.1.0@{DIGEST} # trailing\n"
self.assertEqual(len(civ.check_pins(m, {"f": both})), 1)
def matrix(self, mol="0.1.0", ci="0.2.0"):
def matrix(self, mol="0.1", ci="0.2"):
return civ.matrix_of(IMAGE_YML.format(mol=mol, ci=ci))
def test_images_of_the_denied_namespace_are_refused(self):
@@ -153,15 +153,13 @@ class PureTest(unittest.TestCase):
"must not use or name",
)
def test_pins_ok_and_lagging(self):
pin = f"image: git.rkdr.net/mortis/ci:0.1.0@{DIGEST}\n"
self.assertEqual(civ.check_pins(self.matrix(), {"lint.yml": pin}), [])
def test_pins_of_a_matrix_image_pass_whatever_their_version(self):
for version in ("0.1.0", "0.2.7", "0.3.0"):
pin = f"image: git.rkdr.net/mortis/ci:{version}@{DIGEST}\n"
self.assertEqual(civ.check_pins(self.matrix(), {"lint.yml": pin}), [])
def test_pin_ahead_of_matrix_unknown_image_and_malformed(self):
def test_unknown_image_and_malformed_pins(self):
m = self.matrix()
self.assertEqual(
len(civ.check_pins(m, {"f": f"git.rkdr.net/mortis/ci:0.3.0@{DIGEST}"})), 1
)
self.assertEqual(
len(civ.check_pins(m, {"f": f"git.rkdr.net/mortis/zzz:0.1.0@{DIGEST}"})), 1
)
@@ -260,8 +258,8 @@ class PureTest(unittest.TestCase):
for tail in (",", ")", "]", "}", "'", '"', ";"):
text = f"x: [git.rkdr.net/mortis/ci:0.1.0@{DIGEST}{tail if tail != ';' else ''} y]\n"
self.assertEqual(civ.check_pins(m, {"f": text}), [], tail)
ahead = f"x: (git.rkdr.net/mortis/ci:0.3.0@{DIGEST}), z\n"
self.assertEqual(len(civ.check_pins(m, {"f": ahead})), 1)
unknown = f"x: (git.rkdr.net/mortis/zzz:0.1.0@{DIGEST}), z\n"
self.assertEqual(len(civ.check_pins(m, {"f": unknown})), 1)
self.assertEqual(
len(
civ.check_pins(
@@ -278,26 +276,32 @@ class PureTest(unittest.TestCase):
+ body
)
entry = " - name: a\n version: '0.1.0'\n dockerfile: D\n"
entry = (
" - name: a\n base: '0.1'\n dockerfile: D\n"
)
cases = {
"": "non-empty list",
" []\n": "non-empty block list",
" - 1\n": "not a mapping",
" - name: 1\n version: '0.1.0'\n dockerfile: D\n": "name must be a non-empty string",
" - name: 1\n base: '0.1'\n dockerfile: D\n": "name must be a non-empty string",
entry + " subpath: [x]\n": "cannot read the value",
entry.replace("'0.1.0'", "0.1") + "": "version must be a string",
entry.replace("'0.1'", "0.1"): "base must be a string",
entry + entry: "duplicate image name 'a'",
entry + " version: '0.2.0'\n": "duplicate key 'version'",
entry + " base: '0.2'\n": "duplicate key 'base'",
entry.replace("base: '0.1'", "base: *anchor"): "cannot read the value",
entry.replace(
"version: '0.1.0'", "version: *anchor"
): "cannot read the value",
entry.replace(
"version: '0.1.0'", 'version: "0\\x2e1.0"'
"base: '0.1'", 'base: "0\\x2e1"'
): "cannot read the quoted value",
entry.replace(
" - name: a\n", " - name: ''\n"
): "name must be a non-empty string",
" - name: a\n dockerfile: D\n": "lacks the key 'version'",
" - name: a\n dockerfile: D\n": "lacks the key 'base'",
entry.replace("'0.1'", "'0.2.3'"): "base must be MAJOR.MINOR",
entry.replace("'0.1'", "'v1.0'"): "base must be MAJOR.MINOR",
entry.replace("'0.1'", "'01.2'"): "base must be MAJOR.MINOR",
entry.replace("'0.1'", "'1.02'"): "base must be MAJOR.MINOR",
entry.replace("'0.1'", "''"): "base must be a string",
entry + " version: '0.1.0'\n": "key 'version' is not allowed",
}
for body, text in cases.items():
with self.assertRaises(civ.CheckError, msg=body) as ctx:
@@ -312,13 +316,13 @@ class PureTest(unittest.TestCase):
self.assertNotIn("\n", str(ctx.exception))
def test_matrix_reader_accepts_comments_quotes_and_key_order(self):
text = IMAGE_YML.format(mol="0.1.0", ci="0.2.0")
text = IMAGE_YML.format(mol="0.1", ci="0.2")
text = text.replace(
" - name: ci\n version: 0.2.0\n",
' # the ci image\n - version: "0.2.0" # bumped by the bot\n'
' - name: ci\n base: "0.2"\n',
" # the ci image\n - base: '0.2' # minor of the next build\n"
" name: ci\n",
)
self.assertEqual(civ.matrix_of(text)["ci"]["version"], "0.2.0")
self.assertEqual(civ.matrix_of(text)["ci"]["base"], "0.2")
self.assertEqual(civ.matrix_of(text)["mol"]["subpath"], "images/mol")
def test_matrix_reader_agrees_with_pyyaml_on_the_real_file(self):
@@ -328,35 +332,26 @@ class PureTest(unittest.TestCase):
real = (
pathlib.Path(__file__).resolve().parents[2] / civ.MATRIX_FILE
).read_text()
want = {
e["name"]: {
"version": e["version"],
"subpath": e.get("subpath") or "",
"dockerfile": e["dockerfile"],
"job_image": e["job_image"],
}
for e in yaml.safe_load(real)["jobs"]["image"]["strategy"]["matrix"][
"include"
]
}
self.assertEqual(civ.matrix_of(real), want)
self.assertEqual(
civ.matrix_of(IMAGE_YML.format(mol="0.1.0", ci="0.2.0")),
{
def oracle(text):
return {
e["name"]: {
"version": e["version"],
"base": e["base"],
"subpath": e.get("subpath") or "",
"dockerfile": e["dockerfile"],
"job_image": e["job_image"],
}
for e in yaml.safe_load(IMAGE_YML.format(mol="0.1.0", ci="0.2.0"))[
"jobs"
]["image"]["strategy"]["matrix"]["include"]
},
)
for e in yaml.safe_load(text)["jobs"]["image"]["strategy"]["matrix"][
"include"
]
}
self.assertEqual(civ.matrix_of(real), oracle(real))
fixture = IMAGE_YML.format(mol="0.1", ci="0.2")
self.assertEqual(civ.matrix_of(fixture), oracle(fixture))
def test_job_image_must_match_the_dockerfile_release(self):
text = IMAGE_YML.format(mol="0.1.0", ci="0.2.0")
text = IMAGE_YML.format(mol="0.1", ci="0.2")
matrix = civ.matrix_of(text)
files = {
"images/mol/Dockerfile": f"FROM {JOB}\n",
@@ -394,7 +389,7 @@ class PureTest(unittest.TestCase):
)
def test_job_image_shape_and_missing_key(self):
text = IMAGE_YML.format(mol="0.1.0", ci="0.2.0")
text = IMAGE_YML.format(mol="0.1", ci="0.2")
for bad in (
text.replace(f"job_image: {JOB}", "job_image: ubuntu:latest", 1),
text.replace(f" job_image: {JOB}\n", "", 1),
@@ -434,7 +429,7 @@ class PureTest(unittest.TestCase):
)
def test_container_line_must_sit_under_container(self):
text = IMAGE_YML.format(mol="0.1.0", ci="0.2.0")
text = IMAGE_YML.format(mol="0.1", ci="0.2")
lookalike = text.replace(
" container:\n image: ${{ matrix.job_image }}\n",
" container:\n image: ubuntu:24.04\n steps:\n - run: |\n"
@@ -447,14 +442,14 @@ class PureTest(unittest.TestCase):
self.assertIn("the job container must be", errors[0])
def test_quoted_job_image_is_refused(self):
text = IMAGE_YML.format(mol="0.1.0", ci="0.2.0").replace(
text = IMAGE_YML.format(mol="0.1", ci="0.2").replace(
f"job_image: {JOB}", f'job_image: "{JOB}"', 1
)
with self.assertRaises(civ.CheckError):
civ.matrix_of(text)
def test_image_metadata_labels_and_documentation(self):
text = IMAGE_YML.format(mol="0.1.0", ci="0.2.0")
text = IMAGE_YML.format(mol="0.1", ci="0.2")
matrix = civ.matrix_of(text)
files = {
"images/mol/Dockerfile": labelled("mol"),
@@ -528,45 +523,10 @@ class PureTest(unittest.TestCase):
[],
)
def test_scripts_do_not_import_yaml(self):
# The bump step runs in the Renovate image, whose python3 has no PyYAML (dry run 2453).
for script in ("check_image_versions.py", "bump_image_versions.py"):
source = (
pathlib.Path(__file__).resolve().parents[2] / "scripts" / script
).read_text()
self.assertNotRegex(source, r"(?m)^\s*(import|from)\s+yaml\b", script)
def test_bump_logic(self):
def read(_path):
return CI_DOCKERFILE
base = self.matrix()
same = self.matrix()
for path in (
"images/mol/Dockerfile",
"images/ci/Dockerfile",
"requirements-dev.txt",
".pre-commit-config.yaml",
):
self.assertTrue(civ.check_bump(base, same, [path], read), path)
self.assertEqual(
civ.check_bump(
base,
same,
["README.md", "images/other/x", ".gitea/workflows/lint.yml"],
read,
),
[],
)
self.assertEqual(
civ.check_bump(
base, self.matrix(ci="0.2.1"), ["requirements-dev.txt"], read
),
[],
)
self.assertTrue(
civ.check_bump(base, self.matrix(ci="0.1.9"), [], read)
) # backwards
def test_script_does_not_import_yaml(self):
# Its importers run in job containers whose python3 has no PyYAML.
source = (SCRIPTS / "check_image_versions.py").read_text()
self.assertNotRegex(source, r"(?m)^\s*(import|from)\s+yaml\b")
class PythonPinTest(unittest.TestCase):
@@ -669,9 +629,7 @@ class GitTest(unittest.TestCase):
run(self.repo, "git", "init", "-q", "-b", "main")
run(self.repo, "git", "config", "user.email", "[email protected]")
run(self.repo, "git", "config", "user.name", "t")
self.write(
".gitea/workflows/image.yml", IMAGE_YML.format(mol="0.1.0", ci="0.2.0")
)
self.write(".gitea/workflows/image.yml", IMAGE_YML.format(mol="0.1", ci="0.2"))
self.write(
"images/ci/Dockerfile",
labelled(
@@ -700,20 +658,10 @@ class GitTest(unittest.TestCase):
run(self.repo, "git", "add", "-A")
run(self.repo, "git", "commit", "-q", "-m", msg)
def bump(self):
proc = subprocess.run(
[sys.executable, str(SCRIPTS / "check_image_versions.py"), "bump", "main"],
cwd=self.repo,
capture_output=True,
text=True,
check=False,
)
return proc.returncode, proc.stderr
def bump_args(self, *args, cwd=None):
def script(self, *args):
proc = subprocess.run(
[sys.executable, str(SCRIPTS / "check_image_versions.py"), *args],
cwd=cwd or self.repo,
cwd=self.repo,
capture_output=True,
text=True,
check=False,
@@ -729,101 +677,48 @@ class GitTest(unittest.TestCase):
def test_pins_cli_refuses_the_denied_namespace_in_any_workflow(self):
self.write(".gitea/workflows/lint.yml", "name: Lint\n")
self.assertEqual(self.bump_args("pins")[0], 0)
self.assertEqual(self.script("pins")[0], 0)
self.write(
".gitea/workflows/other.yaml",
"container: git.rkdr.net/defame1297/runner:1\n",
)
code, err = self.bump_args("pins")
code, err = self.script("pins")
self.assertEqual(code, 1)
self.assertIn("other.yaml: mentions git.rkdr.net/defame1297/", err)
def test_moved_input_out_of_the_set_fails(self):
run(self.repo, "git", "mv", "images/mol/Dockerfile", "elsewhere.dockerfile")
self.commit("move out")
code, err = self.bump()
self.assertEqual(code, 1)
self.assertIn("image 'mol'", err)
def test_bump_mode_is_gone(self):
self.write(".gitea/workflows/lint.yml", "name: Lint\n")
self.assertEqual(self.script("bump", "main")[0], 2)
def test_renamed_pinned_input_fails(self):
run(self.repo, "git", "mv", "requirements-dev.txt", "req2.txt")
self.commit("rename")
code, err = self.bump()
self.assertEqual(code, 1)
self.assertIn("image 'ci'", err)
def test_option_like_base_ref_is_rejected(self):
for ref in ("--output=/tmp/x", "-h", "a b", "main\n"):
self.assert_clean_failure(
self.bump_args("bump", ref), "not a plain ref name"
)
def test_unknown_base_ref_is_one_line(self):
self.assert_clean_failure(
self.bump_args("bump", "nope"), "git merge-base failed"
)
def test_git_errors_are_decoded_text_not_a_bytes_repr(self):
code, err = self.bump_args("bump", "nope")
self.assertEqual(code, 1)
self.assertNotIn("b'", err)
self.assertNotIn('b"', err)
self.assertIn("fatal:", err)
def test_bad_version_in_matrix_is_one_line(self):
def test_bad_base_in_matrix_is_one_line(self):
self.write(
".gitea/workflows/image.yml", IMAGE_YML.format(mol="latest", ci="0.2.0")
".gitea/workflows/image.yml", IMAGE_YML.format(mol="latest", ci="0.2")
)
self.commit("bad version")
self.assert_clean_failure(self.bump(), "'latest' is not MAJOR.MINOR.PATCH")
self.assert_clean_failure(self.script("pins"), "base must be MAJOR.MINOR")
def test_missing_matrix_keys_is_one_line(self):
self.write(
".gitea/workflows/image.yml",
IMAGE_YML.replace(" version: {mol}\n", "").format(
mol="", ci="0.2.0"
IMAGE_YML.replace(' base: "{mol}"\n', "").format(
mol="", ci="0.2"
),
)
self.commit("no version")
self.assert_clean_failure(self.bump(), "lacks the key")
self.assert_clean_failure(self.script("pins"), "lacks the key")
self.write(".gitea/workflows/image.yml", "jobs: {}\n")
self.commit("no matrix")
self.assert_clean_failure(self.bump(), "no jobs.image.strategy.matrix.include")
self.assert_clean_failure(
self.script("pins"), "no jobs.image.strategy.matrix.include"
)
def test_pins_mode_missing_matrix_file_is_one_line(self):
run(self.repo, "git", "rm", "-q", ".gitea/workflows/image.yml")
self.assert_clean_failure(self.bump_args("pins"), "cannot read a file")
def test_non_ascii_input_path_counts(self):
self.write("images/mol/caf\u00e9", "x\n")
self.commit("non-ascii input")
code, err = self.bump()
self.assertEqual(code, 1, err)
self.assertIn("image 'mol'", err)
self.assertIn("caf\u00e9", err)
def test_non_utf8_path_in_the_diff_is_named(self):
path = os.fsencode(str(self.repo)) + b"/images/mol/caf\xe9"
try:
with open(path, "wb") as handle:
handle.write(b"x\n")
except OSError:
self.skipTest("the file system does not accept a non-UTF-8 name")
self.commit("non-utf8 input")
self.assert_clean_failure(self.bump(), "path is not valid UTF-8")
self.assert_clean_failure(self.script("pins"), "cannot read a file")
def test_empty_image_yml_says_so(self):
self.write(".gitea/workflows/image.yml", "")
self.commit("empty matrix")
self.assert_clean_failure(
self.bump(), "no jobs.image.strategy.matrix.include list"
self.script("pins"), "no jobs.image.strategy.matrix.include list"
)
def test_unsupported_dockerfile_source_fails_with_file_and_line(self):
self.write("images/ci/Dockerfile", "FROM x\nCOPY $SRC ./\n")
self.commit("variable source")
self.assert_clean_failure(self.bump(), "images/ci/Dockerfile:2:")
def test_pins_mode_requires_lint_yml_and_lists_checked_consumers(self):
proc = subprocess.run(
[sys.executable, str(SCRIPTS / "check_image_versions.py"), "pins"],
@@ -849,46 +744,6 @@ class GitTest(unittest.TestCase):
self.assertIn("(checked: .gitea/workflows/lint.yml)", proc.stdout)
self.assertNotIn("build.yml", proc.stdout)
def test_unrelated_change_passes(self):
self.write("README.md", "x\n")
self.commit("docs")
self.assertEqual(self.bump()[0], 0)
def test_pinned_input_without_bump_fails(self):
self.write("requirements-dev.txt", "a==2\n")
self.commit("deps")
code, err = self.bump()
self.assertEqual(code, 1)
self.assertIn("image 'ci'", err)
self.assertNotIn("image 'mol'", err)
def test_pinned_input_with_bump_passes(self):
self.write("requirements-dev.txt", "a==2\n")
self.write(
".gitea/workflows/image.yml", IMAGE_YML.format(mol="0.1.0", ci="0.2.1")
)
self.commit("deps and bump")
self.assertEqual(self.bump()[0], 0)
def test_dockerfile_change_of_molecule_image_fails(self):
self.write("images/mol/Dockerfile", "FROM y\n")
self.commit("mol")
code, err = self.bump()
self.assertEqual(code, 1)
self.assertIn("image 'mol'", err)
def test_bump_survives_main_moving_on(self):
run(self.repo, "git", "checkout", "-q", "main")
self.write("README.md", "newer main\n")
self.commit("main moves")
run(self.repo, "git", "checkout", "-q", "feature")
self.write("images/mol/Dockerfile", "FROM y\n")
self.write(
".gitea/workflows/image.yml", IMAGE_YML.format(mol="0.2.0", ci="0.2.0")
)
self.commit("mol with bump")
self.assertEqual(self.bump()[0], 0)
if __name__ == "__main__":
unittest.main()