Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

439 no support for buildx build build context #440

Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion python_on_whales/components/buildx/cli_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def build(
allow: List[str] = [],
attest: Optional[Dict[str, str]] = None,
build_args: Dict[str, str] = {},
# TODO: build_context
build_contexts: Dict[str, Union[str, ValidPath]] = {},
builder: Optional[ValidBuilder] = None,
cache: bool = True,
# TODO: cache_filters
Expand Down Expand Up @@ -254,6 +254,11 @@ def build(
attest: Attestation parameters. Eg `attest={"type": "sbom", "generator": "my_image"}`
build_args: The build arguments.
ex `build_args={"PY_VERSION": "3.7.8", "UBUNTU_VERSION": "20.04"}`.
build_contexts: Additional build contexts.
`build_contexts={[name]: [value], ...}`
Supports local directories, git repositories, HTTP URL to a tarball, a docker
image defined with a `docker-image://` prefix, and the `oci-layout://` protocol.
ex `build_contexts={"project2": "../path/to/project2/src", "qumu-src": "https://github.com/qemu/qemu.git"}`.
builder: Specify which builder to use.
cache: Whether or not to use the cache
cache_from: Works only with the container driver. Loads the cache
Expand Down Expand Up @@ -317,6 +322,7 @@ def build(
if isinstance(attest, dict):
full_cmd.add_simple_arg("--attest", format_dict_for_buildx(attest))
full_cmd.add_args_list("--build-arg", format_dict_for_cli(build_args))
full_cmd.add_args_list("--build-context", format_dict_for_cli(build_contexts))
full_cmd.add_simple_arg("--builder", builder)
full_cmd.add_args_list("--label", format_dict_for_cli(labels))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import tarfile

import pytest

Expand All @@ -13,6 +14,21 @@
RUN touch /dada
"""

dockerfile_content2 = """
FROM busybox
COPY --from=test_context README.md /README.md
"""

dockerfile_content3 = """
FROM test_context
RUN touch /dada
"""

dockerfile_content4 = """
FROM busybox
COPY --from=test_context /dada /dada
"""


@pytest.fixture
def with_docker_driver():
Expand All @@ -30,6 +46,25 @@ def with_container_driver():
docker.buildx.use(current_builder)


@pytest.fixture
def with_oci_layout_compliant_dir(tmp_path):
(tmp_path / "Dockerfile").write_text(dockerfile_content1)

# Build the oci layout compliant directory
tar_path = os.path.join(tmp_path, "oci-layout.tar")
oci_folder_path = os.path.join(tmp_path, "oci-layout")

current_builder = docker.buildx.inspect()
with docker.buildx.create(use=True):
docker.buildx.build(tmp_path, output={"type": "oci", "dest": tar_path})
docker.buildx.use(current_builder)

# Extract tar to directory
tar = tarfile.open(tar_path)
tar.extractall(oci_folder_path)
tar.close()
bigcat2014 marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.usefixtures("with_docker_driver")
def test_buildx_build(tmp_path):
(tmp_path / "Dockerfile").write_text(dockerfile_content1)
Expand Down Expand Up @@ -251,6 +286,63 @@ def test_buildx_build_attestations(tmp_path, kwargs):
docker.buildx.build(tmp_path, **kwargs)


# Does the build work when passing extra contexts
# without making use of them in the Dockerfile
@pytest.mark.usefixtures("with_container_driver")
def test_buildx_build_build_context1(tmp_path):
(tmp_path / "Dockerfile").write_text(dockerfile_content1)
docker.buildx.build(tmp_path, build_contexts=dict(test_context="."))


# Does the build work when passing extra contexts
# when the Dockerfile does make use of them
@pytest.mark.usefixtures("with_container_driver")
@pytest.mark.parametrize(
"test_context",
[
# Test with local directory
os.path.join(os.path.dirname(__file__), "../../../.."),
bigcat2014 marked this conversation as resolved.
Show resolved Hide resolved
# Test with git repo
"https://github.com/gabrieldemarmiesse/python-on-whales.git",
],
)
def test_buildx_build_build_context2(tmp_path, test_context):
(tmp_path / "Dockerfile").write_text(dockerfile_content2)
docker.buildx.build(tmp_path, build_contexts=dict(test_context=test_context))


# Test with oci layout compliant directory
@pytest.mark.usefixtures("with_oci_layout_compliant_dir")
@pytest.mark.usefixtures("with_container_driver")
def test_buildx_build_build_context_oci(tmp_path):
(tmp_path / "Dockerfile").write_text(dockerfile_content4)
docker.buildx.build(
tmp_path,
build_contexts=dict(
test_context=f"oci-layout://{os.path.join(tmp_path, 'oci-layout')}"
),
)


# Test with docker image
@pytest.mark.usefixtures("with_container_driver")
def test_buildx_build_build_context_image(tmp_path):
(tmp_path / "Dockerfile").write_text(dockerfile_content3)
docker.buildx.build(
tmp_path,
build_contexts=dict(test_context="docker-image://busybox:1.36.0"),
)


# Does the build fail when NOT passing extra contexts
# when the dockerfile does make use of them
@pytest.mark.usefixtures("with_container_driver")
def test_buildx_build_build_context_fail(tmp_path):
(tmp_path / "Dockerfile").write_text(dockerfile_content2)
with pytest.raises(DockerException):
docker.buildx.build(tmp_path)


def test_buildx_build_context_manager2(tmp_path):
(tmp_path / "Dockerfile").write_text(dockerfile_content1)
buildx_builder = docker.buildx.create()
Expand Down
Loading