From 3017af53016aa2420df44bd4288b371d65bb0d05 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Wed, 20 Sep 2023 14:46:53 -0700 Subject: [PATCH] refactor: stop using jq for stamping bazel-lib 2.0 has a breaking change for this, and expand_template is simpler --- MODULE.bazel | 2 +- docs/push.md | 18 ++++++++++++++--- e2e/smoke/MODULE.bazel | 2 +- examples/env/BUILD.bazel | 31 +++++++++++++--------------- examples/labels/BUILD.bazel | 35 ++++++++++++++++---------------- examples/push/BUILD.bazel | 23 +++++++++++++++------ examples/push/stamp_tags.bzl | 39 ------------------------------------ oci/dependencies.bzl | 6 +++--- oci/private/push.bzl | 18 ++++++++++++++--- 9 files changed, 83 insertions(+), 91 deletions(-) delete mode 100644 examples/push/stamp_tags.bzl diff --git a/MODULE.bazel b/MODULE.bazel index c48f2c9b..e02f6ea1 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -6,7 +6,7 @@ module( version = "0.0.0", ) -bazel_dep(name = "aspect_bazel_lib", version = "1.30.2") +bazel_dep(name = "aspect_bazel_lib", version = "1.32.0") bazel_dep(name = "bazel_skylib", version = "1.4.1") bazel_dep(name = "platforms", version = "0.0.5") diff --git a/docs/push.md b/docs/push.md index 5a70b12a..1270875f 100644 --- a/docs/push.md +++ b/docs/push.md @@ -91,10 +91,22 @@ oci_image_index( ] ) -# This is defined in our /examples/push -stamp_tags( +write_file( + name = "tags_tmpl", + out = "tags.txt.tmpl", + content = [ + "BUILD_VERSION", + ], +) + +# Use the value of --embed_label under --stamp, otherwise use a deterministic constant +# value to ensure cache hits for actions that depend on this. +expand_template( name = "stamped", - remote_tags = ["""($stamp.BUILD_EMBED_LABEL // "0.0.0")"""], + out = "_stamped.tags.txt", + template = "tags_tmpl", + substitutions = {"BUILD_VERSION": "0.0.0"}, + stamp_substitutions = {"BUILD_VERSION": "{{BUILD_EMBED_LABEL}}"}, ) oci_push( diff --git a/e2e/smoke/MODULE.bazel b/e2e/smoke/MODULE.bazel index 5dd81189..df87ed95 100644 --- a/e2e/smoke/MODULE.bazel +++ b/e2e/smoke/MODULE.bazel @@ -2,7 +2,7 @@ bazel_dep(name = "container_structure_test", dev_dependency = True, version = "1.15.0") bazel_dep(name = "rules_oci", dev_dependency = True, version = "0.0.0") bazel_dep(name = "platforms", version = "0.0.5") -bazel_dep(name = "aspect_bazel_lib", version = "1.25.0") +bazel_dep(name = "aspect_bazel_lib", version = "1.32.0") bazel_dep(name = "bazel_skylib", version = "1.1.1") local_path_override( diff --git a/examples/env/BUILD.bazel b/examples/env/BUILD.bazel index 346ceb6e..f66de7bc 100644 --- a/examples/env/BUILD.bazel +++ b/examples/env/BUILD.bazel @@ -1,5 +1,6 @@ load("//oci:defs.bzl", "oci_image") -load("@aspect_bazel_lib//lib:jq.bzl", "jq") +load("@bazel_skylib//rules:write_file.bzl", "write_file") +load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template") load("@rules_pkg//pkg:tar.bzl", "pkg_tar") load("@container_structure_test//:defs.bzl", "container_structure_test") @@ -8,24 +9,20 @@ pkg_tar( srcs = ["test.bash"], ) -# We don't actually work with JSON, but the jq rule happens to be stamp-aware so we can (ab)use it -# to put (non-deterministic) version information in our labels/annotations. -# You'd typically use that only when building release artifacts, not for testing, as it causes -# action cache misses. -jq( +write_file( + name = "env_tmpl", + out = "env.txt.tmpl", + content = ["BUILD_VERSION=EMBED_LABEL"], +) + +# Use the value of --embed_label under --stamp, otherwise use a deterministic constant +# value to ensure cache hits for actions that depend on this. +expand_template( name = "env", - srcs = [], out = "env.txt", - args = ["--raw-output"], - filter = """\ - # See docs for jq rule about stamping: - # https://docs.aspect.build/rules/aspect_bazel_lib/docs/jq - $ARGS.named.STAMP as $stamp | - - # Use the value of --embed_label under --stamp, otherwise use a deterministic constant - # value to ensure cache hits for actions that depend on this. - "BUILD_VERSION=" + ($stamp.BUILD_VERSION // "1.2.3") - """, + stamp_substitutions = {"EMBED_LABEL": "{{BUILD_EMBED_LABEL}}"}, + substitutions = {"EMBED_LABEL": "1.2.3"}, + template = "env_tmpl", ) oci_image( diff --git a/examples/labels/BUILD.bazel b/examples/labels/BUILD.bazel index 6a5573f0..29aec3cc 100644 --- a/examples/labels/BUILD.bazel +++ b/examples/labels/BUILD.bazel @@ -1,5 +1,6 @@ load("//oci:defs.bzl", "oci_image") -load("@aspect_bazel_lib//lib:jq.bzl", "jq") +load("@bazel_skylib//rules:write_file.bzl", "write_file") +load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template") load("@rules_pkg//pkg:tar.bzl", "pkg_tar") load("@container_structure_test//:defs.bzl", "container_structure_test") @@ -8,25 +9,23 @@ pkg_tar( srcs = ["test.bash"], ) -# We don't actually work with JSON, but the jq rule happens to be stamp-aware so we can (ab)use it -# to put (non-deterministic) version information in our labels/annotations. -# You'd typically use that only when building release artifacts, not for testing, as it causes -# action cache misses. -jq( +write_file( + name = "labels_tmpl", + out = "labels.txt.tmpl", + content = [ + "org.opencontainers.image.version=BUILD_VERSION", + "org.opencontainers.image.source=https://github.com/bazel-contrib/rules_oci", + ], +) + +# Use the value of --embed_label under --stamp, otherwise use a deterministic constant +# value to ensure cache hits for actions that depend on this. +expand_template( name = "labels", - srcs = [], out = "labels.txt", - args = ["--raw-output"], - filter = """\ - # See docs for jq rule about stamping: - # https://docs.aspect.build/rules/aspect_bazel_lib/docs/jq - $ARGS.named.STAMP as $stamp | - - # Use the value of --embed_label under --stamp, otherwise use a deterministic constant - # value to ensure cache hits for actions that depend on this. - "org.opencontainers.image.version=" + ($stamp.BUILD_EMBED_LABEL // "0.0.0"), - "org.opencontainers.image.source=https://github.com/bazel-contrib/rules_oci" - """, + stamp_substitutions = {"BUILD_VERSION": "{{BUILD_EMBED_LABEL}}"}, + substitutions = {"BUILD_VERSION": "0.0.0"}, + template = "labels_tmpl", ) oci_image( diff --git a/examples/push/BUILD.bazel b/examples/push/BUILD.bazel index ef8f799c..2a91d541 100644 --- a/examples/push/BUILD.bazel +++ b/examples/push/BUILD.bazel @@ -1,5 +1,6 @@ load("//oci:defs.bzl", "oci_image", "oci_image_index", "oci_push") -load(":stamp_tags.bzl", "stamp_tags") +load("@bazel_skylib//rules:write_file.bzl", "write_file") +load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template") oci_image( name = "image", @@ -35,15 +36,25 @@ oci_image_index( ], ) -stamp_tags( - name = "stamped", - remote_tags = [ - # With --stamp, use the --embed_label value, otherwise use 0.0.0 - """($stamp.BUILD_EMBED_LABEL // "0.0.0")""", +write_file( + name = "tags_tmpl", + out = "tags.txt.tmpl", + content = [ + "BUILD_VERSION", "nightly", ], ) +# Use the value of --embed_label under --stamp, otherwise use a deterministic constant +# value to ensure cache hits for actions that depend on this. +expand_template( + name = "stamped", + out = "_stamped.tags.txt", + stamp_substitutions = {"BUILD_VERSION": "{{BUILD_EMBED_LABEL}}"}, + substitutions = {"BUILD_VERSION": "0.0.0"}, + template = "tags_tmpl", +) + oci_push( name = "push_image_index", image = ":image_index", diff --git a/examples/push/stamp_tags.bzl b/examples/push/stamp_tags.bzl deleted file mode 100644 index e95a418a..00000000 --- a/examples/push/stamp_tags.bzl +++ /dev/null @@ -1,39 +0,0 @@ -"Helper for stamping version control info into the tag" - -load("@aspect_bazel_lib//lib:jq.bzl", "jq") -load("@bazel_skylib//lib:types.bzl", "types") - -def stamp_tags(name, remote_tags, **kwargs): - """Wrapper macro around the [jq](https://docs.aspect.build/rules/aspect_bazel_lib/docs/jq) rule. - - Produces a text file that can be used with the `remote_tags` attribute of [`oci_push`](#oci_push). - - Each entry in `remote_tags` is typically either a constant like `my-repo:latest`, or can contain a stamp expression. - The latter can use any key from `bazel-out/stable-status.txt` or `bazel-out/volatile-status.txt`. - See https://docs.aspect.build/rules/aspect_bazel_lib/docs/stamping/ for details. - - The jq `//` default operator is useful for returning an alternative value for unstamped builds. - - For example, if you use the expression `($stamp.BUILD_EMBED_LABEL // "0.0.0")`, this resolves to - "0.0.0" if stamping is not enabled. When built with `--stamp --embed_label=1.2.3` it will - resolve to `1.2.3`. - - Args: - name: name of the resulting jq target. - remote_tags: list of jq expressions which result in a string value, see docs above - **kwargs: additional named parameters to the jq rule. - """ - if not types.is_list(remote_tags): - fail("remote_tags should be a list") - _maybe_quote = lambda x: x if "\"" in x else "\"{}\"".format(x) - jq( - name = name, - srcs = [], - out = "_{}.tags.txt".format(name), - args = ["--raw-output"], - filter = "|".join([ - "$ARGS.named.STAMP as $stamp", - ",".join([_maybe_quote(t) for t in remote_tags]), - ]), - **kwargs - ) diff --git a/oci/dependencies.bzl b/oci/dependencies.bzl index 34db3a62..5626323b 100644 --- a/oci/dependencies.bzl +++ b/oci/dependencies.bzl @@ -29,7 +29,7 @@ def rules_oci_dependencies(): http_archive( name = "aspect_bazel_lib", - sha256 = "d2a71e1e39961535db2f9164c7a588cff1a86938564634532062ed3a3cf307b3", - strip_prefix = "bazel-lib-1.25.0", - url = "https://github.com/aspect-build/bazel-lib/archive/refs/tags/v1.25.0.tar.gz", + sha256 = "f1c181b910f821072f38ee45bb87db6b56275458c7f832c54c54ba6334119eca", + strip_prefix = "bazel-lib-1.32.0", + url = "https://github.com/aspect-build/bazel-lib/releases/download/v1.32.0/bazel-lib-v1.32.0.tar.gz", ) diff --git a/oci/private/push.bzl b/oci/private/push.bzl index f01b97d8..a6f68194 100644 --- a/oci/private/push.bzl +++ b/oci/private/push.bzl @@ -77,10 +77,22 @@ oci_image_index( ] ) -# This is defined in our /examples/push -stamp_tags( +write_file( + name = "tags_tmpl", + out = "tags.txt.tmpl", + content = [ + "BUILD_VERSION", + ], +) + +# Use the value of --embed_label under --stamp, otherwise use a deterministic constant +# value to ensure cache hits for actions that depend on this. +expand_template( name = "stamped", - remote_tags = [\"\"\"($stamp.BUILD_EMBED_LABEL // "0.0.0")\"\"\"], + out = "_stamped.tags.txt", + template = "tags_tmpl", + substitutions = {"BUILD_VERSION": "0.0.0"}, + stamp_substitutions = {"BUILD_VERSION": "{{BUILD_EMBED_LABEL}}"}, ) oci_push(