diff --git a/.github/scripts/set-latest-in-s3.sh b/.github/scripts/set-latest-in-s3.sh index 2447f2e2..e7e66d3f 100755 --- a/.github/scripts/set-latest-in-s3.sh +++ b/.github/scripts/set-latest-in-s3.sh @@ -1,18 +1,22 @@ #!/usr/bin/env bash -# Point releases/analytics-cli/prod/channel.json at an already-published version. This is the -# only mutable object in the prefix; consumers read `latest` from it. +# Point releases/analytics-cli/prod/channel.json and channel-.json at an already-published +# version. These are the only mutable objects in the prefix. Each pointer only moves forward +# (unless ALLOW_DOWNGRADE=true), so a 0.x backport after 1.0.0 moves channel-0.json alone. # # Usage: set-latest-in-s3.sh set -euo pipefail +shopt -s inherit_errexit bucket="${BUCKET:-trunk-releases}" prefix="${PREFIX:-releases/analytics-cli/prod}" +allow_downgrade="${ALLOW_DOWNGRADE:-false}" version="${1:?usage: set-latest-in-s3.sh }" if [[ ! ${version} =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then echo "::error::'${version}' is not a semver version" exit 1 fi +major="${version%%.*}" manifest_key="${prefix}/${version}/manifest.json" if ! output="$(aws s3api head-object --bucket "${bucket}" --key "${manifest_key}" 2>&1)"; then @@ -24,7 +28,50 @@ if ! output="$(aws s3api head-object --bucket "${bucket}" --key "${manifest_key} exit 1 fi -jq -nc --arg v "${version}" '{latest: $v}' | - aws s3 cp - "s3://${bucket}/${prefix}/channel.json" --only-show-errors \ - --content-type "application/json" --cache-control "max-age=60" -echo "https://trunk.io/${prefix}/channel.json now points at ${version}" +current_latest() { + local key="${prefix}/$1" body output + body="$(mktemp)" + if ! output="$(aws s3api get-object --bucket "${bucket}" --key "${key}" "${body}" 2>&1)"; then + rm -f "${body}" + if [[ ${output} == *"NoSuchKey"* || ${output} == *"Not Found"* || ${output} == *"404"* ]]; then + return 0 + fi + echo "::error::get-object s3://${bucket}/${key} failed: ${output}" >&2 + return 1 + fi + jq -r '.latest // empty' "${body}" + rm -f "${body}" +} + +at_or_after() { + [[ -z $2 ]] || [[ "$(printf '%s\n%s\n' "$2" "$1" | sort -V | tail -n1)" == "$1" ]] +} + +move_pointer() { + local name="$1" current + current="$(current_latest "${name}")" + if [[ ${allow_downgrade} != "true" ]] && ! at_or_after "${version}" "${current}"; then + echo "https://trunk.io/${prefix}/${name} stays at ${current}: ${version} is older (set ALLOW_DOWNGRADE=true to move it back)" >&2 + echo kept + return 0 + fi + jq -nc --arg v "${version}" '{latest: $v}' | + aws s3 cp - "s3://${bucket}/${prefix}/${name}" --only-show-errors \ + --content-type "application/json" --cache-control "max-age=60" + echo "https://trunk.io/${prefix}/${name} now points at ${version}" >&2 + echo moved +} + +major_result="$(move_pointer "channel-${major}.json")" +global_result="$(move_pointer "channel.json")" + +if [[ ${major_result} == kept && ${global_result} == kept ]]; then + echo "::warning::${version} moved no pointer; both already point at a newer version" +fi +if [[ -n ${GITHUB_OUTPUT-} ]]; then + if [[ ${global_result} == moved ]]; then + echo "global_moved=true" >>"${GITHUB_OUTPUT}" + else + echo "global_moved=false" >>"${GITHUB_OUTPUT}" + fi +fi diff --git a/.github/workflows/mirror_release_to_s3.yml b/.github/workflows/mirror_release_to_s3.yml index 5825d98d..6d602009 100644 --- a/.github/workflows/mirror_release_to_s3.yml +++ b/.github/workflows/mirror_release_to_s3.yml @@ -53,6 +53,6 @@ jobs: rm -rf "${dir}" done - - name: Point channel.json at GitHub's latest release + - name: Point channel.json and channel-.json at GitHub's latest release if: inputs.set-latest-to-github-latest run: .github/scripts/set-latest-in-s3.sh "$(gh release view --json tagName --jq .tagName)" diff --git a/.github/workflows/promote_release.yml b/.github/workflows/promote_release.yml index 3f9261cb..e1818c79 100644 --- a/.github/workflows/promote_release.yml +++ b/.github/workflows/promote_release.yml @@ -6,8 +6,12 @@ on: # trunk-ignore(checkov/CKV_GHA_7) version: type: string - description: Already-released version to make latest (on GitHub and at trunk.io/releases/analytics-cli/prod/channel.json) + description: Already-released version to make latest for its major (channel-.json), and overall (channel.json and GitHub) when it is the newest required: true + allow-downgrade: + type: boolean + description: Move the pointers even to an older version (a rollback) + default: false concurrency: ${{ github.workflow }} permissions: @@ -22,6 +26,7 @@ jobs: id-token: write env: VERSION: ${{ inputs.version }} + ALLOW_DOWNGRADE: ${{ inputs.allow-downgrade }} GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} steps: - uses: actions/checkout@v4 @@ -64,8 +69,15 @@ jobs: # S3 moves first: its preconditions (the role, a complete manifest) are the ones that fail in # practice, and when they do GitHub has not moved either. Both steps are idempotent, so if the # GitHub edit fails afterwards, re-running this workflow converges the two. - - name: Point channel.json at ${{ inputs.version }} + - name: Point channel.json and channel-.json at ${{ inputs.version }} + id: s3 run: .github/scripts/set-latest-in-s3.sh "${VERSION}" - name: Mark ${{ inputs.version }} latest on GitHub + if: steps.s3.outputs.global_moved == 'true' run: gh release edit "${VERSION}" --prerelease=false --latest + + # A backport of an older major is published but must not take GitHub's latest from the newer one. + - name: Mark ${{ inputs.version }} released on GitHub + if: steps.s3.outputs.global_moved != 'true' + run: gh release edit "${VERSION}" --prerelease=false --latest=false diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2cc6bf24..36a82d66 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -145,12 +145,14 @@ The pattern is: Releases are published in two places: GitHub releases, and `https://trunk.io/releases/analytics-cli/prod/` (the `trunk-releases` S3 bucket). Trunk's own consumers (the `analytics-uploader` action and the `trunk` CLI's `flakytests` command) read the S3 copy. GitHub releases stay, because the frozen legacy `trunk` launcher and customers' own scripts download from them. 1. **Cut**: run the `Release` workflow with the version. It builds, smoke-tests, creates a GitHub **prerelease**, and uploads the same assets to `releases/analytics-cli/prod//`. A version is immutable once published. -2. **Promote**: run the `Promote Release` workflow with the version. It points `releases/analytics-cli/prod/channel.json` at it and marks it latest on GitHub. Do **not** mark a release latest in the GitHub UI, since that leaves `channel.json` behind. +2. **Promote**: run the `Promote Release` workflow with the version. It points `channel-.json` at it. If it is the newest release overall, it also points `channel.json` at it and marks it latest on GitHub. So promoting a 0.x backport after 1.x moves only `channel-0.json`. A pointer never moves back unless you tick `allow-downgrade`, for a rollback. Do **not** mark a release latest in the GitHub UI, since that leaves the pointers behind. 3. **Backfill**: run `Mirror Releases to S3` to copy releases that predate the S3 lane (or that a failed run left incomplete). Pass space-separated tags or `all`. Re-running it is safe because complete versions are skipped. Each download is first checked against the sha256 digest GitHub recorded for it; releases from before June 2025 have no digest and are copied unchecked. Layout under `releases/analytics-cli/prod/`: -- `channel.json`: `{"latest": ""}`, the only mutable object (`Cache-Control: max-age=60`). +- `channel.json`: `{"latest": ""}`, the newest release overall. Read by the `trunk` CLI and the docs. +- `channel-.json`: the same shape, for the newest release of one major version (`channel-0.json`, `channel-1.json`). Each analytics-uploader major reads its own, so `@v2` stays on 0.x. +- Both are written with `Cache-Control: max-age=60` and are the only mutable objects. - `/`: each GitHub release asset under the same filename, e.g. `trunk-analytics-cli-x86_64-unknown-linux.tar.gz`. - `/manifest.json`: `{"version": "", "artifacts": {"": {"sha256": ""}}}`, uploaded last, so its presence means the version is complete.