Skip to content

Commit 12efa46

Browse files
authored
Add support for --metadata-file option with buildx bake (#693)
Closes #504
1 parent 6dfcce4 commit 12efa46

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

python_on_whales/components/buildx/cli_wrapper.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def bake(
135135
push: bool = False,
136136
set: Dict[str, str] = {},
137137
variables: Dict[str, str] = {},
138+
metadata_file: Optional[ValidPath] = None,
138139
stream_logs: bool = False,
139140
remote_definition: Union[str, None] = None,
140141
) -> Union[Dict[str, Dict[str, Dict[str, Any]]], Iterator[str]]:
@@ -159,6 +160,7 @@ def bake(
159160
set: A list of overrides in the form `"targetpattern.key=value"`.
160161
variables: A dict containing the values of the variables defined in the
161162
hcl file. See <https://github.com/docker/buildx#hcl-variables-and-functions>
163+
metadata_file: Write build results metadata to the given file
162164
remote_definition: Remote context in which to find bake files
163165
164166
# Returns
@@ -211,6 +213,8 @@ def bake(
211213
full_cmd.add_args_iterable_or_single("--set", format_mapping_for_cli(set))
212214
if remote_definition is not None:
213215
full_cmd.append(remote_definition)
216+
if metadata_file is not None:
217+
full_cmd.add_simple_arg("--metadata-file", metadata_file)
214218
targets = to_list(targets)
215219
env = dict(variables)
216220
if print:

tests/python_on_whales/components/buildx/test_buildx_cli_wrapper.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,109 @@ def test_bake_stream_logs(monkeypatch):
705705
assert output[-1].startswith("#")
706706

707707

708+
@pytest.mark.usefixtures("with_docker_driver")
709+
@pytest.mark.usefixtures("change_cwd")
710+
@pytest.mark.parametrize("only_print", [True, False])
711+
def test_bake_metadata_file_output(only_print, tmp_path):
712+
metadata_path = tmp_path / "metadata.json"
713+
714+
config = docker.buildx.bake(
715+
files=[bake_file], metadata_file=metadata_path, print=only_print
716+
)
717+
assert config == {
718+
"group": {"default": {"targets": ["my_out1", "my_out2"]}},
719+
"target": {
720+
"my_out1": {
721+
"context": ".",
722+
"dockerfile": "Dockerfile",
723+
"tags": ["pretty_image1:1.0.0"],
724+
"target": "out1",
725+
},
726+
"my_out2": {
727+
"context": ".",
728+
"dockerfile": "Dockerfile",
729+
"tags": ["pretty_image2:1.0.0"],
730+
"target": "out2",
731+
},
732+
},
733+
}
734+
if not only_print:
735+
assert metadata_path.is_file()
736+
with open(metadata_path, "r") as f:
737+
metadata = json.load(f)
738+
assert len(metadata.keys()) == len(
739+
config["target"]
740+
) # two target builds expected
741+
for target_name, target_metadata in metadata.items():
742+
assert target_name in config["target"]
743+
assert (
744+
"docker.io/library/{}".format(config["target"][target_name]["tags"][0])
745+
in target_metadata["image.name"]
746+
)
747+
748+
749+
@pytest.mark.usefixtures("with_docker_driver")
750+
@pytest.mark.usefixtures("change_cwd")
751+
@pytest.mark.parametrize("only_print", [True, False])
752+
def test_bake_metadata_file_option(only_print, monkeypatch, tmp_path):
753+
recorded = {}
754+
755+
def fake_run(cmd, capture_stderr=True, env={}):
756+
recorded["cmd"] = list(cmd)
757+
return "{}"
758+
759+
monkeypatch.setattr(python_on_whales.components.buildx.cli_wrapper, "run", fake_run)
760+
761+
metadata_path = tmp_path / "metadata.json"
762+
763+
docker.buildx.bake(files=[bake_file], print=only_print, metadata_file=metadata_path)
764+
765+
cmd = recorded.get("cmd", [])
766+
assert "--metadata-file" in cmd
767+
assert metadata_path in cmd or str(metadata_path) in map(str, cmd)
768+
769+
770+
@pytest.mark.usefixtures("with_docker_driver")
771+
@pytest.mark.usefixtures("change_cwd")
772+
@pytest.mark.parametrize("only_print", [True, False])
773+
def test_bake_without_metadata_file_option(only_print, monkeypatch, tmp_path):
774+
recorded = {}
775+
776+
def fake_run(cmd, capture_stderr=True, env={}):
777+
recorded["cmd"] = list(cmd)
778+
return "{}"
779+
780+
monkeypatch.setattr(python_on_whales.components.buildx.cli_wrapper, "run", fake_run)
781+
782+
docker.buildx.bake(files=[bake_file], print=only_print)
783+
784+
cmd = recorded.get("cmd", [])
785+
assert "--metadata-file" not in cmd
786+
787+
788+
@pytest.mark.usefixtures("with_docker_driver")
789+
@pytest.mark.usefixtures("change_cwd")
790+
@pytest.mark.parametrize("only_print", [True, False])
791+
def test_bake_metadata_file_str_path_option(only_print, monkeypatch, tmp_path):
792+
recorded = {}
793+
794+
def fake_run(cmd, capture_stderr=True, env={}):
795+
recorded["cmd"] = list(cmd)
796+
return "{}"
797+
798+
monkeypatch.setattr(python_on_whales.components.buildx.cli_wrapper, "run", fake_run)
799+
800+
metadata_path = tmp_path / "metadata.json"
801+
802+
docker.buildx.bake(
803+
files=[bake_file], print=only_print, metadata_file=str(metadata_path)
804+
)
805+
806+
cmd = recorded.get("cmd", [])
807+
assert "--metadata-file" in cmd
808+
assert metadata_path in cmd or str(metadata_path) in map(str, cmd)
809+
810+
708811
@pytest.mark.usefixtures("with_docker_driver")
709812
@pytest.mark.usefixtures("prune_all")
710813
def test_prune_all_empty():

0 commit comments

Comments
 (0)