Skip to content

Commit

Permalink
example: add multi arch & repo example (#8)
Browse files Browse the repository at this point in the history
Signed-off-by: thesayyn <thesayyn@gmail.com>
  • Loading branch information
thesayyn authored Sep 15, 2023
1 parent dbbbdc0 commit 0d8e246
Show file tree
Hide file tree
Showing 15 changed files with 2,288 additions and 1,093 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ bazel-*
.bazelrc.user
.idea/
.ijwb/
.DS_Store
5 changes: 5 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ apk.translate_lock(
name = "examples_oci",
lock = "//examples/oci:apko.lock.json",
)
apk.translate_lock(
name = "examples_multi_arch_and_repo",
lock = "//examples/multi_arch_and_repo:apko.lock.json",
)
use_repo(apk, "examples_multi_arch_and_repo")
use_repo(apk, "examples_lock")
use_repo(apk, "examples_wolfi_base")
use_repo(apk, "examples_oci")
1,604 changes: 548 additions & 1,056 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

25 changes: 15 additions & 10 deletions apko/private/apko_image.bzl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"A rule for running apko with prepopulated cache"

_ATTRS = {
"packages": attr.label(),
"tag": attr.string(mandatory = True),
"contents": attr.label(mandatory = True),
"config": attr.label(allow_single_file = True, mandatory = True),
"output": attr.string(default = "oci", values = ["oci", "docker"]),
"architecture": attr.string(),
"tag": attr.string(mandatory = True),
"args": attr.string_list(),
}

Expand All @@ -31,17 +32,21 @@ def _impl(ctx):
args.add("--cache-dir={}/{}/{}".format(ctx.bin_dir.path, ctx.label.package, cache_name))
args.add("--offline")

inputs = [ctx.file.config] + ctx.files.packages
if ctx.attr.architecture:
args.add("--arch")
args.add(ctx.attr.architecture)

inputs = [ctx.file.config] + ctx.files.contents

for package in ctx.files.packages:
package_owner = package.owner.workspace_name
package_cache_entry_key = package.path[package.path.find(package_owner) + len(package_owner) + 1:]
package_entry = ctx.actions.declare_file("/".join([cache_name, package_cache_entry_key]))
for content in ctx.files.contents:
content_owner = content.owner.workspace_name
content_cache_entry_key = content.path[content.path.find(content_owner) + len(content_owner) + 1:]
content_entry = ctx.actions.declare_file("/".join([cache_name, content_cache_entry_key]))
ctx.actions.symlink(
target_file = package,
output = package_entry,
target_file = content,
output = content_entry,
)
inputs.append(package_entry)
inputs.append(content_entry)

ctx.actions.run(
executable = apko_info.binary,
Expand Down
5 changes: 3 additions & 2 deletions docs/rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion e2e/smoke/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ apko_bazelrc()
apko_image(
name = "lock",
config = "apko.yaml",
packages = "@example_lock//:contents",
contents = "@example_lock//:contents",
tag = "lock:latest",
)

Expand Down
2 changes: 1 addition & 1 deletion examples/lock/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test")
apko_image(
name = "lock",
config = "apko.yaml",
packages = "@examples_lock//:contents",
contents = "@examples_lock//:contents",
tag = "lock:latest",
)

Expand Down
21 changes: 21 additions & 0 deletions examples/multi_arch_and_repo/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
load("@rules_apko//apko:defs.bzl", "apko_image")
load("@container_structure_test//:defs.bzl", "container_structure_test")

# An example demonstrating how to use multi architecture alpine packages with a lock file.
# See MODULE.bazel for how apko.lock.json is translated to @examples_multi_arch_and_repo//:contents
apko_image(
name = "image",
architecture = select({
"@platforms//cpu:arm64": "arm64",
"@platforms//cpu:x86_64": "amd64",
}),
config = "apko.yaml",
contents = "@examples_multi_arch_and_repo//:contents",
tag = "multi_arch_and_repo:latest",
)

container_structure_test(
name = "test",
configs = ["test.yaml"],
image = ":image",
)
Loading

0 comments on commit 0d8e246

Please sign in to comment.