From ba8d2c3250e3fe65a2d1de0ad3156cf53384818e Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Tue, 26 Mar 2024 16:45:30 -0700 Subject: [PATCH] docs: simplify use of expand_template Also include the aspect_bazel_lib load statement since the bazel_skylib one is broken. Fixes #512 --- docs/push.md | 15 ++++----------- examples/env/BUILD.bazel | 12 ++---------- examples/labels/BUILD.bazel | 18 +++++------------- examples/push/BUILD.bazel | 18 +++++------------- oci/private/push.bzl | 15 ++++----------- 5 files changed, 20 insertions(+), 58 deletions(-) diff --git a/docs/push.md b/docs/push.md index 564461c8..be384019 100644 --- a/docs/push.md +++ b/docs/push.md @@ -76,6 +76,8 @@ oci_push( Push a multi-architecture image to github container registry with a semver tag ```starlark +load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template_rule") + oci_image(name = "app_linux_arm64") oci_image(name = "app_linux_amd64") @@ -91,22 +93,13 @@ oci_image_index( ] ) -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", out = "_stamped.tags.txt", - template = "tags_tmpl", - substitutions = {"BUILD_VERSION": "0.0.0"}, - stamp_substitutions = {"BUILD_VERSION": "{{BUILD_EMBED_LABEL}}"}, + template = ["0.0.0"], + stamp_substitutions = {"0.0.0": "{{BUILD_EMBED_LABEL}}"}, ) oci_push( diff --git a/examples/env/BUILD.bazel b/examples/env/BUILD.bazel index f09ca7b5..4756170b 100644 --- a/examples/env/BUILD.bazel +++ b/examples/env/BUILD.bazel @@ -1,5 +1,4 @@ load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template") -load("@bazel_skylib//rules:write_file.bzl", "write_file") load("@container_structure_test//:defs.bzl", "container_structure_test") load("@rules_pkg//pkg:tar.bzl", "pkg_tar") load("//oci:defs.bzl", "oci_image") @@ -9,20 +8,13 @@ pkg_tar( srcs = ["test.bash"], ) -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", out = "env.txt", - stamp_substitutions = {"EMBED_LABEL": "{{BUILD_EMBED_LABEL}}"}, - substitutions = {"EMBED_LABEL": "1.2.3"}, - template = "env_tmpl", + stamp_substitutions = {"1.2.3": "{{BUILD_EMBED_LABEL}}"}, + template = ["BUILD_VERSION=1.2.3"], ) oci_image( diff --git a/examples/labels/BUILD.bazel b/examples/labels/BUILD.bazel index 080ad0a5..86781eef 100644 --- a/examples/labels/BUILD.bazel +++ b/examples/labels/BUILD.bazel @@ -1,5 +1,4 @@ load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template") -load("@bazel_skylib//rules:write_file.bzl", "write_file") load("@container_structure_test//:defs.bzl", "container_structure_test") load("@rules_pkg//pkg:tar.bzl", "pkg_tar") load("//oci:defs.bzl", "oci_image") @@ -9,23 +8,16 @@ pkg_tar( srcs = ["test.bash"], ) -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", out = "labels.txt", - stamp_substitutions = {"BUILD_VERSION": "{{BUILD_EMBED_LABEL}}"}, - substitutions = {"BUILD_VERSION": "0.0.0"}, - template = "labels_tmpl", + stamp_substitutions = {"0.0.0": "{{BUILD_EMBED_LABEL}}"}, + template = [ + "org.opencontainers.image.version=0.0.0", + "org.opencontainers.image.source=https://github.com/bazel-contrib/rules_oci", + ], ) oci_image( diff --git a/examples/push/BUILD.bazel b/examples/push/BUILD.bazel index 5e1eb8d5..88b4412e 100644 --- a/examples/push/BUILD.bazel +++ b/examples/push/BUILD.bazel @@ -1,5 +1,4 @@ load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template") -load("@bazel_skylib//rules:write_file.bzl", "write_file") load("//oci:defs.bzl", "oci_image", "oci_image_index", "oci_push") oci_image( @@ -36,23 +35,16 @@ oci_image_index( ], ) -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", + stamp_substitutions = {"0.0.0": "{{BUILD_EMBED_LABEL}}"}, + template = [ + "0.0.0", + "nightly", + ], ) oci_push( diff --git a/oci/private/push.bzl b/oci/private/push.bzl index a6f68194..fbf92c30 100644 --- a/oci/private/push.bzl +++ b/oci/private/push.bzl @@ -62,6 +62,8 @@ oci_push( Push a multi-architecture image to github container registry with a semver tag ```starlark +load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template_rule") + oci_image(name = "app_linux_arm64") oci_image(name = "app_linux_amd64") @@ -77,22 +79,13 @@ oci_image_index( ] ) -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", out = "_stamped.tags.txt", - template = "tags_tmpl", - substitutions = {"BUILD_VERSION": "0.0.0"}, - stamp_substitutions = {"BUILD_VERSION": "{{BUILD_EMBED_LABEL}}"}, + template = ["0.0.0"], + stamp_substitutions = {"0.0.0": "{{BUILD_EMBED_LABEL}}"}, ) oci_push(