From e5f27dd9ccde90a67fc78a4662cf23c5c39b763e Mon Sep 17 00:00:00 2001 From: Ilyes512 Date: Mon, 9 Nov 2026 14:50:28 +0100 Subject: [PATCH] Build reproducible images: SOURCE_DATE_EPOCH from the commit time The plan job reads the commit time with git log -1 --pretty=%ct and freezes it in the definition as the SOURCE_DATE_EPOCH build arg, so every platform builds with 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 also sets the time of every file the build created to SOURCE_DATE_EPOCH; without it every RUN leaves the build's own time in the layers. CREATED, and with it the org.opencontainers.image.created label and annotations, is now that commit time instead of the time of the run, so the same commit builds the same image. test-bake.yml gets a Reproducible job that builds a test image twice without cache and fails when the digests differ. --- .github/workflows/test-bake.yml | 38 +++++++++++++++++++++++++++++++ bake/plan/action.yml | 7 ++++++ bake/plan/definition.jq | 2 +- bake/plan/freeze.sh | 2 +- bake/plan/vars.sh | 15 +++++++++---- bake/test/vars.sh | 17 +++++++++++--- docs/bake.md | 40 ++++++++++++++++++++++++--------- docs/migration-4.0.md | 3 +++ docs/pipeline.md | 33 ++++++++++++++++++++++++--- 9 files changed, 134 insertions(+), 23 deletions(-) diff --git a/.github/workflows/test-bake.yml b/.github/workflows/test-bake.yml index 156217a..dd21789 100644 --- a/.github/workflows/test-bake.yml +++ b/.github/workflows/test-bake.yml @@ -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 diff --git a/bake/plan/action.yml b/bake/plan/action.yml index 5b1ce79..3e1927d 100644 --- a/bake/plan/action.yml +++ b/bake/plan/action.yml @@ -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 }} @@ -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 }} diff --git a/bake/plan/definition.jq b/bake/plan/definition.jq index 4b471ca..2828df6 100644 --- a/bake/plan/definition.jq +++ b/bake/plan/definition.jq @@ -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 diff --git a/bake/plan/freeze.sh b/bake/plan/freeze.sh index be0ea4d..3be62f7 100755 --- a/bake/plan/freeze.sh +++ b/bake/plan/freeze.sh @@ -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 '|--------|-----------|------|' diff --git a/bake/plan/vars.sh b/bake/plan/vars.sh index da64151..1045e08 100755 --- a/bake/plan/vars.sh +++ b/bake/plan/vars.sh @@ -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 @@ -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")" diff --git a/bake/test/vars.sh b/bake/test/vars.sh index 7e3536c..2e84e4d 100755 --- a/bake/test/vars.sh +++ b/bake/test/vars.sh @@ -7,19 +7,24 @@ cd "$(dirname "$0")/../.." failed=0 # check +# 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<--`, so two images built from the same Dockerfile and stage no longer overwrite each other's cache. @@ -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 ` / 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