Skip to content

Commit 71600cc

Browse files
rbucklandkaosRamon Buckland
authored
Docker: Add full_directory interpolation value for repository configuration. (#20530)
Add the BUILD file full path, as a variable to the docker backend configuration. --------- Co-authored-by: Andreas Stenius <[email protected]> Co-authored-by: Ramon Buckland <[email protected]>
1 parent a7f86a0 commit 71600cc

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

docs/docs/docker/tagging-docker-images.mdx

+4-3
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ docker_image(
130130

131131
The default placeholders are:
132132

133-
- `{directory}`: The directory the docker_image's BUILD file is in.
134-
- `{parent_directory}`: The parent directory of `{directory}`.
135-
- `{name}`: The name of the docker_image target.
133+
- `{name}`: The name of the `docker_image` target.
134+
- `{directory}`: The folder name of the docker_image's BUILD file.
135+
- `{parent_directory}`: The parent folder name of `{directory}`.
136+
- `{full_directory}`: The full path to the BUILD file.
136137
- `{build_args.ARG_NAME}`: Each defined Docker build arg is available for interpolation under the `build_args.` prefix.
137138
- `{default_repository}`: The default repository from configuration.
138139
- `{target_repository}`: The repository on the `docker_image` if provided, otherwise the default repository.

src/python/pants/backend/docker/goals/package_image.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ def format_repository(
9494
) -> str:
9595
repository_context = InterpolationContext.from_dict(
9696
{
97-
"directory": os.path.basename(self.address.spec_path),
9897
"name": self.address.target_name,
98+
"directory": os.path.basename(self.address.spec_path),
99+
"full_directory": self.address.spec_path,
99100
"parent_directory": os.path.basename(os.path.dirname(self.address.spec_path)),
100101
"default_repository": default_repository,
101102
"target_repository": self.repository.value or default_repository,

src/python/pants/backend/docker/goals/package_image_test.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ def test_build_docker_image(rule_runner: RuleRunner) -> None:
272272
name="test5",
273273
image_tags=["latest", "alpha-1.0", "alpha-1"],
274274
)
275+
docker_image(
276+
name="test6",
277+
image_tags=["1.2.3"],
278+
repository="xyz/{full_directory}/{name}",
279+
)
275280
docker_image(
276281
name="err1",
277282
repository="{bad_template}",
@@ -365,12 +370,17 @@ def test_build_docker_image(rule_runner: RuleRunner) -> None:
365370
)
366371
],
367372
)
373+
assert_build(
374+
rule_runner,
375+
Address("docker/test", target_name="test6"),
376+
"Built docker image: xyz/docker/test/test6:1.2.3",
377+
)
368378

369379
err1 = (
370380
r"Invalid value for the `repository` field of the `docker_image` target at "
371381
r"docker/test:err1: '{bad_template}'\.\n\nThe placeholder 'bad_template' is unknown\. "
372-
r"Try with one of: build_args, default_repository, directory, name, pants, "
373-
r"parent_directory, tags, target_repository\."
382+
r"Try with one of: build_args, default_repository, directory, full_directory, name, "
383+
r"pants, parent_directory, tags, target_repository\."
374384
)
375385
with pytest.raises(DockerRepositoryNameError, match=err1):
376386
assert_build(

src/python/pants/backend/docker/subsystems/docker_options.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,14 @@ def env_vars(self) -> tuple[str, ...]:
108108
The value is formatted and may reference these variables (in addition to the normal
109109
placeholders derived from the Dockerfile and build args etc):
110110
111-
{bullet_list(["name", "directory", "parent_directory", "target_repository"])}
111+
{bullet_list(["name", "directory", "parent_directory", "full_directory", "target_repository"])}
112112
113113
Example: `--default-repository="{{directory}}/{{name}}"`.
114114
115-
The `name` variable is the `docker_image`'s target name, `directory` and
116-
`parent_directory` are the name of the directory in which the BUILD file is for the
117-
target, and its parent directory respectively.
115+
The `name` variable is the `docker_image`'s target name.
116+
117+
With the directory variables available, given a sample repository path of `baz/foo/bar/BUILD`,
118+
then `directory` is `bar`, `parent_directory` is `foo` and `full_directory` will be `baz/foo/bar`.
118119
119120
Use the `repository` field to set this value directly on a `docker_image` target.
120121

0 commit comments

Comments
 (0)