ci(publish): bind Lambda release retries to immutable run artifacts (#7920)

* ci(publish): reuse Lambda assets only from a release this run created (SECOPS-1217)

The pack job downloaded any Lambda handler zip or video image already on the release and pinned it
into @hyperframes/aws-lambda without checking where it came from, so anyone with contents write
could pre-stage both before a stable release.

A new step fails the run when the release exists but github-actions[bot] did not create it during
an earlier attempt of this run, or when a Lambda asset on it was uploaded by anyone else. The pin
steps reuse an asset only when that step allows it.

* fix: bind release retries to immutable run artifacts

* fix: fail release digest pipelines before side effects
This commit is contained in:
Xuanru Li
2026-11-16 04:49:58 +00:00
committed by GitHub
parent 2be03408db
commit 8e8eeb2059
6 changed files with 538 additions and 284 deletions
+179 -79
View File
@@ -2,6 +2,11 @@ name: Publish to npm
permissions: {}
# Explicit bash enables pipefail, including digest-generation pipelines.
defaults:
run:
shell: bash
on:
push:
tags:
@@ -21,6 +26,10 @@ env:
@hyperframes/player @hyperframes/producer @hyperframes/shader-transitions
@hyperframes/studio @hyperframes/aws-lambda @hyperframes/gcp-cloud-run hyperframes
concurrency:
group: publish-${{ github.event.pull_request.head.ref || github.ref }}
cancel-in-progress: false
jobs:
lambda-arm64-ffmpeg:
name: Lambda arm64 ffmpeg
@@ -59,6 +68,7 @@ jobs:
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
with:
name: hf-media-linux-aarch64
overwrite: true
path: out/hf-media-linux-aarch64.tar.gz
if-no-files-found: error
@@ -110,11 +120,14 @@ jobs:
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
with:
name: hyperframes-lambda-handler-zip
overwrite: true
path: packages/aws-lambda/dist/handler.zip
if-no-files-found: error
pack:
name: Build and pack
outputs:
bundle_id: ${{ steps.previous_bundle.outputs.artifact || steps.bundle.outputs.artifact-id }}
needs: [lambda-arm64-ffmpeg, lambda-handler-zip]
runs-on: ubuntu-latest
timeout-minutes: 30
@@ -122,6 +135,7 @@ jobs:
# read-only token and no secrets. The publish job gets only what this job packs.
permissions:
contents: read
actions: read
env:
EXPECTED_RELEASE_SHA: >-
${{ github.event_name == 'pull_request'
@@ -150,7 +164,27 @@ jobs:
exit 1
fi
- name: Find previous release bundle
id: previous_bundle
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
RUN_ID: ${{ github.run_id }}
run: |
gh api --paginate "repos/$REPO/actions/runs/$RUN_ID/artifacts?per_page=100" > "$RUNNER_TEMP/previous_bundle-pages.json"
FOUND="$(jq -sce '[.[].artifacts[] | select(.name == "release-bundle")] | if length == 0 then "" elif length == 1 and .[0].expired == false then .[0].id | tostring else error("Missing valid unique release-bundle") end' "$RUNNER_TEMP/previous_bundle-pages.json")"
echo "artifact=${FOUND}" >> "$GITHUB_OUTPUT"
- name: Restore previous release bundle
if: steps.previous_bundle.outputs.artifact != ''
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
with:
artifact-ids: ${{ steps.previous_bundle.outputs.artifact }}
path: release-bundle
merge-multiple: true
- name: Resolve version
if: steps.previous_bundle.outputs.artifact == ''
id: version
env:
EVENT_NAME: ${{ github.event_name }}
@@ -176,6 +210,7 @@ jobs:
echo "Resolved version=${VERSION} dist_tag=${DIST_TAG}"
- name: Validate release channel
if: steps.previous_bundle.outputs.artifact == ''
env:
VERSION: ${{ steps.version.outputs.version }}
DIST_TAG: ${{ steps.version.outputs.dist_tag }}
@@ -185,25 +220,32 @@ jobs:
# The release installs the bun it downloads, never one restored from the shared cache.
- uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2
if: steps.previous_bundle.outputs.artifact == ''
with:
no-cache: true
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
if: steps.previous_bundle.outputs.artifact == ''
with:
node-version: 24
- run: corepack enable
if: steps.previous_bundle.outputs.artifact == ''
- run: corepack prepare pnpm@10.17.1 --activate
if: steps.previous_bundle.outputs.artifact == ''
- run: bun install --frozen-lockfile
if: steps.previous_bundle.outputs.artifact == ''
- run: bun run build
if: steps.previous_bundle.outputs.artifact == ''
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
if: steps.previous_bundle.outputs.artifact == ''
with:
name: hyperframes-lambda-handler-zip
path: dist/lambda-handler-build
# Before packing, so @hyperframes/aws-lambda pins this exact zip. The pin
# is always this run's build; the publish job decides which asset the
# release keeps.
# Before packing, so @hyperframes/aws-lambda pins this exact zip.
- name: Pin Lambda handler zip
if: steps.previous_bundle.outputs.artifact == ''
id: lambda_zip
env:
EXPECTED_SHA256: ${{ needs.lambda-handler-zip.outputs.sha256 }}
run: |
@@ -217,12 +259,15 @@ jobs:
bun packages/aws-lambda/scripts/pin-handler-zip.ts
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
if: steps.previous_bundle.outputs.artifact == ''
with:
name: hf-media-linux-aarch64
path: dist/hf-media
# Built from the zip pinned above. The video stage only copies files, so no emulation is needed.
- name: Build Lambda video image
if: steps.previous_bundle.outputs.artifact == ''
id: lambda_video_image
run: |
bun packages/aws-lambda/scripts/stage-arm64-image.ts --zip=dist/hyperframes-lambda-handler.zip \
--ffmpeg=dist/hf-media/hf-media-linux-aarch64.tar.gz --out=dist/arm64-image
@@ -234,10 +279,12 @@ jobs:
bun packages/aws-lambda/scripts/pin-handler-zip.ts --video-image
- run: bun run verify:packed-manifests
if: steps.previous_bundle.outputs.artifact == ''
# pnpm pack writes the same tarball pnpm publish uploads: publishConfig
# applied and workspace: ranges replaced with versions.
- name: Pack packages
if: steps.previous_bundle.outputs.artifact == ''
run: |
OUT="$PWD/release-bundle/npm"
mkdir -p "$OUT"
@@ -256,9 +303,12 @@ jobs:
(cd packages/cli && npm pack --pack-destination "$OUT")
- name: Build portable agent plugin
if: steps.previous_bundle.outputs.artifact == ''
run: node scripts/package-agent-plugin.mjs
# Keep the original bytes for all retries, including partially uploaded pairs.
- name: Collect release bundle
if: steps.previous_bundle.outputs.artifact == ''
env:
VERSION: ${{ steps.version.outputs.version }}
run: |
@@ -271,12 +321,14 @@ jobs:
fi
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
id: bundle
if: steps.previous_bundle.outputs.artifact == ''
with:
name: release-bundle
path: release-bundle/
if-no-files-found: error
compression-level: 1
overwrite: true
overwrite: false
retention-days: 7
publish:
@@ -293,6 +345,7 @@ jobs:
# pack job's tarballs and uploads them.
permissions:
contents: write
actions: read
id-token: write
env:
EXPECTED_RELEASE_SHA: >-
@@ -415,7 +468,8 @@ jobs:
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
with:
name: release-bundle
artifact-ids: ${{ needs.pack.outputs.bundle_id }}
merge-multiple: true
path: ${{ runner.temp }}/release-bundle
- name: Check packed tarballs
@@ -465,97 +519,146 @@ jobs:
fi
cat "$ORDER"
- name: Check published Lambda pins
env:
VERSION: ${{ steps.version.outputs.version }}
run: |
META="$RUNNER_TEMP/aws-lambda-version.json"
PUBLISHED="$RUNNER_TEMP/aws-lambda-published.tgz"
STATUS="$(curl -sS -o "$META" -w '%{http_code}' "https://registry.npmjs.org/@hyperframes%2faws-lambda/${VERSION}")"
if [ "$STATUS" = "404" ]; then
exit 0
fi
if [ "$STATUS" != "200" ]; then
echo "::error::The npm registry answered $STATUS for @hyperframes/aws-lambda@${VERSION}"
exit 1
fi
curl -fsSL -o "$PUBLISHED" "$(jq -r '.dist.tarball' "$META")"
INTEGRITY="sha512-$(openssl dgst -sha512 -binary "$PUBLISHED" | base64 -w0)"
if [ "$INTEGRITY" != "$(jq -r '.dist.integrity' "$META")" ]; then
echo "::error::The @hyperframes/aws-lambda@${VERSION} tarball does not match its npm integrity"
exit 1
fi
check_pin() {
local asset="$1" manifest="$2" pinned built
pinned="$(tar -xzOf "$PUBLISHED" "package/prebuilt/$manifest" | jq -r '.sha256')"
built="$(sha256sum "$RUNNER_TEMP/release-bundle/assets/$asset" | cut -d' ' -f1)"
if [ "$pinned" != "$built" ]; then
echo "::error::@hyperframes/aws-lambda@${VERSION} on npm pins $asset at $pinned, but this run's original bundle has $built"
exit 1
fi
}
check_pin hyperframes-lambda-handler.zip handler-zip.json
check_pin hyperframes-lambda-video-arm64.oci.tar video-image.json
- name: Find previous release receipt
id: previous_receipt
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
RUN_ID: ${{ github.run_id }}
run: |
gh api --paginate "repos/$REPO/actions/runs/$RUN_ID/artifacts?per_page=100" > "$RUNNER_TEMP/previous_receipt-pages.json"
FOUND="$(jq -sce '[.[].artifacts[] | select(.name == "lambda-release-receipt")] | if length == 0 then "" elif length == 1 and .[0].expired == false then .[0].id | tostring else error("Missing valid unique lambda-release-receipt") end' "$RUNNER_TEMP/previous_receipt-pages.json")"
echo "artifact=${FOUND}" >> "$GITHUB_OUTPUT"
- name: Restore previous release receipt
if: steps.previous_receipt.outputs.artifact != ''
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
with:
artifact-ids: ${{ steps.previous_receipt.outputs.artifact }}
path: ${{ runner.temp }}/release-receipt
merge-multiple: true
# This receipt is stored in this run's immutable Actions artifact, never on the mutable release.
- name: Create GitHub Release
id: release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VERSION: ${{ steps.version.outputs.version }}
PRERELEASE: ${{ steps.version.outputs.prerelease }}
REPO: ${{ github.repository }}
RUN_ID: ${{ github.run_id }}
run: |
NOTES_FILE="$RUNNER_TEMP/release-bundle/notes.md"
# Skip if release already exists (idempotent re-runs)
if gh release view "v${VERSION}" --repo "$REPO" >/dev/null 2>&1; then
echo "Release v${VERSION} already exists — skipping"
RECEIPT="$RUNNER_TEMP/release-receipt/receipt.json"
ASSETS="$RUNNER_TEMP/release-bundle/assets"
mkdir -p "$RUNNER_TEMP/release-receipt"
DIGESTS="$RUNNER_TEMP/lambda-digests.json"
for name in hyperframes-lambda-handler.zip hyperframes-lambda-handler.sha256 hyperframes-lambda-video-arm64.oci.tar hyperframes-lambda-video-arm64.sha256; do
digest="$(sha256sum "$ASSETS/$name" | cut -d' ' -f1)"
jq -n --arg name "$name" --arg digest "$digest" '{key: $name, value: $digest}'
done | jq -s 'from_entries' > "$DIGESTS"
if [ -f "$RECEIPT" ]; then
jq -e --arg repo "$REPO" --arg run "$RUN_ID" --arg tag "v$VERSION" --arg sha "$EXPECTED_RELEASE_SHA" --slurpfile assets "$DIGESTS" '
.repo == $repo and .run == $run and .tag == $tag and .sha == $sha and
(.release_id | type == "number" and . > 0 and floor == .) and .assets == $assets[0]
' "$RECEIPT" > /dev/null
RELEASE_ID="$(jq -r .release_id "$RECEIPT")"
gh api "repos/$REPO/releases/tags/v$VERSION" > "$RUNNER_TEMP/release.json"
jq -e --argjson id "$RELEASE_ID" '.id == $id' "$RUNNER_TEMP/release.json" > /dev/null
else
FLAGS=(--repo "$REPO" --title "v${VERSION}")
if [ -f "$NOTES_FILE" ]; then
FLAGS+=(--notes-file "$NOTES_FILE")
else
echo "No reviewed release notes found at releases/v${VERSION}.md — using GitHub generated notes"
FLAGS+=(--generate-notes)
# Create-only: an existing release or a concurrent creator fails, never becomes ours.
NOTES_FILE="$RUNNER_TEMP/release-bundle/notes.md"
if [ ! -f "$NOTES_FILE" ]; then
gh api --method POST "repos/$REPO/releases/generate-notes" -f tag_name="v$VERSION" --jq .body > "$RUNNER_TEMP/generated-notes.md"
NOTES_FILE="$RUNNER_TEMP/generated-notes.md"
fi
if [ "$PRERELEASE" = "true" ]; then
FLAGS+=(--prerelease)
fi
gh release create "v${VERSION}" "${FLAGS[@]}"
jq -n --arg tag "v$VERSION" --arg sha "$EXPECTED_RELEASE_SHA" --argjson prerelease "$PRERELEASE" --rawfile body "$NOTES_FILE" \
'{tag_name:$tag, target_commitish:$sha, name:$tag, prerelease:$prerelease, body:$body}' > "$RUNNER_TEMP/create-release.json"
gh api --method POST "repos/$REPO/releases" --input "$RUNNER_TEMP/create-release.json" > "$RUNNER_TEMP/release.json"
RELEASE_ID="$(jq -er '.id | select(type == "number" and . > 0 and floor == .)' "$RUNNER_TEMP/release.json")"
jq -n --arg repo "$REPO" --arg run "$RUN_ID" --arg tag "v$VERSION" --arg sha "$EXPECTED_RELEASE_SHA" --argjson id "$RELEASE_ID" --slurpfile assets "$DIGESTS" \
'{repo:$repo,run:$run,tag:$tag,sha:$sha,release_id:$id,assets:$assets[0]}' > "$RECEIPT"
fi
echo "id=$RELEASE_ID" >> "$GITHUB_OUTPUT"
- name: Save release receipt
if: steps.previous_receipt.outputs.artifact == ''
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
with:
name: lambda-release-receipt
path: ${{ runner.temp }}/release-receipt/receipt.json
if-no-files-found: error
overwrite: false
retention-days: 7
# Users get these assets only through the sha256 pins in
# @hyperframes/aws-lambda. Once that version is on npm, the release must
# keep exactly the pinned files. Before then, an asset this bundle did not
# produce fails a first attempt, because something other than this run
# attached it, and a re-run of this workflow run replaces it.
- name: Attach Lambda assets to release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VERSION: ${{ steps.version.outputs.version }}
REPO: ${{ github.repository }}
RUN_ATTEMPT: ${{ github.run_attempt }}
VERSION: ${{ steps.version.outputs.version }}
RELEASE_ID: ${{ steps.release.outputs.id }}
run: |
ASSETS="$RUNNER_TEMP/release-bundle/assets"
META="$RUNNER_TEMP/aws-lambda-version.json"
PUBLISHED="$RUNNER_TEMP/aws-lambda-published.tgz"
rm -f "$PUBLISHED"
STATUS="$(curl -sS -o "$META" -w '%{http_code}' "https://registry.npmjs.org/@hyperframes%2faws-lambda/${VERSION}")"
if [ "$STATUS" = "200" ]; then
curl -fsSL -o "$PUBLISHED" "$(jq -r '.dist.tarball' "$META")"
INTEGRITY="sha512-$(openssl dgst -sha512 -binary "$PUBLISHED" | base64 -w0)"
if [ "$INTEGRITY" != "$(jq -r '.dist.integrity' "$META")" ]; then
echo "::error::The @hyperframes/aws-lambda@${VERSION} tarball does not match its npm integrity"
exit 1
fi
elif [ "$STATUS" != "404" ]; then
echo "::error::The npm registry answered $STATUS for @hyperframes/aws-lambda@${VERSION}"
exit 1
fi
ON_RELEASE="$RUNNER_TEMP/release-assets.tsv"
gh api "repos/$REPO/releases/tags/v${VERSION}" --jq '.assets[] | [.name, (.digest // "")] | @tsv' > "$ON_RELEASE"
attach_asset() {
local asset="$1" sidecar="$2" manifest="$3"
local line attached pinned built
line="$(awk -F'\t' -v name="$asset" '$1 == name' "$ON_RELEASE")"
attached="${line#*$'\t'}"
if [ -n "$line" ] && [ -z "$attached" ]; then
echo "::error::Release v${VERSION} has $asset without a digest to check it by"
exit 1
fi
if [ -f "$PUBLISHED" ]; then
pinned="sha256:$(tar -xzOf "$PUBLISHED" "package/prebuilt/$manifest" | jq -r '.sha256')"
if [ "$attached" != "$pinned" ]; then
echo "::error::@hyperframes/aws-lambda@${VERSION} on npm pins $asset at $pinned, but release v${VERSION} has ${attached:-no such asset}"
exit 1
fi
echo "$asset on the release is the one npm pins — keeping it"
return 0
fi
built="sha256:$(sha256sum "$ASSETS/$asset" | cut -d' ' -f1)"
if [ -z "$line" ]; then
gh release upload "v${VERSION}" "$ASSETS/$asset" "$ASSETS/$sidecar" --repo "$REPO"
elif [ "$attached" = "$built" ]; then
echo "$asset is already attached with these contents — skipping"
elif [ "$RUN_ATTEMPT" -gt 1 ]; then
echo "::warning::Replacing $asset on release v${VERSION}; nothing on npm pins it yet."
gh release upload "v${VERSION}" "$ASSETS/$asset" "$ASSETS/$sidecar" --repo "$REPO" --clobber
for name in hyperframes-lambda-handler.zip hyperframes-lambda-handler.sha256 hyperframes-lambda-video-arm64.oci.tar hyperframes-lambda-video-arm64.sha256; do
digest="$(sha256sum "$ASSETS/$name" | cut -d' ' -f1)"
gh api --paginate "repos/$REPO/releases/$RELEASE_ID/assets?per_page=100" > "$RUNNER_TEMP/assets-pages.json"
existing="$(jq -sc --arg name "$name" '[.[][] | select(.name == $name)]' "$RUNNER_TEMP/assets-pages.json")"
if [ "$(jq length <<< "$existing")" != 0 ]; then
jq -e --arg digest "sha256:$digest" 'length == 1 and .[0].state == "uploaded" and .[0].digest == $digest' <<< "$existing" > /dev/null
else
echo "::error::Release v${VERSION} already has $asset, and this run did not attach it. Find out who did before releasing this version."
exit 1
# Upload by the recorded ID; a replacement release at the tag cannot receive these bytes.
gh api --method POST "https://uploads.github.com/repos/$REPO/releases/$RELEASE_ID/assets?name=$name" \
-H 'Content-Type: application/octet-stream' --input "$ASSETS/$name" > "$RUNNER_TEMP/uploaded-asset.json"
jq -e --arg digest "sha256:$digest" '.state == "uploaded" and .digest == $digest' "$RUNNER_TEMP/uploaded-asset.json" > /dev/null
fi
}
done
attach_asset hyperframes-lambda-handler.zip hyperframes-lambda-handler.sha256 handler-zip.json
attach_asset hyperframes-lambda-video-arm64.oci.tar hyperframes-lambda-video-arm64.sha256 video-image.json
- name: Verify Lambda release before npm publication
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
VERSION: ${{ steps.version.outputs.version }}
RELEASE_ID: ${{ steps.release.outputs.id }}
run: |
gh api "repos/$REPO/releases/tags/v$VERSION" > "$RUNNER_TEMP/release.json"
jq -e --argjson id "$RELEASE_ID" '.id == $id' "$RUNNER_TEMP/release.json" > /dev/null
gh api --paginate "repos/$REPO/releases/$RELEASE_ID/assets?per_page=100" > "$RUNNER_TEMP/assets-pages.json"
jq -se --slurpfile receipt "$RUNNER_TEMP/release-receipt/receipt.json" '
[.[][]] as $assets | $receipt[0].assets | to_entries | all(.[];
. as $expected | [$assets[] | select(.name == $expected.key)] |
length == 1 and .[0].state == "uploaded" and .[0].digest == ("sha256:" + $expected.value))
' "$RUNNER_TEMP/assets-pages.json" > /dev/null
# Runs outside the checkout so no project .npmrc applies, and with
# --ignore-scripts so no lifecycle script runs next to the npm token.
@@ -565,7 +668,6 @@ jobs:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
VERSION: ${{ steps.version.outputs.version }}
DIST_TAG: ${{ steps.version.outputs.dist_tag }}
RUN_ATTEMPT: ${{ github.run_attempt }}
run: |
FAILED=0
@@ -578,8 +680,6 @@ jobs:
if published="$(npm view "${name}@${VERSION}" dist.integrity 2>/dev/null)" && [ -n "$published" ]; then
if [ "$published" = "$integrity" ]; then
echo "⏭️ ${name}@${VERSION} already published with these contents — skipping"
elif [ "$RUN_ATTEMPT" -gt 1 ]; then
echo "::warning::${name}@${VERSION} is already on npm with other contents; this re-run keeps the earlier attempt's upload."
else
echo "::error::${name}@${VERSION} is already on npm, and this run did not publish it. Find out who did before releasing this version."
FAILED=1