diff --git a/buildrunner/docker/multiplatform_image_builder.py b/buildrunner/docker/multiplatform_image_builder.py index 0344d783..65f77d98 100644 --- a/buildrunner/docker/multiplatform_image_builder.py +++ b/buildrunner/docker/multiplatform_image_builder.py @@ -244,6 +244,9 @@ def _build_with_inject( file: str, build_args: dict, builder: Optional[str], + cache: bool = False, + cache_from: list[str] = None, + pull: bool = False, ) -> None: if not path or not os.path.isdir(path): @@ -279,7 +282,10 @@ def _build_with_inject( load=True, file=file, builder=builder, - build_args=build_args + build_args=build_args, + cache=cache, + cache_from=cache_from, + pull=pull, ) # pylint: disable=too-many-arguments @@ -298,7 +304,10 @@ def _build_single_image( tags: List[str], build_args: dict, mp_image_name: str, - inject: dict) -> None: + inject: dict, + cache: bool = False, + cache_from: list[str] = None, + pull: bool = False,) -> None: """ Builds a single image for the given platform @@ -330,6 +339,9 @@ def _build_single_image( file=file, build_args=build_args, builder=builder, + cache=cache, + cache_from=cache_from, + pull=pull, ) else: docker.buildx.build( @@ -340,6 +352,9 @@ def _build_single_image( file=file, build_args=build_args, builder=builder, + cache=cache, + cache_from=cache_from, + pull=pull, ) # Push after the initial load to support remote builders that cannot access the local registry docker.push(tagged_names) @@ -401,6 +416,9 @@ def build_multiple_images( do_multiprocessing: bool = True, build_args: dict = None, inject: dict = None, + cache: bool = False, + cache_from: list[str] = None, + pull: bool = False, ) -> List[ImageInfo]: """ Builds multiple images for the given platforms. One image will be built for each platform. @@ -419,6 +437,10 @@ def build_multiple_images( List[ImageInfo]: The list of intermediate built images, these images are ephemeral and will be removed when the builder is garbage collected """ + logger.debug(f"cache: {cache}") + logger.debug(f"cache_from: {cache_from}") + logger.debug(f"pull: {pull}") + def get_path(file): if os.path.exists(file): return os.path.dirname(file) diff --git a/buildrunner/steprunner/tasks/build.py b/buildrunner/steprunner/tasks/build.py index 6d271cf9..89523046 100644 --- a/buildrunner/steprunner/tasks/build.py +++ b/buildrunner/steprunner/tasks/build.py @@ -223,6 +223,9 @@ def run(self, context): mp_image_name=self.get_unique_build_name(), build_args=self.buildargs, inject=self.to_inject, + cache=not self.nocache, + cache_from=self.cache_from, + pull=self.pull, ) number_of_images = len(self.platforms) diff --git a/buildrunner/validation/step.py b/buildrunner/validation/step.py index 2649afaa..a8652f43 100644 --- a/buildrunner/validation/step.py +++ b/buildrunner/validation/step.py @@ -47,6 +47,7 @@ class StepBuild(BaseModel, extra='forbid'): inject: Optional[Dict[str, Optional[str]]] = None no_cache: Optional[bool] = Field(alias='no-cache', default=None) buildargs: Optional[Dict[str, Any]] = None + cache_from: Optional[List[str]] = None class RunAndServicesBase(BaseModel): diff --git a/tests/test_config_validation/test_validation_step.py b/tests/test_config_validation/test_validation_step.py index 5486297e..64fefab7 100644 --- a/tests/test_config_validation/test_validation_step.py +++ b/tests/test_config_validation/test_validation_step.py @@ -80,6 +80,30 @@ def test_valid_platforms(): assert errors is None +def test_valid_platforms(): + config_yaml = """ + steps: + build-container-multi-platform: + build: + path: . + dockerfile: Dockerfile + pull: false + platforms: + - linux/amd64 + - linux/arm64 + no-cache: true + cache_from: + - mytest-reg/buildrunner-test-multi-platform:latest + push: + repository: mytest-reg/buildrunner-test-multi-platform + tags: + - latest + """ + config = yaml.load(config_yaml, Loader=yaml.Loader) + errors = validate_config(**config) + assert errors is None + + def test_duplicate_mp_tags_dictionary_invalid(): # Invalid to have duplicate multi-platform tag config_yaml = """ diff --git a/tests/test_multiplatform.py b/tests/test_multiplatform.py index 865bb10d..01065622 100644 --- a/tests/test_multiplatform.py +++ b/tests/test_multiplatform.py @@ -475,6 +475,9 @@ def test_build_multiple_builds(mock_build, mock_pull, mock_push, mock_inspect, m file='tests/test-files/multiplatform/Dockerfile', build_args={'DOCKER_REGISTRY': None}, builder=None, + cache=False, + cache_from=None, + pull=False ), call( 'tests/test-files/multiplatform', @@ -484,6 +487,9 @@ def test_build_multiple_builds(mock_build, mock_pull, mock_push, mock_inspect, m file='tests/test-files/multiplatform/Dockerfile', build_args={'DOCKER_REGISTRY': None}, builder=None, + cache=False, + cache_from=None, + pull=False ), call( 'tests/test-files/multiplatform', @@ -493,6 +499,9 @@ def test_build_multiple_builds(mock_build, mock_pull, mock_push, mock_inspect, m file='tests/test-files/multiplatform/Dockerfile', build_args={'DOCKER_REGISTRY': None}, builder=None, + cache=False, + cache_from=None, + pull=False ), call( 'tests/test-files/multiplatform', @@ -502,6 +511,9 @@ def test_build_multiple_builds(mock_build, mock_pull, mock_push, mock_inspect, m file='tests/test-files/multiplatform/Dockerfile', build_args={'DOCKER_REGISTRY': None}, builder=None, + cache=False, + cache_from=None, + pull=False ), ] assert mock_push.call_count == 4