Lorenz Brun | 901c732 | 2023-07-13 20:10:37 +0200 | [diff] [blame] | 1 | load("@io_bazel_rules_docker//container:providers.bzl", "ImageInfo") |
| 2 | |
| 3 | def _localregistry_manifest_impl(ctx): |
| 4 | manifest_out = ctx.actions.declare_file(ctx.label.name+".prototxt") |
| 5 | |
| 6 | images = [] |
| 7 | referenced = [manifest_out] |
| 8 | for i in ctx.attr.images: |
| 9 | image_info = i[ImageInfo].container_parts |
| 10 | referenced.append(image_info['config']) |
| 11 | referenced.append(image_info['config_digest']) |
| 12 | image = struct( |
| 13 | name = i.label.package + "/" + i.label.name, |
| 14 | config = struct( |
| 15 | file_path = image_info['config'].short_path, |
| 16 | digest_path = image_info['config_digest'].short_path, |
| 17 | ), |
| 18 | layers = [], |
| 19 | ) |
| 20 | for layer in zip(image_info['zipped_layer'], image_info['blobsum']): |
| 21 | referenced.append(layer[0]) |
| 22 | referenced.append(layer[1]) |
| 23 | image.layers.append(struct(file_path = layer[0].short_path, digest_path=layer[1].short_path)) |
| 24 | images.append(image) |
| 25 | |
| 26 | ctx.actions.write(manifest_out, proto.encode_text(struct(images = images))) |
| 27 | return [DefaultInfo(runfiles = ctx.runfiles(files = referenced), files = depset([manifest_out]))] |
| 28 | |
| 29 | |
| 30 | localregistry_manifest = rule( |
| 31 | implementation = _localregistry_manifest_impl, |
| 32 | doc = """ |
| 33 | Builds a manifest for serving images directly from the build files. |
| 34 | """, |
| 35 | attrs = { |
| 36 | "images": attr.label_list( |
| 37 | mandatory = True, |
| 38 | doc = """ |
| 39 | List of images (with ImageInfo provider) to be served from the local registry. |
| 40 | """, |
| 41 | providers = [ImageInfo], |
| 42 | ), |
| 43 | }, |
| 44 | ) |