Skip to content

Commit 404f6c2

Browse files
authored
Fix incorrect use of runfiles for helm_push (#245)
closes #109
1 parent 94bb56a commit 404f6c2

File tree

5 files changed

+99
-37
lines changed

5 files changed

+99
-37
lines changed

helm/private/pusher/BUILD.bazel

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
package(default_visibility = ["//visibility:public"])
1+
load("@rules_go//go:def.bzl", "go_binary")
22

3-
exports_files([
4-
"helm_push.bat.template",
5-
"helm_push.sh.template",
6-
])
7-
8-
alias(
9-
name = "template",
10-
actual = select({
11-
"@platforms//os:windows": ":helm_push.bat.template",
12-
"//conditions:default": ":helm_push.sh.template",
13-
}),
3+
go_binary(
4+
name = "pusher",
5+
srcs = ["pusher.go"],
6+
visibility = ["//visibility:public"],
7+
deps = [
8+
"//helm/private/helm_utils",
9+
"@rules_go//go/runfiles",
10+
],
1411
)

helm/private/pusher/helm_push.bat.template

Lines changed: 0 additions & 3 deletions
This file was deleted.

helm/private/pusher/helm_push.sh.template

Lines changed: 0 additions & 5 deletions
This file was deleted.

helm/private/pusher/pusher.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"log"
6+
"os"
7+
"os/exec"
8+
9+
"github.com/bazelbuild/rules_go/go/runfiles"
10+
"github.com/periareon/rules_helm/helm/private/helm_utils"
11+
)
12+
13+
func main() {
14+
argsRlocation := os.Getenv("RULES_HELM_PUSHER_ARGS_FILE")
15+
if argsRlocation == "" {
16+
log.Fatalf("RULES_HELM_PUSHER_ARGS_FILE environment variable is not set")
17+
}
18+
19+
argsFilePath := helm_utils.GetRunfile(argsRlocation)
20+
21+
file, err := os.Open(argsFilePath)
22+
if err != nil {
23+
log.Fatalf("Failed to open args file %s: %v", argsFilePath, err)
24+
}
25+
defer file.Close()
26+
27+
var imagePushers []string
28+
scanner := bufio.NewScanner(file)
29+
for scanner.Scan() {
30+
line := scanner.Text()
31+
if line != "" {
32+
imagePushers = append(imagePushers, helm_utils.GetRunfile(line))
33+
}
34+
}
35+
if err := scanner.Err(); err != nil {
36+
log.Fatalf("Failed to read args file: %v", err)
37+
}
38+
39+
r, err := runfiles.New()
40+
if err != nil {
41+
log.Fatalf("Unable to create runfiles: %v", err)
42+
}
43+
44+
for _, pusher := range imagePushers {
45+
cmd := exec.Command(pusher)
46+
cmd.Stdout = os.Stdout
47+
cmd.Stderr = os.Stderr
48+
cmd.Env = append(os.Environ(), r.Env()...)
49+
50+
log.Printf("Running image pusher: %s", pusher)
51+
if err := cmd.Run(); err != nil {
52+
log.Fatalf("Failed to run image pusher %s: %v", pusher, err)
53+
}
54+
}
55+
}

helm/private/registry.bzl

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,16 @@ def _helm_push_images_impl(ctx):
144144
toolchain = ctx.toolchains[Label("//helm:toolchain_type")]
145145

146146
if toolchain.helm.basename.endswith(".exe"):
147-
pusher = ctx.actions.declare_file(ctx.label.name + ".bat")
147+
pusher_wrapper = ctx.actions.declare_file(ctx.label.name + ".exe")
148148
else:
149-
pusher = ctx.actions.declare_file(ctx.label.name + ".sh")
149+
pusher_wrapper = ctx.actions.declare_file(ctx.label.name)
150+
151+
symlink(
152+
ctx = ctx,
153+
target_file = ctx.executable._pusher,
154+
output = pusher_wrapper,
155+
is_executable = True,
156+
)
150157

151158
pkg_info = ctx.attr.package[HelmPackageInfo]
152159

@@ -155,27 +162,32 @@ def _helm_push_images_impl(ctx):
155162
pkg_info = pkg_info,
156163
)
157164

158-
image_commands = "\n".join([file.short_path for file in image_pushers])
165+
args = ctx.actions.args()
166+
args.set_param_file_format("multiline")
167+
for p in image_pushers:
168+
args.add(rlocationpath(p, ctx.workspace_name))
159169

160-
ctx.actions.expand_template(
161-
template = ctx.file._pusher,
162-
output = pusher,
163-
substitutions = {
164-
"{image_pushers}": image_commands,
165-
},
166-
is_executable = True,
170+
args_file = ctx.actions.declare_file("{}.args.txt".format(ctx.label.name))
171+
ctx.actions.write(
172+
output = args_file,
173+
content = args,
167174
)
168175

169-
runfiles = ctx.runfiles([pusher]).merge(image_runfiles)
176+
runfiles = ctx.runfiles([
177+
pusher_wrapper,
178+
args_file,
179+
]).merge(image_runfiles)
170180

171181
return [
172182
DefaultInfo(
173-
files = depset([pusher]),
183+
files = depset([pusher_wrapper]),
174184
runfiles = runfiles,
175-
executable = pusher,
185+
executable = pusher_wrapper,
176186
),
177187
RunEnvironmentInfo(
178-
environment = ctx.attr.env,
188+
environment = ctx.attr.env | {
189+
"RULES_HELM_PUSHER_ARGS_FILE": rlocationpath(args_file, ctx.workspace_name),
190+
},
179191
),
180192
]
181193

@@ -192,10 +204,16 @@ helm_push_images = rule(
192204
providers = [HelmPackageInfo],
193205
mandatory = True,
194206
),
207+
"_copier": attr.label(
208+
cfg = "exec",
209+
executable = True,
210+
default = Label("//helm/private/copier"),
211+
),
195212
"_pusher": attr.label(
196-
doc = "A template used to produce the pusher executable.",
197-
allow_single_file = True,
198-
default = Label("//helm/private/pusher:template"),
213+
doc = "A process wrapper to use for pushing images.",
214+
executable = True,
215+
cfg = "exec",
216+
default = Label("//helm/private/pusher"),
199217
),
200218
},
201219
toolchains = [

0 commit comments

Comments
 (0)