Skip to content

Commit

Permalink
Add Docker global no build cache option (pantsbuild#19533)
Browse files Browse the repository at this point in the history
Resolves  pantsbuild#19350

I can't see a use case in having the option able to be set statically on
each `docker_image` target.
There are ways to achieve cache invalidation using BUILD_ARGS - e.g.
https://stackoverflow.com/a/70796677/10531450

Co-authored-by: Rhys Madigan <[email protected]>
  • Loading branch information
riisi and Rhys Madigan authored Aug 14, 2023
1 parent c41394c commit f7f487c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/python/pants/backend/docker/goals/package_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ def get_build_options(
field_set: DockerPackageFieldSet,
global_target_stage_option: str | None,
global_build_hosts_options: dict | None,
global_build_no_cache_option: bool | None,
target: Target,
) -> Iterator[str]:
# Build options from target fields inheriting from DockerBuildOptionFieldMixin
Expand Down Expand Up @@ -355,6 +356,9 @@ def get_build_options(
if target_stage:
yield from ("--target", target_stage)

if global_build_no_cache_option:
yield "--no-cache"


@rule
async def build_docker_image(
Expand Down Expand Up @@ -418,6 +422,7 @@ async def build_docker_image(
field_set=field_set,
global_target_stage_option=options.build_target_stage,
global_build_hosts_options=options.build_hosts,
global_build_no_cache_option=options.build_no_cache,
target=wrapped_target.target,
)
),
Expand Down
40 changes: 40 additions & 0 deletions src/python/pants/backend/docker/goals/package_image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def mock_get_info_file(request: CreateDigest) -> Digest:
opts.setdefault("build_target_stage", None)
opts.setdefault("build_hosts", None)
opts.setdefault("build_verbose", False)
opts.setdefault("build_no_cache", False)
opts.setdefault("env_vars", [])

docker_options = create_subsystem(
Expand Down Expand Up @@ -1011,6 +1012,45 @@ def check_docker_proc(process: Process):
)


def test_docker_build_no_cache_option(rule_runner: RuleRunner) -> None:
rule_runner.set_options(
[],
env={
"PANTS_DOCKER_BUILD_NO_CACHE": "true",
},
)
rule_runner.write_files(
{
"docker/test/BUILD": dedent(
"""\
docker_image(
name="img1",
)
"""
),
}
)

def check_docker_proc(process: Process):
assert process.argv == (
"/dummy/docker",
"build",
"--pull=False",
"--no-cache",
"--tag",
"img1:latest",
"--file",
"docker/test/Dockerfile",
".",
)

assert_build(
rule_runner,
Address("docker/test", target_name="img1"),
process_assertions=check_docker_proc,
)


def test_docker_build_hosts_option(rule_runner: RuleRunner) -> None:
rule_runner.set_options(
[],
Expand Down
4 changes: 4 additions & 0 deletions src/python/pants/backend/docker/subsystems/docker_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ def env_vars(self) -> tuple[str, ...]:
"""
),
)
build_no_cache = BoolOption(
default=False,
help="Do not use the Docker cache when building images.",
)
build_verbose = BoolOption(
default=False,
help="Whether to log the Docker output to the console. If false, only the image ID is logged.",
Expand Down

0 comments on commit f7f487c

Please sign in to comment.