Skip to content

Commit

Permalink
Add no-cache, cache_from and pull MP build params
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejbrown committed Nov 14, 2023
1 parent 70e9a99 commit 4700dcf
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
26 changes: 24 additions & 2 deletions buildrunner/docker/multiplatform_image_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions buildrunner/steprunner/tasks/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions buildrunner/validation/step.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
24 changes: 24 additions & 0 deletions tests/test_config_validation/test_validation_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand Down
12 changes: 12 additions & 0 deletions tests/test_multiplatform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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
Expand Down

0 comments on commit 4700dcf

Please sign in to comment.