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

Docker: Add full_directory interpolation value for repository configuration. #20530

Merged
merged 9 commits into from
Feb 14, 2024
1 change: 1 addition & 0 deletions docs/docs/docker/tagging-docker-images.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ docker_image(
The default placeholders are:

- `{directory}`: The directory the docker_image's BUILD file is in.
- `{build_file_path}`: The full path to the BUILD file.
- `{parent_directory}`: The parent directory of `{directory}`.
- `{name}`: The name of the docker_image target.
- `{build_args.ARG_NAME}`: Each defined Docker build arg is available for interpolation under the `build_args.` prefix.
Expand Down
1 change: 1 addition & 0 deletions src/python/pants/backend/docker/goals/package_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def format_repository(
repository_context = InterpolationContext.from_dict(
{
"directory": os.path.basename(self.address.spec_path),
"build_file_path": self.address.spec_path,
"name": self.address.target_name,
"parent_directory": os.path.basename(os.path.dirname(self.address.spec_path)),
"default_repository": default_repository,
Expand Down
12 changes: 11 additions & 1 deletion src/python/pants/backend/docker/goals/package_image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ def test_build_docker_image(rule_runner: RuleRunner) -> None:
name="test5",
image_tags=["latest", "alpha-1.0", "alpha-1"],
)
docker_image(
name="test6",
image_tags=["1.2.3"],
repository="xyz/{build_file_path}/{name}",
)
docker_image(
name="err1",
repository="{bad_template}",
Expand Down Expand Up @@ -365,11 +370,16 @@ def test_build_docker_image(rule_runner: RuleRunner) -> None:
)
],
)
assert_build(
rule_runner,
Address("docker/test", target_name="test6"),
"Built docker image: xyz/docker/test/test6:1.2.3",
)

err1 = (
r"Invalid value for the `repository` field of the `docker_image` target at "
r"docker/test:err1: '{bad_template}'\.\n\nThe placeholder 'bad_template' is unknown\. "
r"Try with one of: build_args, default_repository, directory, name, pants, "
r"Try with one of: build_args, build_file_path, default_repository, directory, name, pants, "
r"parent_directory, tags, target_repository\."
)
with pytest.raises(DockerRepositoryNameError, match=err1):
Expand Down
5 changes: 3 additions & 2 deletions src/python/pants/backend/docker/subsystems/docker_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,14 @@ def env_vars(self) -> tuple[str, ...]:
The value is formatted and may reference these variables (in addition to the normal
placeholders derived from the Dockerfile and build args etc):

{bullet_list(["name", "directory", "parent_directory", "target_repository"])}
{bullet_list(["name", "directory", "parent_directory", "build_file_path", "target_repository"])}

Example: `--default-repository="{{directory}}/{{name}}"`.

The `name` variable is the `docker_image`'s target name, `directory` and
`parent_directory` are the name of the directory in which the BUILD file is for the
target, and its parent directory respectively.
target, and its parent directory respectively; and `build_file_path` is the full repository path
to the BUILD file.

Use the `repository` field to set this value directly on a `docker_image` target.

Expand Down
Loading