From 804852b21829fca5d0c1a0372f14a98e0cc538c5 Mon Sep 17 00:00:00 2001 From: Felix Rubio Date: Sat, 28 Dec 2024 20:49:07 +0100 Subject: [PATCH] Provide support for cache_from and cache_to fields Signed-off-by: Felix Rubio --- newsfragments/cache-fields-support.feature | 1 + podman_compose.py | 4 +++ tests/unit/test_container_to_build_args.py | 30 ++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 newsfragments/cache-fields-support.feature diff --git a/newsfragments/cache-fields-support.feature b/newsfragments/cache-fields-support.feature new file mode 100644 index 0000000..34a3c47 --- /dev/null +++ b/newsfragments/cache-fields-support.feature @@ -0,0 +1 @@ +Added support for cache_from and cache_to fields in build section. diff --git a/podman_compose.py b/podman_compose.py index cd213a6..684ae01 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -2519,6 +2519,10 @@ def container_to_build_args(compose, cnt, args, path_exists): "--build-arg", build_arg, )) + for cache_img in build_desc.get("cache_from", []): + build_args.extend(["--cache-from", cache_img]) + for cache_img in build_desc.get("cache_to", []): + build_args.extend(["--cache-to", cache_img]) build_args.append(ctx) return build_args diff --git a/tests/unit/test_container_to_build_args.py b/tests/unit/test_container_to_build_args.py index 61ddf6e..df861fb 100644 --- a/tests/unit/test_container_to_build_args.py +++ b/tests/unit/test_container_to_build_args.py @@ -126,3 +126,33 @@ class TestContainerToBuildArgs(unittest.TestCase): '.', ], ) + + def test_caches(self): + c = create_compose_mock() + + cnt = get_minimal_container() + cnt['build']['cache_from'] = ['registry/image1', 'registry/image2'] + cnt['build']['cache_to'] = ['registry/image3', 'registry/image4'] + args = get_minimal_args() + + args = container_to_build_args(c, cnt, args, lambda path: True) + self.assertEqual( + args, + [ + '-f', + './Containerfile', + '-t', + 'new-image', + '--no-cache', + '--pull-always', + '--cache-from', + 'registry/image1', + '--cache-from', + 'registry/image2', + '--cache-to', + 'registry/image3', + '--cache-to', + 'registry/image4', + '.', + ], + )