From 028a4a129315f4f04ffafa1ab65b825fddc3b158 Mon Sep 17 00:00:00 2001 From: Mihir Rawool Date: Thu, 3 Dec 2026 15:01:33 +0530 Subject: [PATCH] Publish a GitHub release per tag with the binary's build records People who use the k6 binary over a network never receive it, so SOURCE.md could not ask them to read build-info.json or run `go version -m` inside the image. Each release now publishes those records instead. - builder.Dockerfile: the reference build, moved out of SOURCE.md into a real file. Besides the binary it writes build-info-.json (tag, commit, toolkitDir, k6Version, arch, sha256) and go-version-m-.txt; only those records leave the build (the binary itself is not published). - release.yml: builds both architectures with it and creates the GitHub release for a tag (generated notes, a SOURCE.md link) with the records and their SHA256SUMS. tag-main.yml calls it after tagging, since a tag pushed with GITHUB_TOKEN starts no other workflow; it can also be run by hand to publish an existing tag. - tag-release.sh hands the tag to the next job as a step output. - SOURCE.md identifies a binary by its release tag and points at the release assets for the version, the transitive modules and the rebuild recipe, which now takes toolkitDir from build-info (Go records the checkout directory in the binary). - check-source-md.sh also fails when builder.Dockerfile's Go image or xk6 version drift from SOURCE.md or ci.yml. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/release.yml | 73 +++++++++++++++++++++++ .github/workflows/tag-main.yml | 13 ++++ SOURCE.md | 105 +++++++++++++-------------------- builder.Dockerfile | 45 ++++++++++++++ scripts/check-source-md.sh | 19 +++++- scripts/tag-release.sh | 5 ++ scripts/tag-release_test.sh | 7 +++ 7 files changed, 200 insertions(+), 67 deletions(-) create mode 100644 .github/workflows/release.yml create mode 100644 builder.Dockerfile diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..5b6a044 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,73 @@ +name: Release + +# Publishes the GitHub release for a tag: generated release notes, plus the +# build-info and `go version -m` output of the reference build (builder.Dockerfile) +# for each architecture, which SOURCE.md points to. The binary itself is not +# published. Called by tag-main.yml for every new tag; run it by hand to +# (re)publish an existing tag. +on: + workflow_call: + inputs: + tag: + type: string + required: true + workflow_dispatch: + inputs: + tag: + description: "An existing release tag, e.g. v0.1.1" + type: string + required: true + +permissions: + contents: write + +concurrency: + group: release-${{ inputs.tag }} + cancel-in-progress: false + +jobs: + release: + runs-on: ubuntu-latest + env: + TAG: ${{ inputs.tag }} + GH_REPO: ${{ github.repository }} + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + fetch-tags: true + persist-credentials: false + + - name: Resolve the tag + id: tag + run: | + [[ "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo "not a release tag: $TAG" >&2; exit 1; } + commit="$(git rev-parse --verify "refs/tags/${TAG}^{commit}")" + echo "commit=$commit" >> "$GITHUB_OUTPUT" + # Build with the recipe the tag itself carries. Tags made before + # builder.Dockerfile existed use this ref's copy. + mkdir -p "$RUNNER_TEMP/ctx" + git show "${commit}:builder.Dockerfile" > "$RUNNER_TEMP/ctx/builder.Dockerfile" 2>/dev/null \ + || cp builder.Dockerfile "$RUNNER_TEMP/ctx/builder.Dockerfile" + + - name: Reference build, both architectures + env: + COMMIT: ${{ steps.tag.outputs.commit }} + run: | + for arch in amd64 arm64; do + docker build -f "$RUNNER_TEMP/ctx/builder.Dockerfile" --target release-assets \ + --build-arg TOOLKIT_COMMIT="$COMMIT" --build-arg TOOLKIT_TAG="$TAG" --build-arg ARCH="$arch" \ + --output type=local,dest=assets "$RUNNER_TEMP/ctx" + done + (cd assets && sha256sum -- * > SHA256SUMS && cat SHA256SUMS) + jq -r '"\(.arch): \(.sha256) \(.commit) \(.toolkitDir)"' assets/build-info-*.json + + - name: Publish the release + env: + GH_TOKEN: ${{ github.token }} + run: | + if ! gh release view "$TAG" >/dev/null 2>&1; then + gh release create "$TAG" --verify-tag --title "$TAG" --generate-notes \ + --notes "Corresponding source and build recipe: [SOURCE.md at $TAG](https://github.com/${GH_REPO}/blob/${TAG}/SOURCE.md). The attached build-info-.json and go-version-m-.txt describe the binary built from this tag." + fi + gh release upload "$TAG" assets/* --clobber diff --git a/.github/workflows/tag-main.yml b/.github/workflows/tag-main.yml index 646e28a..eeab750 100644 --- a/.github/workflows/tag-main.yml +++ b/.github/workflows/tag-main.yml @@ -21,12 +21,25 @@ jobs: # A manual run from another branch must not tag that branch's commit. if: github.ref == 'refs/heads/main' runs-on: ubuntu-latest + outputs: + tag: ${{ steps.tag.outputs.tag }} steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: fetch-depth: 0 fetch-tags: true - name: Tag this commit + id: tag env: COMMIT: ${{ github.sha }} run: bash scripts/tag-release.sh "$COMMIT" + + # A tag pushed with GITHUB_TOKEN starts no other workflow, so call the release + # here rather than on a tag push. + release: + needs: tag + uses: ./.github/workflows/release.yml + with: + tag: ${{ needs.tag.outputs.tag }} + permissions: + contents: write diff --git a/SOURCE.md b/SOURCE.md index 3929007..24a1dbb 100644 --- a/SOURCE.md +++ b/SOURCE.md @@ -13,20 +13,24 @@ Every version pin below lives in this repository (`extensions.json`, this file, was built from** is the source listing for that binary. Every commit on `main` gets a release tag, `vX.Y.Z` -(`.github/workflows/tag-main.yml`). A binary built from `main` is identified by -that tag and by its commit: +(`.github/workflows/tag-main.yml`), and a GitHub release +(`.github/workflows/release.yml`). A binary is identified by its release tag. -- the container image's labels: `org.opencontainers.image.version` (the tag), - `org.opencontainers.image.revision` (the commit) and - `org.opencontainers.image.source` (this repository); -- the record in the image: +People who use the binary over a network never receive it, so each release +publishes what would otherwise only be readable from the binary. For each +architecture (`amd64`, `arm64`), the release attaches the output of the reference +build ([`builder.Dockerfile`](builder.Dockerfile)) of that tag: - ```sh - cat /opt/xk6-load-toolkit/build-info.json # "commit", "arch", "sha256" of the binary - ``` +| Asset | Contents | +|---|---| +| `build-info-.json` | `tag`, `commit`, `toolkitDir` (the directory the build checked this repository out to), `k6Version`, `arch`, and the binary's `sha256` | +| `go-version-m-.txt` | `go version -m` of the binary: the Go toolchain, the build settings, and every linked module with its version and `h1:` checksum | +| `SHA256SUMS` | The checksums of the files above | -Read this file at that tag (or commit): -`https://github.com/browserstack/xk6-load-toolkit/blob//SOURCE.md`. +Releases: . For tag +``, read this file at +`https://github.com/browserstack/xk6-load-toolkit/blob//SOURCE.md` and the +assets at `https://github.com/browserstack/xk6-load-toolkit/releases/tag/`. ## Linked components @@ -35,7 +39,7 @@ Read this file at that tag (or commit): | Component | Go module | Version | Commit | Licence | Source | |---|---|---|---|---|---| | k6 | `go.k6.io/k6/v2` | v2.2.0 | `00a9a1b7f552d6bb4337278b10ae25aac0f4e666` | AGPL-3.0 | | -| xk6-load-toolkit | `github.com/browserstack/xk6-load-toolkit` | the image's `org.opencontainers.image.version` label | the `commit` in `build-info.json` (also the `org.opencontainers.image.revision` label) | AGPL-3.0 | > | +| xk6-load-toolkit | `github.com/browserstack/xk6-load-toolkit` | the release tag, `vX.Y.Z` | the `commit` in the release's `build-info-.json` | AGPL-3.0 | > | ### Third-party extensions (15, pinned in `extensions.json`) @@ -65,11 +69,9 @@ version. Every other module linked into the binary is resolved by Go's minimal version selection from the `go.mod` files of the components above, so it is fixed by -them. The binary records the full list with versions and checksums: - -```sh -go version -m k6 # every linked module, its version and its h1: checksum -``` +them. The binary records the full list with versions and checksums, and each +release publishes it as `go-version-m-.txt` (the output of +`go version -m k6`). Each listed module's source is available from the Go module proxy at that version (`https://proxy.golang.org//@v/.zip`) and is under its @@ -96,63 +98,38 @@ All in this repository at the same commit: | [`extensions.json`](extensions.json) | The manifest: the k6 version and every extension with its pinned version. | | [`build.sh`](build.sh) | Turns the manifest into `xk6 build --k6-version … --with @ …` and runs it with CGO off. | | [`scripts/release-gate.sh`](scripts/release-gate.sh) | Reads the binary's `go version -m` output and fails unless every pin above is linked at exactly its version. | -| This file | The builder stage below, the toolchain pins and the component list. | - -### Builder stage - -The container stage the binary is built in. The `golang` image digest and the -xk6 version are the pins listed under [Build tools](#build-tools). - -```dockerfile -FROM --platform=linux/amd64 golang:1.26.11-bookworm@sha256:dc40a13b61304647ed7e5d581d246b86413dfc29d63c6d018310feb93cd98fc0 AS k6-builder - -RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates git jq && \ - rm -rf /var/lib/apt/lists/* - -RUN go install go.k6.io/xk6/cmd/xk6@v1.4.12 && ln -sf /go/bin/xk6 /usr/local/bin/xk6 - -# The commit from build-info.json, and the directory the original build checked -# this repository out to (see "Rebuilding the exact binary" below). -ARG TOOLKIT_COMMIT -ARG TOOLKIT_DIR=/src/xk6-load-toolkit -ARG TARGETARCH=amd64 - -RUN git clone https://github.com/browserstack/xk6-load-toolkit.git "$TOOLKIT_DIR" && \ - git -C "$TOOLKIT_DIR" checkout --detach "$TOOLKIT_COMMIT" - -WORKDIR $TOOLKIT_DIR -RUN GOOS=linux GOARCH="$TARGETARCH" CGO_ENABLED=0 ./build.sh --output /out/k6 && \ - scripts/release-gate.sh --binary /out/k6 --arch "$TARGETARCH" -``` +| [`builder.Dockerfile`](builder.Dockerfile) | The reference build: the pinned Go image and xk6, the checkout of a given commit into a given directory, `build.sh`, the release gate, and the `build-info-.json` and `go-version-m-.txt` records. | +| [`.github/workflows/release.yml`](.github/workflows/release.yml) | Runs `builder.Dockerfile` for both architectures at each release tag and attaches its records to the GitHub release. | +| This file | The toolchain pins and the component list. | ## Rebuilding the exact binary -1. Read `commit`, `arch` and `sha256` from `/opt/xk6-load-toolkit/build-info.json` - in the image, and copy the binary out of it. -2. Read the directory the original build checked this repository out to. Go - records it in the binary, and it is part of the binary's contents: - - ```sh - go version -m k6 | grep -A1 'xk6-load-toolkit' # the "=>" line is the directory - ``` - -3. Build with the stage above, passing those values: +1. From the release of the tag, download `build-info-.json` and + `go-version-m-.txt`. Note its `commit`, `toolkitDir` and `sha256`. + `toolkitDir` matters: Go records the directory this repository was checked + out to in the binary (the `=>` line under `xk6-load-toolkit` in + `go-version-m-.txt`), so it is part of the binary's contents. +2. Build with [`builder.Dockerfile`](builder.Dockerfile) at that tag, passing + those values: ```sh - docker build --target k6-builder \ + docker build -f builder.Dockerfile --target release-assets \ --build-arg TOOLKIT_COMMIT= \ - --build-arg TOOLKIT_DIR= \ - --build-arg TARGETARCH= \ - -t k6-rebuild -f . - docker run --rm k6-rebuild sha256sum /out/k6 + --build-arg TOOLKIT_DIR= \ + --build-arg ARCH= \ + --output type=local,dest=rebuild . ``` -4. Compare. The `sha256` should equal the one in `build-info.json`, and - `go version -m` of the two binaries should match line for line. If the hashes - differ, the `go version -m` diff names the input that differs. +3. Compare. The `sha256` in `rebuild/build-info-.json` should equal the + release's, and `rebuild/go-version-m-.txt` should match the release's + line for line. If the hashes differ, the `go version -m` diff names the input + that differs. To keep the binary itself, build `--target k6-builder` and copy + `/out/k6` out of that image. ## Keeping this file current A change to `extensions.json`, to the Go image or `go-version`, or to the xk6 version must update this file in the same commit, so that this file at any -commit describes the binaries built from that commit. +commit describes the binaries built from that commit. `builder.Dockerfile` +carries the same Go image and xk6 pins; `scripts/check-source-md.sh` fails when +any of them disagree. diff --git a/builder.Dockerfile b/builder.Dockerfile new file mode 100644 index 0000000..6d94237 --- /dev/null +++ b/builder.Dockerfile @@ -0,0 +1,45 @@ +# builder.Dockerfile — the reference build of the k6 binary (see SOURCE.md). +# +# The release workflow builds every release with this file and attaches the +# build-info and `go version -m` output it produces. A rebuild with the same +# TOOLKIT_COMMIT and TOOLKIT_DIR gives the same binary, byte for byte: +# +# docker build -f builder.Dockerfile --target release-assets \ +# --build-arg TOOLKIT_COMMIT= --build-arg TOOLKIT_DIR= \ +# --build-arg ARCH=amd64|arm64 --output type=local,dest=out . +# +# The Go image digest and the xk6 version are the pins SOURCE.md lists under +# "Build tools"; scripts/check-source-md.sh fails when they disagree. +FROM --platform=linux/amd64 golang:1.26.11-bookworm@sha256:dc40a13b61304647ed7e5d581d246b86413dfc29d63c6d018310feb93cd98fc0 AS k6-builder + +RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates git jq && \ + rm -rf /var/lib/apt/lists/* + +RUN go install go.k6.io/xk6/cmd/xk6@v1.4.12 && ln -sf /go/bin/xk6 /usr/local/bin/xk6 + +# The commit to build, and the directory to check it out to. Go records that +# directory in the binary, so it is part of the binary's contents. +ARG TOOLKIT_COMMIT +ARG TOOLKIT_DIR=/src/xk6-load-toolkit +ARG TOOLKIT_TAG= +ARG ARCH=amd64 + +RUN test -n "$TOOLKIT_COMMIT" || { echo "TOOLKIT_COMMIT is required" >&2; exit 1; }; \ + git clone https://github.com/browserstack/xk6-load-toolkit.git "$TOOLKIT_DIR" && \ + git -C "$TOOLKIT_DIR" checkout --detach "$TOOLKIT_COMMIT" + +WORKDIR $TOOLKIT_DIR +RUN GOOS=linux GOARCH="$ARCH" CGO_ENABLED=0 ./build.sh --output /out/k6 && \ + scripts/release-gate.sh --binary /out/k6 --arch "$ARCH" && \ + go version -m /out/k6 > "/out/go-version-m-$ARCH.txt" && \ + jq -n --arg tag "$TOOLKIT_TAG" --arg commit "$(git rev-parse HEAD)" --arg toolkitDir "$TOOLKIT_DIR" \ + --arg k6Version "$(jq -r .k6Version extensions.json)" --arg arch "$ARCH" \ + --arg sha256 "$(sha256sum /out/k6 | cut -d' ' -f1)" \ + '{repository: "https://github.com/browserstack/xk6-load-toolkit.git", tag: $tag, commit: $commit, + toolkitDir: $toolkitDir, k6Version: $k6Version, arch: $arch, sha256: $sha256}' \ + > "/out/build-info-$ARCH.json" + +# Only the metadata leaves the build: the binary itself is not published. +FROM scratch AS release-assets +ARG ARCH=amd64 +COPY --from=k6-builder /out/build-info-${ARCH}.json /out/go-version-m-${ARCH}.txt / diff --git a/scripts/check-source-md.sh b/scripts/check-source-md.sh index 07f41ef..3499eac 100755 --- a/scripts/check-source-md.sh +++ b/scripts/check-source-md.sh @@ -1,20 +1,24 @@ #!/usr/bin/env bash # check-source-md.sh — fail when SOURCE.md no longer lists what the build uses: -# the manifest's k6 version, every pinned extension at its version, and the Go -# version CI builds with. +# the manifest's k6 version, every pinned extension at its version, the Go +# version CI builds with, and the Go image and xk6 version builder.Dockerfile +# builds the release records with. # -# scripts/check-source-md.sh [--manifest extensions.json] [--source SOURCE.md] [--ci .github/workflows/ci.yml] +# scripts/check-source-md.sh [--manifest extensions.json] [--source SOURCE.md] +# [--ci .github/workflows/ci.yml] [--builder builder.Dockerfile] set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" MANIFEST="$ROOT/extensions.json" SOURCE="$ROOT/SOURCE.md" CI="$ROOT/.github/workflows/ci.yml" +BUILDER="$ROOT/builder.Dockerfile" while [[ $# -gt 0 ]]; do case "$1" in --manifest) MANIFEST="$2"; shift 2 ;; --source) SOURCE="$2"; shift 2 ;; --ci) CI="$2"; shift 2 ;; + --builder) BUILDER="$2"; shift 2 ;; *) echo "check-source-md: unknown argument $1" >&2; exit 2 ;; esac done @@ -40,5 +44,14 @@ go_version="$(sed -nE 's/.*go-version: "([0-9.]+)".*/\1/p' "$CI" | head -1)" [[ -n "$go_version" ]] || fail "no go-version in $CI" grep -qF "go${go_version}," "$SOURCE" || fail "Go toolchain go${go_version} (from $CI) is not listed" +go_image="$(sed -nE 's/^FROM .*(golang:[^ ]+@sha256:[0-9a-f]{64}).*/\1/p' "$BUILDER" | head -1)" +[[ -n "$go_image" ]] || fail "no pinned golang image in $BUILDER" +grep -qF "\`$go_image\`" "$SOURCE" || fail "Go image $go_image (from $BUILDER) is not listed" +[[ "$go_image" == "golang:${go_version}-"* ]] || fail "Go image $go_image (from $BUILDER) is not go${go_version} (from $CI)" + +xk6_version="$(sed -nE 's#.*go\.k6\.io/xk6/cmd/xk6@(v[0-9.]+).*#\1#p' "$BUILDER" | head -1)" +[[ -n "$xk6_version" ]] || fail "no pinned xk6 in $BUILDER" +grep -qE "^\| xk6 \| ${xk6_version//./\\.} " "$SOURCE" || fail "xk6 $xk6_version (from $BUILDER) is not listed" + [[ "$errors" -eq 0 ]] || exit 1 echo "check-source-md: SOURCE.md matches the manifest and go${go_version}" diff --git a/scripts/tag-release.sh b/scripts/tag-release.sh index 85628eb..917f81b 100755 --- a/scripts/tag-release.sh +++ b/scripts/tag-release.sh @@ -30,9 +30,13 @@ if [[ "$PUSH" -eq 1 ]]; then git fetch --quiet --tags "$REMOTE" fi +# In a GitHub Actions step, hand the tag to later steps as the `tag` output. +emit() { if [[ -n "${GITHUB_OUTPUT:-}" ]]; then echo "tag=$1" >> "$GITHUB_OUTPUT"; fi; } + existing="$(git tag --points-at "$sha" --list "${SERIES}.*" | grep -E "^${SERIES//./\\.}\.[0-9]+$" | head -1 || true)" if [[ -n "$existing" ]]; then echo "tag-release: $sha is already $existing" + emit "$existing" exit 0 fi @@ -44,3 +48,4 @@ if [[ "$PUSH" -eq 1 ]]; then git push --quiet "$REMOTE" "refs/tags/${next}" fi echo "tag-release: tagged $sha as $next" +emit "$next" diff --git a/scripts/tag-release_test.sh b/scripts/tag-release_test.sh index 9323020..655aee6 100755 --- a/scripts/tag-release_test.sh +++ b/scripts/tag-release_test.sh @@ -35,6 +35,13 @@ git tag v0.2.0 "$c1" c3="$(commit three)" check "numeric, series-only max" "tag-release: tagged $c3 as v0.1.10" "$(bash "$SCRIPT" "$c3" --no-push)" +gh_out="$WORK/github_output" +c4="$(commit four)" +GITHUB_OUTPUT="$gh_out" bash "$SCRIPT" "$c4" --no-push >/dev/null +check "new tag is a step output" "tag=v0.1.11" "$(tail -1 "$gh_out")" +GITHUB_OUTPUT="$gh_out" bash "$SCRIPT" "$c4" --no-push >/dev/null +check "existing tag is a step output" "tag=v0.1.11" "$(tail -1 "$gh_out")" + check "rejects a non-commit" "1" "$(bash "$SCRIPT" deadbeef >/dev/null 2>&1; echo $?)" check "rejects a bad series" "2" "$(bash "$SCRIPT" "$c3" --series 0.1 >/dev/null 2>&1; echo $?)"