Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: publish multi-arch images #201

Closed
wants to merge 10 commits into from
49 changes: 33 additions & 16 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,22 @@ http_archive(
)

http_archive(
name = "io_bazel_rules_docker",
sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf",
urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.25.0/rules_docker-v0.25.0.tar.gz"],
name = "aspect_bazel_lib",
sha256 = "6c25c59581041ede31e117693047f972cc4700c89acf913658dc89d04c338f8d",
strip_prefix = "bazel-lib-2.5.3",
url = "https://github.com/aspect-build/bazel-lib/releases/download/v2.5.3/bazel-lib-v2.5.3.tar.gz",
)

load("@aspect_bazel_lib//lib:repositories.bzl", "register_expand_template_toolchains")

register_expand_template_toolchains()

http_archive(
name = "rules_oci",
sha256 = "c78e700939d6b6e83ff2dbc5a79e516c03e6a3b4aa123471c5fb0140b5aefac0",
strip_prefix = "rules_oci-d53a9f41536b8fdad772c98bd327223c72ba1197",
# HEAD of https://github.com/bazel-contrib/rules_oci/pull/531/files
url = "https://github.com/bazel-contrib/rules_oci/archive/d53a9f41536b8fdad772c98bd327223c72ba1197.tar.gz",
)

http_archive(
Expand All @@ -36,7 +49,7 @@ http_archive(
],
)

load("@bazel_gazelle//:deps.bzl", "go_repository")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")

# Override the version of gomock to one that includes support for
# generating mocks for function types. We can't do this through go.mod,
Expand All @@ -61,21 +74,25 @@ go_rules_dependencies()

go_register_toolchains(version = "1.21.5")

load("@io_bazel_rules_docker//repositories:repositories.bzl", container_repositories = "repositories")

container_repositories()

load("@io_bazel_rules_docker//repositories:deps.bzl", container_deps = "deps")

container_deps()

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

gazelle_dependencies()

load("@io_bazel_rules_docker//go:image.bzl", _go_image_repos = "repositories")
load("@rules_oci//oci:pull.bzl", "oci_pull")
load("@rules_oci//oci:repositories.bzl", "LATEST_CRANE_VERSION", "oci_register_toolchains")

_go_image_repos()
oci_register_toolchains(
name = "oci",
crane_version = LATEST_CRANE_VERSION,
)

oci_pull(
name = "distroless_static",
digest = "sha256:7e5c6a2a4ae854242874d36171b31d26e0539c98fc6080f942f16b03e82851ab",
image = "gcr.io/distroless/static",
platforms = [
"linux/amd64",
"linux/arm64/v8",
],
)

http_archive(
name = "com_github_bazelbuild_buildtools",
Expand Down
9 changes: 3 additions & 6 deletions cmd/bb_copy/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
load("//tools:container.bzl", "container_push_official")
load("@io_bazel_rules_docker//go:image.bzl", "go_image")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("//tools:container.bzl", "container_push_official", "multiarch_go_image")

go_library(
name = "bb_copy_lib",
Expand All @@ -27,11 +26,9 @@ go_binary(
visibility = ["//visibility:public"],
)

go_image(
multiarch_go_image(
name = "bb_copy_container",
embed = [":bb_copy_lib"],
pure = "on",
visibility = ["//visibility:public"],
binary = ":bb_copy",
)

container_push_official(
Expand Down
9 changes: 3 additions & 6 deletions cmd/bb_replicator/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
load("//tools:container.bzl", "container_push_official")
load("@io_bazel_rules_docker//go:image.bzl", "go_image")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("//tools:container.bzl", "container_push_official", "multiarch_go_image")

go_library(
name = "bb_replicator_lib",
Expand Down Expand Up @@ -29,11 +28,9 @@ go_binary(
visibility = ["//visibility:public"],
)

go_image(
multiarch_go_image(
name = "bb_replicator_container",
embed = [":bb_replicator_lib"],
pure = "on",
visibility = ["//visibility:public"],
binary = ":bb_replicator",
)

container_push_official(
Expand Down
9 changes: 3 additions & 6 deletions cmd/bb_storage/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
load("//tools:container.bzl", "container_push_official")
load("@io_bazel_rules_docker//go:image.bzl", "go_image")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("//tools:container.bzl", "container_push_official", "multiarch_go_image")

go_library(
name = "bb_storage_lib",
Expand Down Expand Up @@ -37,11 +36,9 @@ go_binary(
visibility = ["//visibility:public"],
)

go_image(
multiarch_go_image(
name = "bb_storage_container",
embed = [":bb_storage_lib"],
pure = "on",
visibility = ["//visibility:public"],
binary = ":bb_storage",
)

container_push_official(
Expand Down
12 changes: 12 additions & 0 deletions tools/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
Expand All @@ -11,3 +12,14 @@ go_library(
"@org_golang_x_lint//:lint",
],
)

# When built with --stamp, creates a non-deterministic output file for pushing images to a remote registry.
# With --nostamp, produces a deterministic output so dependents get cache hits.
expand_template(
name = "stamped_tags",
out = "_stamped.tags.txt",
stamp_substitutions = {"_TAG_": "{BUILD_SCM_TIMESTAMP}-{BUILD_SCM_REVISION}"},
substitutions = {"_TAG_": "0.0.0"},
template = ["_TAG_"],
visibility = ["//visibility:public"],
)
46 changes: 40 additions & 6 deletions tools/container.bzl
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
load("@io_bazel_rules_docker//container:container.bzl", "container_push")
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup")
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_image_index", "oci_push")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")

def multiarch_go_image(name, binary):
"""Create a container image with two variants of the given go_binary target.

Args:
name: resulting oci_image_index target
binary: label of a go_binary target; it may be transitioned to another architecture
"""
images = []
tar_target = "_{}.tar".format(name)
image_target = "_{}.image".format(name)

pkg_tar(
name = tar_target,
srcs = [binary],
include_runfiles = True,
package_dir = "app",
)

oci_image(
name = image_target,
base = "@distroless_static",
entrypoint = ["/app/{}".format(native.package_relative_label(binary).name)],
tars = [tar_target],
)

oci_image_index(
name = name,
image = image_target,
platforms = [
"@io_bazel_rules_go//go/toolchain:linux_{}".format(arch)
for arch in ["amd64", "arm64"]
],
)

def container_push_official(name, image, component):
container_push(
oci_push(
name = name,
format = "Docker",
image = image,
registry = "ghcr.io",
repository = "buildbarn/" + component,
tag = "{BUILD_SCM_TIMESTAMP}-{BUILD_SCM_REVISION}",
repository = "ghcr.io/buildbarn/" + component,
remote_tags = "@com_github_buildbarn_bb_storage//tools:stamped_tags",
)
1 change: 1 addition & 0 deletions tools/github_workflows/workflows_template.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
buildAndTestCommand: 'build',
// Building '//...' is broken for FreeBSD, because rules_docker
// doesn't want to initialize properly.
// TODO(who?): now that rules_docker is removed, this could be revisited
buildJustBinaries: true,
extension: '',
},
Expand Down
Loading