Skip to content

Commit

Permalink
refactor: stop using jq for stamping
Browse files Browse the repository at this point in the history
bazel-lib 2.0 has a breaking change for this, and expand_template is simpler
  • Loading branch information
alexeagle committed Sep 20, 2023
1 parent 828b737 commit 51f7fe0
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 91 deletions.
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
18 changes: 15 additions & 3 deletions docs/push.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion e2e/smoke/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
31 changes: 14 additions & 17 deletions examples/env/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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"],
)

# 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 = {"BUILD_VERSION": "{{BUILD_EMBED_LABEL}}"},
substitutions = {"BUILD_VERSION": "1.2.3"},
template = "env_tmpl",
)

oci_image(
Expand Down
35 changes: 17 additions & 18 deletions examples/labels/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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(
Expand Down
23 changes: 17 additions & 6 deletions examples/push/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
39 changes: 0 additions & 39 deletions examples/push/stamp_tags.bzl

This file was deleted.

6 changes: 3 additions & 3 deletions oci/dependencies.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
18 changes: 15 additions & 3 deletions oci/private/push.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 51f7fe0

Please sign in to comment.