Skip to content

Commit

Permalink
Add label argument to fondant build (#623)
Browse files Browse the repository at this point in the history
We could use `fondant build` in the cicd pipeline for building the
component images. Add a parameter to enable the cli adding the label.
  • Loading branch information
mrchtr authored Nov 14, 2023
1 parent 9e94da6 commit 139e165
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/fondant/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def build_component( # ruff: noqa: PLR0912, PLR0915
*,
tag: t.Optional[str],
build_args: t.List[str],
labels: t.List[str],
nocache: bool = False,
pull: bool = False,
target: t.Optional[str] = None,
Expand Down Expand Up @@ -57,12 +58,19 @@ def build_component( # ruff: noqa: PLR0912, PLR0915
logger.info(f"Assuming full image name: {full_image_name}")

logger.info("Building image...")

# Convert build args from ["key=value", ...] to {"key": "value", ...}
build_kwargs = {}
for arg in build_args:
k, v = arg.split("=", 1)
build_kwargs[k] = v

# Convert label args from ["key=value", ...] to {"key": "value", ...}
label_kwargs = {}
for arg in labels:
k, v = arg.split("=", 1)
label_kwargs[k] = v

try:
docker_client = docker.from_env()
except docker.errors.DockerException:
Expand All @@ -89,6 +97,7 @@ def build_component( # ruff: noqa: PLR0912, PLR0915
pull=pull,
target=target,
decode=True,
labels=label_kwargs,
)

for chunk in logs:
Expand Down
8 changes: 8 additions & 0 deletions src/fondant/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ def register_build(parent_parser):
help="Name of the build-stage to build in a multi-stage Dockerfile.",
)

parser.add_argument(
"--label",
action="append",
help="Label passed to `docker build` and assigned to the container. Format {key}={value}, can be repeated.",
default=[],
)

parser.set_defaults(func=build)


Expand All @@ -265,6 +272,7 @@ def build(args):
nocache=args.nocache,
pull=args.pull,
target=args.target,
labels=args.label,
)


Expand Down
2 changes: 2 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ def test_component_build(mock_build, mock_push):
nocache=True,
pull=True,
target="base",
label=["label_0_key=label_0_value", "label_1_key=label_1_value"],
)

# Set up the return values for the mocked methods
Expand All @@ -373,6 +374,7 @@ def test_component_build(mock_build, mock_push):
pull=True,
target="base",
decode=True,
labels={"label_0_key": "label_0_value", "label_1_key": "label_1_value"},
)

mock_push.assert_called_with("image", tag="test", stream=True, decode=True)
Expand Down

0 comments on commit 139e165

Please sign in to comment.