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

Use platform transition in the oci_image_index rule #531

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 54 additions & 6 deletions docs/image_index.md

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

21 changes: 9 additions & 12 deletions docs/push.md

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

95 changes: 88 additions & 7 deletions oci/private/image_index.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,96 @@ Requires `wc` and either `sha256sum` or `shasum` to be installed on the executio

```starlark
oci_image(
name = "app_linux_amd64"
name = "app_linux"
)

oci_image_index(
name = "app",
image = ":app_linux",
platforms = [
"@io_bazel_rules_go//go/toolchain:linux_amd64",
"@io_bazel_rules_go//go/toolchain:linux_arm64",
]
)
```

Deprecated use without platform transition:

```starlark
oci_image(
name = "app_linux_arm64"
name = "app_linux_amd64",
)

oci_image(
name = "app_linux_arm64",
)

oci_image_index(
name = "app",
images = [
image = [
":app_linux_amd64",
":app_linux_arm64"
]
],
)
```

Another variant for transitioning away from the deprecated use:

```starlark
oci_image(
name = "app_linux_amd64",
)

oci_image(
name = "app_linux_arm64",
)

alias(
name = "app_linux",
actual = select({
"@platforms//cpu:x86_64": ":app_linux_amd64",
"@platforms//cpu:aarch64": ":app_linux_arm64",
}),
)

oci_image_index(
name = "app",
image = ":app_linux",
platforms = [
"@io_bazel_rules_go//go/toolchain:linux_amd64",
"@io_bazel_rules_go//go/toolchain:linux_arm64",
],
)
```
"""

def _oci_platform_transition_impl(settings, attr):
if attr.platforms == []:
# No platform specified, use the current target platform only.
ret = [settings]
else:
ret = [
{
"//command_line_option:platforms": [platform],
}
for platform in attr.platforms
]
return ret

_oci_platform_transition = transition(
implementation = _oci_platform_transition_impl,
inputs = ["//command_line_option:platforms"],
outputs = ["//command_line_option:platforms"],
)

_attrs = {
"images": attr.label_list(mandatory = True, doc = "List of labels to oci_image targets."),
"images": attr.label_list(mandatory = False, doc = "(Deprecated) List of labels to oci_image targets."),
"image": attr.label(mandatory = False, doc = "An oci_image target.", cfg = _oci_platform_transition),
"platforms": attr.label_list(mandatory = False, default = [], doc = """
The platforms to build the index for. Defaults to `[]` which means that only the current target platform is used.
"""),
"_image_index_sh_tpl": attr.label(default = "image_index.sh.tpl", allow_single_file = True),
"_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"),
}

def _expand_image_to_args(image, expander):
Expand Down Expand Up @@ -56,12 +126,23 @@ def _oci_image_index_impl(ctx):

output = ctx.actions.declare_directory(ctx.label.name)

if ctx.attr.images:
if ctx.attr.image or ctx.attr.platforms:
fail("Specify either 'images' OR 'image' and 'platforms'.")
print("Deprecated use of 'images' in %s. Please change to 'image' and 'platforms'." % ctx.label)
image_files = ctx.files.images
else:
image_files = depset(transitive = [
image[DefaultInfo].files
for image in ctx.attr.image
])

args = ctx.actions.args()
args.add(output.path, format = "--output=%s")
args.add_all(ctx.files.images, map_each = _expand_image_to_args, expand_directories = False)
args.add_all(image_files, map_each = _expand_image_to_args, expand_directories = False)

ctx.actions.run(
inputs = ctx.files.images,
inputs = image_files,
arguments = [args],
outputs = [output],
executable = launcher,
Expand Down
21 changes: 9 additions & 12 deletions oci/private/push.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,16 @@ oci_push(
Push a multi-architecture image to github container registry with a semver tag

```starlark
oci_image(name = "app_linux_arm64")

oci_image(name = "app_linux_amd64")

oci_image(name = "app_windows_amd64")
oci_image(name = "app_image")

oci_image_index(
name = "app_image",
images = [
":app_linux_arm64",
":app_linux_amd64",
":app_windows_amd64",
]
name = "app_index",
image = ":app_image",
platforms = [
"@my_platforms//:linux_arm64",
"@my_platforms//:linux_amd64",
"@my_platforms//:windows_amd64",
],
)

write_file(
Expand All @@ -96,7 +93,7 @@ expand_template(
)

oci_push(
image = ":app_image",
image = ":app_index",
repository = "ghcr.io/<OWNER>/image",
remote_tags = ":stamped",
)
Expand Down
Loading