Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
38 changes: 38 additions & 0 deletions .github/workflows/test-bake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,41 @@ jobs:

- name: Run
run: test "$(docker run --rm ghcr.io/specsnl/github-actions/e2e-cli:local)" = "debian ci"

reproducible:
name: Reproducible
runs-on: ubuntu-26.04
timeout-minutes: 15
permissions:
contents: read
steps:

- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1

- name: Build twice without cache and compare the digests
env:
BUILDX_NO_DEFAULT_ATTESTATIONS: 1
run: |
SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct)
CREATED=$(TZ=UTC printf '%(%Y-%m-%dT%H:%M:%SZ)T' "$SOURCE_DATE_EPOCH")
export SOURCE_DATE_EPOCH CREATED

for build in 1 2; do
docker buildx bake --no-cache --progress=quiet \
-f bake/specs.hcl -f bake/test/e2e/docker-bake.hcl cli-debian \
--set '*.platform=linux/amd64' \
--set '*.output=type=image,name=e2e-repro,push=false,oci-mediatypes=true,rewrite-timestamp=true' \
--metadata-file "$RUNNER_TEMP/build-$build.json"
jq -r '."cli-debian"["containerimage.digest"]' "$RUNNER_TEMP/build-$build.json" | tee "$RUNNER_TEMP/digest-$build"
done

if ! cmp -s "$RUNNER_TEMP/digest-1" "$RUNNER_TEMP/digest-2"; then
echo "::error::Two builds of the same commit produced different images."
exit 1
fi
7 changes: 7 additions & 0 deletions bake/plan/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ runs:
using: composite
steps:

- name: Read the commit time
id: epoch
shell: bash
run: echo "value=$(git log -1 --pretty=%ct)" >> "$GITHUB_OUTPUT"

- name: Work out the run's variables
id: vars
shell: bash
env:
SOURCE_DATE_EPOCH: ${{ steps.epoch.outputs.value }}
INPUT_VERSION: ${{ inputs.version }}
INPUT_DISPATCH_TAG: ${{ inputs.dispatch-tag }}
INPUT_PUSH: ${{ inputs.push }}
Expand All @@ -61,6 +67,7 @@ runs:
VERSION: ${{ steps.vars.outputs.version }}
DISPATCH_TAG: ${{ steps.vars.outputs.dispatch-tag }}
CREATED: ${{ steps.vars.outputs.created }}
SOURCE_DATE_EPOCH: ${{ steps.vars.outputs.source-date-epoch }}
EVENT: ${{ github.event_name }}
REF_TYPE: ${{ github.ref_type }}
REF_NAME: ${{ github.ref_name }}
Expand Down
2 changes: 1 addition & 1 deletion bake/plan/definition.jq
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def image: .tags[0] // "" | sub(":[^:/]*$"; "");
| "\(.value | image | gsub("[^A-Za-z0-9]+"; "-"))-\(.key)-\($slug)" as $scope
| .value |= (
(if $push == "true" then
"type=image,name=\(image),push-by-digest=true,name-canonical=true,oci-mediatypes=true,push=true"
"type=image,name=\(image),push-by-digest=true,name-canonical=true,oci-mediatypes=true,rewrite-timestamp=true,push=true"
else
"type=cacheonly"
end) as $output
Expand Down
2 changes: 1 addition & 1 deletion bake/plan/freeze.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ output matrix "$matrix"
{
echo '### Bake definition'
echo
echo "Version \`${VERSION:-}\`, push \`$PUSH\`."
echo "Version \`${VERSION:-}\`, push \`$PUSH\`, \`SOURCE_DATE_EPOCH\` ${SOURCE_DATE_EPOCH:-} (${CREATED:-})."
echo
echo '| Target | Platforms | Tags |'
echo '|--------|-----------|------|'
Expand Down
15 changes: 11 additions & 4 deletions bake/plan/vars.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env bash
# Works out the run's version, dispatch tag and whether to push, from the event and the inputs.
# Works out the run's version, dispatch tag and whether to push, from the event and the inputs, and the created time
# from the commit time.
#
# Reads INPUT_VERSION, INPUT_DISPATCH_TAG, INPUT_PUSH, PR_NUMBER, PR_AUTHOR, PR_HEAD_REPOSITORY and the GITHUB_*
# variables of the event. Sets the outputs version, dispatch-tag, push and created.
# Reads SOURCE_DATE_EPOCH, INPUT_VERSION, INPUT_DISPATCH_TAG, INPUT_PUSH, PR_NUMBER, PR_AUTHOR, PR_HEAD_REPOSITORY and
# the GITHUB_* variables of the event. Sets the outputs version, dispatch-tag, push, source-date-epoch and created.
set -euo pipefail

# shellcheck source=lib.sh
Expand Down Expand Up @@ -50,7 +51,13 @@ if [ "$push" != true ] && [ "$push" != false ]; then
exit 1
fi

if [[ ! ${SOURCE_DATE_EPOCH:-} =~ ^[0-9]+$ ]]; then
echo "::error::Could not read the commit time, got \"${SOURCE_DATE_EPOCH:-}\"."
exit 1
fi

output version "$version"
output dispatch-tag "$dispatch_tag"
output push "$push"
output created "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
output source-date-epoch "$SOURCE_DATE_EPOCH"
output created "$(TZ=UTC printf '%(%Y-%m-%dT%H:%M:%SZ)T' "$SOURCE_DATE_EPOCH")"
17 changes: 14 additions & 3 deletions bake/test/vars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@ cd "$(dirname "$0")/../.."
failed=0

# check <description> <expected outputs> <environment...>
# Leaves out source-date-epoch and created, unless the description starts with "commit time".
check() {
local description="$1" expected="$2"
shift 2

local skip='^(source-date-epoch|created)<<'
[[ $description == 'commit time'* ]] && skip='^(version|dispatch-tag|push)<<'

local output status=0 actual
output=$(mktemp)
env -i PATH="$PATH" GITHUB_OUTPUT="$output" GITHUB_REPOSITORY=specsnl/example "$@" bake/plan/vars.sh > /dev/null 2>&1 || status=$?
env -i PATH="$PATH" GITHUB_OUTPUT="$output" GITHUB_REPOSITORY=specsnl/example SOURCE_DATE_EPOCH=1762689600 "$@" \
bake/plan/vars.sh > /dev/null 2>&1 || status=$?

if [ "$status" -ne 0 ]; then
actual="exit=$status"
else
actual=$(awk '
/^created<</ { getline; getline; next }
actual=$(awk -v skip="$skip" '
$0 ~ skip { getline; getline; next }
/<<EOF_/ { key = substr($0, 1, index($0, "<<") - 1); getline value; printf "%s=%s ", key, value; getline; next }
' "$output" | sed 's/ $//')
fi
Expand Down Expand Up @@ -93,5 +98,11 @@ check 'invalid push' \
check 'event the tag rules do not cover' \
'version= dispatch-tag=latest push=false' \
GITHUB_EVENT_NAME=merge_group GITHUB_REF_TYPE=branch GITHUB_REF_NAME=gh-readonly-queue/main/pr-1
check 'commit time is the source date epoch and the created label' \
'source-date-epoch=1762689600 created=2025-11-09T12:00:00Z' \
GITHUB_EVENT_NAME=push GITHUB_REF_TYPE=tag GITHUB_REF_NAME=v1.2.3
check 'commit time that is not a number' \
'exit=1' \
GITHUB_EVENT_NAME=push GITHUB_REF_TYPE=tag GITHUB_REF_NAME=v1.2.3 SOURCE_DATE_EPOCH=

exit "$failed"
40 changes: 29 additions & 11 deletions docs/bake.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,44 @@ EVENT=push REF_TYPE=tag VERSION=1.2.3 \
docker buildx bake -f .specs.hcl -f docker-bake.hcl --print
```

To build the same image CI builds, bit for bit, set the commit time the way the plan job does (see
[Reproducible images](pipeline.md#reproducible-images)) and write the image with `rewrite-timestamp=true`:

```bash
export SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct)
export CREATED=$(TZ=UTC printf '%(%Y-%m-%dT%H:%M:%SZ)T' "$SOURCE_DATE_EPOCH")
docker buildx bake -f .specs.hcl -f docker-bake.hcl --no-cache \
--set '*.output=type=image,push=false,oci-mediatypes=true,rewrite-timestamp=true'
```

This needs a `docker-container` builder (`docker buildx create --use`): the default `docker` driver unpacks the image
into Docker's image store, and BuildKit refuses `rewrite-timestamp` together with that. `bake.yml` uses a
`docker-container` builder already.

## The variables CI sets

The plan job works these out once per run and passes them to bake as environment variables. Use them in your own HCL
too — `VERSION` is the usual one, for a version build arg.

| Variable | Local default | In CI |
|----------------|---------------|------------------------------------------------------------------------------------------|
| `VERSION` | `dev` | See below |
| `EVENT` | *empty* | The event name: `push`, `pull_request`, `schedule`, `workflow_dispatch`, … |
| `REF_TYPE` | *empty* | `tag` or `branch` |
| `REF_NAME` | *empty* | The tag or branch name |
| `PR_NUMBER` | *empty* | The pull request number |
| `DISPATCH_TAG` | `latest` | The `dispatch-tag` input, lowercased |
| `REVISION` | *empty* | The commit SHA |
| `SOURCE` | *empty* | The repository URL |
| `CREATED` | *empty* | One timestamp for the whole run, so every platform and every target carries the same one |
| Variable | Local default | In CI |
|----------------|---------------|----------------------------------------------------------------------------|
| `VERSION` | `dev` | See below |
| `EVENT` | *empty* | The event name: `push`, `pull_request`, `schedule`, `workflow_dispatch`, … |
| `REF_TYPE` | *empty* | `tag` or `branch` |
| `REF_NAME` | *empty* | The tag or branch name |
| `PR_NUMBER` | *empty* | The pull request number |
| `DISPATCH_TAG` | `latest` | The `dispatch-tag` input, lowercased |
| `REVISION` | *empty* | The commit SHA |
| `SOURCE` | *empty* | The repository URL |
| `CREATED` | *empty* | The commit time as RFC 3339, the same for every platform and every target |

A release is **stable** when it is a tag push whose version has no `-` in it. `specs.hcl` works that out from `EVENT`,
`REF_TYPE` and `VERSION`; only a stable release moves `1.2`, `1` and `latest`.

The plan job also sets `SOURCE_DATE_EPOCH`, the commit time as a Unix timestamp. That is not a `specs.hcl` variable:
bake passes it to every target as the `SOURCE_DATE_EPOCH` build arg by itself, and BuildKit uses it for the image's
timestamps.

`VERSION` is, in this order:

1. The `version` input, if given.
Expand Down
3 changes: 3 additions & 0 deletions docs/migration-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ Check these against your last 3.x release before you merge the migration:
- **A prerelease never moves `latest`**, for every image, not only Go CLIs.
- **`org.opencontainers.image.version` is the version without the `v`**, on a plain image too, and it is the same in
the config labels, the platform manifests, the index and its descriptors.
- **`created` is the commit time**, not the time of the build. It is the same in the labels, the annotations and the
image config, and with the other timestamps fixed the same commit builds the same image. See
[Reproducible images](pipeline.md#reproducible-images).
- **The same label keys, with the same value everywhere.** `specs_labels` sets the keys metadata-action did, except
`licenses`: `title`, `description`, `version`, `revision`, `source`, `url` and `created`. What changes is that the
index, its descriptors, the platform manifests and the config labels now agree: 3.x left `version` empty (or `latest`)
Expand Down
33 changes: 30 additions & 3 deletions docs/pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ plan ──► build linux/amd64 ──┐

The plan job works out everything that depends on the run, once:

- the bake variables — `VERSION`, the event, the ref, the revision and one `CREATED` timestamp (see
- the bake variables — `VERSION`, the event, the ref, the revision and `CREATED` (see
[the variables CI sets](bake.md#the-variables-ci-sets));
- `SOURCE_DATE_EPOCH`, the commit time: `git log -1 --pretty=%ct` (see [Reproducible images](#reproducible-images));
- whether to push (see [below](#when-nothing-is-pushed)).

It then runs `docker buildx bake --print` on the consumer's bake files with those variables set, and **freezes** the
Expand All @@ -21,8 +22,7 @@ that platform, each target's `output` replaced by a push by digest to its first
all go into one artifact.

Every later job builds from that frozen definition instead of evaluating the HCL again. That is what guarantees every
platform and the publish job see identical tags and labels — including `created`, which would otherwise differ per
job.
platform and the publish job see identical tags, labels and build args, `SOURCE_DATE_EPOCH` included.

The plan job also:

Expand All @@ -44,6 +44,8 @@ Each image is pushed **by digest**: no tags, just the manifest. Nothing is tagge
leaves a `:latest` pointing at one architecture. Bake's metadata, which holds the digests, is uploaded as one artifact
per platform.

The image is written with `rewrite-timestamp=true`, see [Reproducible images](#reproducible-images).

Each target gets its own GitHub Actions cache scope, `<image>-<target>-<platform>`, so two images built from the same
Dockerfile and stage no longer overwrite each other's cache.

Expand All @@ -61,6 +63,31 @@ It doesn't check out the repository: it only needs the artifacts.
Require that one check: there is one call per repository, so it covers every image the repository publishes. A caller
sees it as `<calling job> / Image`.

## Reproducible images

Every timestamp in an image comes from the commit, not from the clock, so building the same commit twice gives the
same image — the same digest:

- `SOURCE_DATE_EPOCH` is the commit time, `git log -1 --pretty=%ct`. The plan job reads it once and freezes it in the
definition as a build arg, so every platform uses the same one. BuildKit uses it for the `created` timestamps in the
image config and its history.
- The build writes the image with `rewrite-timestamp=true`, which sets the time of every file the build created or
changed to `SOURCE_DATE_EPOCH`. Without it, every `RUN` leaves the build's own time in the layers, and the digest
changes on every build.
- The `org.opencontainers.image.created` label and annotations are the same commit time, as RFC 3339, not the time of
the run.

`test-bake.yml` checks it: it builds a test image twice without cache and fails when the digests differ. To check an
image of your own, see [Building locally](bake.md#building-locally).

What stays outside the commit:

- Base images and packages. A `FROM` by tag, or an `apt-get install` without versions, can resolve to something newer
on the next build. Pin base images by digest to rule that out.
- Provenance attestations, when a target turns them on: they record the time of the build.
- The BuildKit version. A different version can assemble the same content differently; BuildKit's
`compatibility-version` exporter option pins that, but `bake.yml` doesn't set it.

## Metadata, in every place

The version and the other `org.opencontainers.image.*` values come from `specs_labels` and end up in four places, all
Expand Down
Loading