Skip to content

Commit a36ebe6

Browse files
authored
fix: make oci_tarball work with disjoint output root (#614)
1 parent 9e70404 commit a36ebe6

File tree

4 files changed

+79
-8
lines changed

4 files changed

+79
-8
lines changed

examples/assertion/BUILD.bazel

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
load("@aspect_bazel_lib//lib:tar.bzl", "tar")
2+
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup")
23
load("@bazel_skylib//rules:build_test.bzl", "build_test")
3-
load("//oci:defs.bzl", "oci_image")
4+
load("//oci:defs.bzl", "oci_image", "oci_tarball")
45

56
# Case 1: image name containing a capital case.
67
oci_image(
@@ -46,12 +47,60 @@ oci_image(
4647
],
4748
)
4849

50+
# Case 4: Transition an oci_image and feed to oci_tarball
51+
oci_image(
52+
name = "case4",
53+
architecture = "arm64",
54+
os = "linux",
55+
)
56+
57+
platform_transition_filegroup(
58+
name = "case4_transition",
59+
srcs = [":case4"],
60+
target_platform = "//examples:linux_arm64",
61+
)
62+
63+
oci_tarball(
64+
name = "case4_tarball",
65+
image = ":case4_transition",
66+
repo_tags = ["case4:example"],
67+
)
68+
69+
filegroup(
70+
name = "case4_tarball_tar",
71+
srcs = [":case4_tarball"],
72+
output_group = "tarball",
73+
)
74+
75+
# Case 5:
76+
77+
# Case 4: An oci_image directly fed into oci_tarball
78+
oci_image(
79+
name = "case5",
80+
architecture = "arm64",
81+
os = "linux",
82+
)
83+
84+
oci_tarball(
85+
name = "case5_tarball",
86+
image = ":case5",
87+
repo_tags = ["case5:example"],
88+
)
89+
90+
filegroup(
91+
name = "case5_tarball_tar",
92+
srcs = [":case5_tarball"],
93+
output_group = "tarball",
94+
)
95+
4996
# build them as test.
5097
build_test(
5198
name = "test",
5299
targets = [
53100
":imagE",
54101
":case2",
55102
":case3",
103+
":case4_tarball_tar",
104+
":case5_tarball_tar",
56105
],
57106
)

oci/private/tarball.bzl

+2-5
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ def _tarball_impl(ctx):
113113
"{{jq_path}}": jq.bin.path,
114114
"{{tar}}": bsdtar.tarinfo.binary.path,
115115
"{{image_dir}}": image.path,
116-
"{{bindir}}": ctx.bin_dir.path,
117116
"{{output}}": mtree_spec.path,
118117
"{{json_out}}": image_json.path,
119118
}
@@ -148,7 +147,7 @@ def _tarball_impl(ctx):
148147
output = exe,
149148
substitutions = {
150149
"{{TAR}}": bsdtar.tarinfo.binary.short_path,
151-
"{{mtree_path}}": mtree_spec.short_path,
150+
"{{mtree_path}}": mtree_spec.path,
152151
"{{loader}}": ctx.file.loader.path if ctx.file.loader else "",
153152
},
154153
is_executable = True,
@@ -160,10 +159,8 @@ def _tarball_impl(ctx):
160159
tar_inputs = depset(direct = mtree_outputs, transitive = [mtree_inputs])
161160
tar_args = ctx.actions.args()
162161
tar_args.add_all(["--create", "--no-xattr", "--no-mac-metadata"])
163-
tar_args.add_all(["--cd", ctx.bin_dir.path])
164162
tar_args.add("--file", tarball)
165-
# To reference our mtree spec file, we have to undo the --cd by removing three path segments
166-
tar_args.add(mtree_spec, format = "@../../../%s")
163+
tar_args.add(mtree_spec, format = "@%s")
167164
ctx.actions.run(
168165
executable = bsdtar.tarinfo.binary,
169166
inputs = tar_inputs,

oci/private/tarball.sh.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ readonly OUTPUT="{{output}}"
1717
function add_to_tar() {
1818
content=$1
1919
tar_path=$2
20-
echo >>"${OUTPUT}" "${tar_path} uid=0 gid=0 mode=0755 time=1672560000 type=file content=${content#{{bindir}}/}"
20+
echo >>"${OUTPUT}" "${tar_path} uid=0 gid=0 mode=0755 time=1672560000 type=file content=${content}"
2121
}
2222

2323
MANIFEST_DIGEST=$(${JQ} -r '.manifests[0].digest | sub(":"; "/")' "${INDEX_FILE}" | tr -d '"')

oci/private/tarball_run.sh.tpl

+26-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,31 @@ else
1313
exit 1
1414
fi
1515

16+
17+
# The execroot detection code is copied from https://github.com/aspect-build/rules_js/blob/d4ac7025a83192d011b7dd7447975a538e34c49b/js/private/js_binary.sh.tpl#L169-L217
18+
if [[ "$PWD" == *"/bazel-out/"* ]]; then
19+
bazel_out_segment="/bazel-out/"
20+
elif [[ "$PWD" == *"/BAZEL-~1/"* ]]; then
21+
bazel_out_segment="/BAZEL-~1/"
22+
elif [[ "$PWD" == *"/bazel-~1/"* ]]; then
23+
bazel_out_segment="/bazel-~1/"
24+
fi
25+
26+
if [[ "${bazel_out_segment:-}" ]]; then
27+
# We are in runfiles and we don't yet know the execroot
28+
rest="${PWD#*"$bazel_out_segment"}"
29+
index=$((${#PWD} - ${#rest} - ${#bazel_out_segment}))
30+
if [ ${index} -lt 0 ]; then
31+
echo "No 'bazel-out' folder found in path '${PWD}'" >&2
32+
exit 1
33+
fi
34+
EXECROOT="${PWD:0:$index}"
35+
else
36+
# We are in execroot or in some other context all or a manually run oci_tarball.
37+
EXECROOT="${PWD}"
38+
fi
39+
40+
1641
"$CONTAINER_CLI" load --input <(
17-
{{TAR}} --create --no-xattr --no-mac-metadata @"{{mtree_path}}"
42+
{{TAR}} --cd "$EXECROOT" --create --no-xattr --no-mac-metadata @"{{mtree_path}}"
1843
)

0 commit comments

Comments
 (0)