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

Draft: Implemented stamping created time #407

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 42 additions & 0 deletions examples/created_timestamp/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template")
load("@aspect_bazel_lib//lib:jq.bzl", "jq")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
load("//oci:defs.bzl", "oci_image")

pkg_tar(
name = "app",
srcs = ["app.sh"],
)

write_file(
name = "build_timestamp_tmpl",
out = "build_timestamp.txt.tmpl",
content = [
"BUILD_TIMESTAMP",
],
)

expand_template(
name = "build_timestamp",
out = "build_timestamp.txt",
stamp_substitutions = {"BUILD_TIMESTAMP": "{{BUILD_TIMESTAMP}}"},
substitutions = {"BUILD_TIMESTAMP": "946684800"},
template = ":build_timestamp_tmpl",
alexeagle marked this conversation as resolved.
Show resolved Hide resolved
)

jq(
name = "created_timestamp",
srcs = [":build_timestamp"],
out = "created_timestamp.txt",
args = ["-r"],
filter = "todate",
)

oci_image(
name = "image_timestamp_file",
base = "@ubuntu",
cmd = ["test.sh"],
created_timestamp = ":created_timestamp",
tars = ["app.tar"],
)
1 change: 1 addition & 0 deletions examples/created_timestamp/app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo "hello world!"
4 changes: 3 additions & 1 deletion oci/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ oci_image_rule = _oci_image
oci_image_index = _oci_image_index
oci_push_rule = _oci_push

def oci_image(name, labels = None, annotations = None, env = None, cmd = None, entrypoint = None, **kwargs):
def oci_image(name, labels = None, annotations = None, env = None, cmd = None, entrypoint = None, created_timestamp = None, **kwargs):
"""Macro wrapper around [oci_image_rule](#oci_image_rule).

Allows labels and annotations to be provided as a dictionary, in addition to a text file.
Expand All @@ -43,6 +43,7 @@ def oci_image(name, labels = None, annotations = None, env = None, cmd = None, e
env: Environment variables provisioned by default to the running container. See documentation above.
cmd: Command & argument configured by default in the running container. See documentation above.
entrypoint: Entrypoint configured by default in the running container. See documentation above.
created_timestamp: A file containing the `created` timestamp of the image in ISO8601 format. See documentation above.
**kwargs: other named arguments to [oci_image_rule](#oci_image_rule) and
[common rule attributes](https://bazel.build/reference/be/common-definitions#common-attributes).
"""
Expand Down Expand Up @@ -115,6 +116,7 @@ def oci_image(name, labels = None, annotations = None, env = None, cmd = None, e
env = env,
cmd = cmd,
entrypoint = entrypoint,
created_timestamp = created_timestamp,
**kwargs
)

Expand Down
5 changes: 5 additions & 0 deletions oci/private/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ If `group/gid` is not specified, the default group and supplementary groups of t
"variant": attr.string(doc = "The variant of the specified CPU architecture. eg: `v6`, `v7`, `v8`. See: https://github.com/opencontainers/image-spec/blob/main/image-index.md#platform-variants for more."),
"labels": attr.label(doc = "A file containing a dictionary of labels. Each line should be in the form `name=value`.", allow_single_file = True),
"annotations": attr.label(doc = "A file containing a dictionary of annotations. Each line should be in the form `name=value`.", allow_single_file = True),
"created_timestamp": attr.label(doc = "A file containing the `created` timestamp of the container.", allow_single_file = True),
"_image_sh_tpl": attr.label(default = "image.sh.tpl", allow_single_file = True),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
# Workaround for https://github.com/google/go-containerregistry/issues/1513
Expand Down Expand Up @@ -171,6 +172,10 @@ def _oci_image_impl(ctx):
args.add(ctx.file.annotations.path, format = "--annotations-file=%s")
inputs_depsets.append(depset([ctx.file.annotations]))

if ctx.attr.created_timestamp:
args.add(ctx.file.created_timestamp.path, format = "--created-timestamp-file=%s")
inputs_depsets.append(depset([ctx.file.created_timestamp]))

output = ctx.actions.declare_directory(ctx.label.name)
args.add(output.path, format = "--output=%s")

Expand Down
6 changes: 6 additions & 0 deletions oci/private/image.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ OUTPUT=""
WORKDIR=""
FIXED_ARGS=()
ENV_EXPANSIONS=()
CREATED_TIMESTAMP_FILE=""

for ARG in "$@"; do
case "$ARG" in
Expand Down Expand Up @@ -131,12 +132,17 @@ for ARG in "$@"; do
FIXED_ARGS+=("--entrypoint=$in")
done <"${ARG#--entrypoint-file=}"
;;
(--created-timestamp-file=*) CREATED_TIMESTAMP_FILE="${ARG#--created-timestamp-file=}" ;;
(*) FIXED_ARGS+=( "${ARG}" )
esac
done

REF=$("${CRANE}" "${FIXED_ARGS[@]}")

if [ -n "${CREATED_TIMESTAMP_FILE}" ]; then
REF=$("${CRANE}" config "${REF}" | "${JQ}" ".created = \"$(cat ${CREATED_TIMESTAMP_FILE})"\" | "${CRANE}" edit config "${REF}")
fi

if [ ${#ENV_EXPANSIONS[@]} -ne 0 ]; then
env_expansion_filter=\
'[$raw | match("\\${?([a-zA-Z0-9_]+)}?"; "gm")] | reduce .[] as $match (
Expand Down