Skip to content

Commit 6ea707b

Browse files
authored
Support credential helpers in container_pull (#2034)
1 parent 8cf7db7 commit 6ea707b

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

container/pull.bzl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ _container_pull_attrs = {
6464
""",
6565
mandatory = False,
6666
),
67+
"cred_helpers": attr.label_list(
68+
doc = """Labels to a list of credential helper binaries that are configured in `docker_client_config`.
69+
70+
More about credential helpers: https://docs.docker.com/engine/reference/commandline/login/#credential-helpers
71+
""",
72+
mandatory = False,
73+
),
6774
"import_tags": attr.string_list(
6875
default = [],
6976
doc = "Tags to be propagated to generated rules.",
@@ -220,6 +227,14 @@ def _impl(repository_ctx):
220227
args.extend(["-timeout", str(repository_ctx.attr.timeout)])
221228
kwargs["timeout"] = repository_ctx.attr.timeout
222229

230+
if repository_ctx.attr.cred_helpers:
231+
kwargs["environment"] = {
232+
"PATH": "{}:{}".format(
233+
":".join([str(repository_ctx.path(helper).dirname) for helper in repository_ctx.attr.cred_helpers]),
234+
repository_ctx.os.environ.get("PATH"),
235+
),
236+
}
237+
223238
result = repository_ctx.execute(args, **kwargs)
224239
if result.return_code:
225240
fail("Pull command failed: %s (%s)" % (result.stderr, " ".join([str(a) for a in args])))

docs/container.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ The created target can be referenced as `@label_name//image`.
160160
## container_pull
161161

162162
<pre>
163-
container_pull(<a href="#container_pull-name">name</a>, <a href="#container_pull-architecture">architecture</a>, <a href="#container_pull-cpu_variant">cpu_variant</a>, <a href="#container_pull-digest">digest</a>, <a href="#container_pull-docker_client_config">docker_client_config</a>, <a href="#container_pull-import_tags">import_tags</a>, <a href="#container_pull-os">os</a>,
164-
<a href="#container_pull-os_features">os_features</a>, <a href="#container_pull-os_version">os_version</a>, <a href="#container_pull-platform_features">platform_features</a>, <a href="#container_pull-puller_darwin">puller_darwin</a>, <a href="#container_pull-puller_linux_amd64">puller_linux_amd64</a>,
165-
<a href="#container_pull-puller_linux_arm64">puller_linux_arm64</a>, <a href="#container_pull-puller_linux_s390x">puller_linux_s390x</a>, <a href="#container_pull-registry">registry</a>, <a href="#container_pull-repo_mapping">repo_mapping</a>, <a href="#container_pull-repository">repository</a>, <a href="#container_pull-tag">tag</a>,
166-
<a href="#container_pull-timeout">timeout</a>)
163+
container_pull(<a href="#container_pull-name">name</a>, <a href="#container_pull-architecture">architecture</a>, <a href="#container_pull-cpu_variant">cpu_variant</a>, <a href="#container_pull-cred_helpers">cred_helpers</a>, <a href="#container_pull-digest">digest</a>, <a href="#container_pull-docker_client_config">docker_client_config</a>,
164+
<a href="#container_pull-import_tags">import_tags</a>, <a href="#container_pull-os">os</a>, <a href="#container_pull-os_features">os_features</a>, <a href="#container_pull-os_version">os_version</a>, <a href="#container_pull-platform_features">platform_features</a>, <a href="#container_pull-puller_darwin">puller_darwin</a>,
165+
<a href="#container_pull-puller_linux_amd64">puller_linux_amd64</a>, <a href="#container_pull-puller_linux_arm64">puller_linux_arm64</a>, <a href="#container_pull-puller_linux_s390x">puller_linux_s390x</a>, <a href="#container_pull-registry">registry</a>, <a href="#container_pull-repo_mapping">repo_mapping</a>,
166+
<a href="#container_pull-repository">repository</a>, <a href="#container_pull-tag">tag</a>, <a href="#container_pull-timeout">timeout</a>)
167167
</pre>
168168

169169
A repository rule that pulls down a Docker base image in a manner suitable for use with the `base` attribute of `container_image`.
@@ -196,6 +196,7 @@ please use the bazel startup flag `--loading_phase_threads=1` in your bazel invo
196196
| <a id="container_pull-name"></a>name | A unique name for this repository. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
197197
| <a id="container_pull-architecture"></a>architecture | Which CPU architecture to pull if this image refers to a multi-platform manifest list, default 'amd64'. | String | optional | "amd64" |
198198
| <a id="container_pull-cpu_variant"></a>cpu_variant | Which CPU variant to pull if this image refers to a multi-platform manifest list. | String | optional | "" |
199+
| <a id="container_pull-cred_helpers"></a>cred_helpers | Labels to a list of credential helper binaries that are configured in <code>docker_client_config</code>.<br><br> More about credential helpers: https://docs.docker.com/engine/reference/commandline/login/#credential-helpers | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
199200
| <a id="container_pull-digest"></a>digest | The digest of the image to pull. | String | optional | "" |
200201
| <a id="container_pull-docker_client_config"></a>docker_client_config | Specifies a Bazel label of the config.json file.<br><br> Don't use this directly. Instead, specify the docker configuration directory using a custom docker toolchain configuration. Look for the <code>client_config</code> attribute in <code>docker_toolchain_configure</code> [here](https://github.com/bazelbuild/rules_docker#setup) for details. See [here](https://github.com/bazelbuild/rules_docker#container_pull-custom-client-configuration) for an example on how to use container_pull after configuring the docker toolchain<br><br> When left unspecified (ie not set explicitly or set by the docker toolchain), docker will use the directory specified via the <code>DOCKER_CONFIG</code> environment variable.<br><br> If <code>DOCKER_CONFIG</code> isn't set, docker falls back to <code>$HOME/.docker</code>. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
201202
| <a id="container_pull-import_tags"></a>import_tags | Tags to be propagated to generated rules. | List of strings | optional | [] |

toolchains/docker/pull.bzl.tpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ def container_pull(**kwargs):
77
fail("docker_client_config attribute should not be set on the container_pull created by the custom docker toolchain configuration")
88
_container_pull(
99
docker_client_config="%{docker_client_config}",
10+
cred_helpers=%{cred_helpers},
1011
**kwargs
1112
)

toolchains/docker/toolchain.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ def _toolchain_configure_impl(repository_ctx):
160160
Label("@io_bazel_rules_docker//toolchains/docker:pull.bzl.tpl"),
161161
{
162162
"%{docker_client_config}": str(repository_ctx.attr.client_config),
163+
"%{cred_helpers}": str(repository_ctx.attr.cred_helpers),
163164
},
164165
False,
165166
)
@@ -202,6 +203,14 @@ toolchain_configure = repository_rule(
202203
"docker tool (typically, the home directory) will be " +
203204
"used.",
204205
),
206+
"cred_helpers": attr.string_list(
207+
mandatory = False,
208+
doc = """Labels to a list of credential helpers binaries that are configured in `client_config`.
209+
210+
More about credential helpers: https://docs.docker.com/engine/reference/commandline/login/#credential-helpers
211+
""",
212+
default = [],
213+
),
205214
"docker_flags": attr.string_list(
206215
mandatory = False,
207216
doc = "List of additional flag arguments to the docker command.",

0 commit comments

Comments
 (0)