Skip to content

Commit 590ffa8

Browse files
committed
docker_push: allow passing cred_helpers from toolchain into commands
When an environment sets up the credentials helpers and configuration it's expected the behaviour that container_pull provides is also offered by container_push
1 parent 8e70c6b commit 590ffa8

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

container/push-tag.bat.tpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@
1414
@REM limitations under the License.
1515

1616
SET RUNFILES=..
17-
17+
18+
%{env_path}
19+
1820
%{container_pusher} %{args} "$@"

container/push-tag.sh.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ function guess_runfiles() {
2828

2929
RUNFILES="${PYTHON_RUNFILES:-$(guess_runfiles)}"
3030

31+
%{env_path}
32+
3133
%{container_pusher} %{args} "$@"

container/push.bzl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ def _get_runfile_path(ctx, f):
3535
else:
3636
return "${RUNFILES}/%s" % runfile(ctx, f)
3737

38+
def _get_env_path(ctx, toolchain_info):
39+
cred_helpers = toolchain_info.cred_helpers
40+
if len(cred_helpers) == 0:
41+
return ""
42+
43+
# if cred_helpers are configured in the toolchain, then we need to make those part of the PATH environment
44+
cred_helpers_path = [ x.dirname.replace('external/', '') for x in cred_helpers]
45+
46+
if ctx.attr.windows_paths:
47+
cred_helpers_path = ";".join([ "%{RUNFILES}%\\%s" % x.replace("/", "\\") for x in cred_helpers_path ])
48+
return "SET PATH=%PATH%;%s" % cred_helpers_path
49+
50+
cred_helpers_path = ":".join([ "${RUNFILES}/%s" % x for x in cred_helpers_path ])
51+
return "export PATH=${PATH}:%s" % cred_helpers_path
52+
3853
def _impl(ctx):
3954
"""Core implementation of container_push."""
4055

@@ -110,7 +125,7 @@ def _impl(ctx):
110125
if toolchain_info.client_config != "":
111126
pusher_args += ["-client-config-dir", str(toolchain_info.client_config)]
112127

113-
pusher_runfiles = [ctx.executable._pusher] + pusher_input
128+
pusher_runfiles = [ctx.executable._pusher] + pusher_input + toolchain_info.cred_helpers
114129
runfiles = ctx.runfiles(files = pusher_runfiles)
115130
runfiles = runfiles.merge(ctx.attr._pusher[DefaultInfo].default_runfiles)
116131

@@ -121,6 +136,7 @@ def _impl(ctx):
121136
substitutions = {
122137
"%{args}": " ".join(pusher_args),
123138
"%{container_pusher}": _get_runfile_path(ctx, ctx.executable._pusher),
139+
"%{env_path}": _get_env_path(ctx, toolchain_info)
124140
},
125141
is_executable = True,
126142
)

toolchains/docker/BUILD.tpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ load("@io_bazel_rules_docker//toolchains/docker:toolchain.bzl", "docker_toolchai
2121
docker_toolchain(
2222
name = "toolchain",
2323
client_config = "%{DOCKER_CONFIG}",
24+
%{CRED_HELPERS_ATTR}
2425
%{BUILD_TAR_ATTR}
2526
%{GZIP_ATTR}
2627
%{TOOL_ATTR}

toolchains/docker/toolchain.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ DockerToolchainInfo = provider(
2424
"the value of the DOCKER_CONFIG environment variable " +
2525
"will be used. If DOCKER_CONFIG is not defined, the " +
2626
"home directory will be used.",
27+
"cred_helpers": "Custom credential helpers to add into the $PATH of " +
28+
"the push and pull tools",
2729
"docker_flags": "Additional flags to the docker command",
2830
"gzip_path": "Optional path to the gzip binary.",
2931
"gzip_target": "Optional Bazel target for the gzip tool. " +
@@ -45,6 +47,7 @@ def _docker_toolchain_impl(ctx):
4547
build_tar_target = ctx.attr.build_tar_target,
4648
docker_flags = ctx.attr.docker_flags,
4749
client_config = ctx.attr.client_config,
50+
cred_helpers = ctx.files.cred_helpers,
4851
gzip_path = ctx.attr.gzip_path,
4952
gzip_target = ctx.attr.gzip_target,
5053
tool_path = ctx.attr.tool_path,
@@ -78,6 +81,11 @@ docker_toolchain = rule(
7881
"DOCKER_CONFIG is not defined, the home directory will be " +
7982
"used.",
8083
),
84+
"cred_helpers": attr.label_list(
85+
default = [],
86+
doc = "List of credentials helper to add to $PATH",
87+
allow_files = True,
88+
),
8189
"docker_flags": attr.string_list(
8290
doc = "Additional flags to the docker command",
8391
),
@@ -175,6 +183,7 @@ def _toolchain_configure_impl(repository_ctx):
175183
Label("@io_bazel_rules_docker//toolchains/docker:BUILD.tpl"),
176184
{
177185
"%{BUILD_TAR_ATTR}": "%s" % build_tar_attr,
186+
"%{CRED_HELPERS_ATTR}": ("cred_helpers = %s," % str(repository_ctx.attr.cred_helpers)) if repository_ctx.attr.cred_helpers else "",
178187
"%{DOCKER_CONFIG}": "%s" % client_config_dir,
179188
"%{DOCKER_FLAGS}": "%s" % "\", \"".join(docker_flags),
180189
"%{TOOL_ATTR}": "%s" % tool_attr,

0 commit comments

Comments
 (0)