From a27d09ce80b76df395607313076b1119367eb2af Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 22:06:02 -0700 Subject: [PATCH] ci: backport protected publishing environments to release Adapt the workflow hardening from main to the release branch, including its legacy Cachix publishers. Keep dry runs unprivileged and pass the environment-scoped JSR token only to deno publish. Co-Authored-By: GPT-6.1 Sol --- .github/workflows/alert.yml | 5 ++++- .github/workflows/apt-repo.yml | 3 +++ .github/workflows/cache.yml | 2 ++ .github/workflows/cachix.yml | 2 ++ .github/workflows/dependabot.yml | 3 +++ .github/workflows/docker.yml | 4 ++++ .github/workflows/release-brew.yml | 26 +++++++++++++++++++-- .github/workflows/release-go-ffi.yml | 3 +++ .github/workflows/release-go.yml | 19 ++++++++++++++++ .github/workflows/release-js.yml | 10 ++++++++- .github/workflows/release-kt-ffi.yml | 4 ++++ .github/workflows/release-kt-lib.yml | 19 ++++++++++++++++ .github/workflows/release-py.yml | 17 ++++++++++++++ .github/workflows/release-rs.yml | 3 +++ .github/workflows/release-swift-ffi.yml | 3 +++ .github/workflows/release-swift-lib.yml | 19 ++++++++++++++++ .github/workflows/release-winget.yml | 30 ++++++++++++++++++++++--- .github/workflows/rpm-repo.yml | 3 +++ js/common/release.ts | 12 +++++++--- 19 files changed, 177 insertions(+), 10 deletions(-) diff --git a/.github/workflows/alert.yml b/.github/workflows/alert.yml index 940ceabbf4e..d91f776dcf2 100644 --- a/.github/workflows/alert.yml +++ b/.github/workflows/alert.yml @@ -11,7 +11,7 @@ name: Alert # pull_request_target. The author already sees them as a check on the PR, and # that noise is what drives people to mute Actions notifications wholesale. # -# Needs a DISCORD_WEBHOOK repo secret (Discord: Channel > Integrations > +# Needs a DISCORD_WEBHOOK secret in `publish` (Discord: Channel > Integrations > # Webhooks). Nothing else reads it. on: @@ -77,6 +77,9 @@ jobs: github.event.workflow_run.event != 'pull_request' && github.event.workflow_run.event != 'pull_request_target' runs-on: ubuntu-latest + # The webhook lives in the `publish` Environment, which only protected refs + # can deploy to; workflow_run runs on `main`. + environment: publish steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 diff --git a/.github/workflows/apt-repo.yml b/.github/workflows/apt-repo.yml index 5e8905ed21c..28539125872 100644 --- a/.github/workflows/apt-repo.yml +++ b/.github/workflows/apt-repo.yml @@ -28,6 +28,9 @@ jobs: publish: name: Publish to apt.moq.dev runs-on: ubuntu-22.04 + # Write credentials live in the `publish` Environment, which only protected + # refs can deploy to, so dispatch this from `main`. + environment: publish steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 diff --git a/.github/workflows/cache.yml b/.github/workflows/cache.yml index 4fb0bf42805..44d709c9708 100644 --- a/.github/workflows/cache.yml +++ b/.github/workflows/cache.yml @@ -61,6 +61,8 @@ jobs: rust: false runs-on: ${{ matrix.runner }} timeout-minutes: 60 + # The Cachix write token lives in the protected `publish` Environment. + environment: publish steps: - name: Free disk space diff --git a/.github/workflows/cachix.yml b/.github/workflows/cachix.yml index 7b54d5f2e74..e8addc770f0 100644 --- a/.github/workflows/cachix.yml +++ b/.github/workflows/cachix.yml @@ -15,6 +15,8 @@ jobs: release: name: Release (${{ matrix.os }}) runs-on: ${{ matrix.runs-on }} + # The Cachix write token lives in the protected `publish` Environment. + environment: publish permissions: contents: read strategy: diff --git a/.github/workflows/dependabot.yml b/.github/workflows/dependabot.yml index 9fe2cae19af..cd9b28d0453 100644 --- a/.github/workflows/dependabot.yml +++ b/.github/workflows/dependabot.yml @@ -32,6 +32,9 @@ jobs: github.event.pull_request.user.login == 'dependabot[bot]' && github.event.pull_request.head.repo.full_name == github.repository runs-on: ubuntu-latest + # The moq-bot key lives in the `publish` Environment, which only protected + # refs can deploy to; pull_request_target runs on the base branch. + environment: publish steps: - name: Generate moq-bot token diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 5fcbc152e44..192813197ff 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -74,6 +74,9 @@ jobs: - platform: linux/arm64 runner: ubuntu-24.04-arm runs-on: ${{ matrix.runner }} + # Docker Hub credentials live in the `publish` Environment, which only + # protected refs can deploy to. Only a tag pushes; other builds need none. + environment: ${{ github.ref_type == 'tag' && 'publish' || '' }} permissions: contents: read steps: @@ -119,6 +122,7 @@ jobs: needs: [parse, build] if: github.ref_type == 'tag' runs-on: ubuntu-latest + environment: publish permissions: {} steps: - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 diff --git a/.github/workflows/release-brew.yml b/.github/workflows/release-brew.yml index d31ca30f367..f167a33a410 100644 --- a/.github/workflows/release-brew.yml +++ b/.github/workflows/release-brew.yml @@ -35,15 +35,37 @@ jobs: name: Publish formula to homebrew-tap # workflow_run has the base repository's privileges and this job checks out # the triggering commit, so accept only a successful tag push in this - # repository: a fork PR can rename a workflow and its branch to match. + # repository: a fork PR can rename a workflow and its branch to match. Only + # the tag patterns the `release tags` ruleset protects are accepted. if: >- github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'push' && github.event.workflow_run.head_repository.full_name == github.repository && - startsWith(github.event.workflow_run.head_branch, 'moq-') + (startsWith(github.event.workflow_run.head_branch, 'moq-relay-v') || + startsWith(github.event.workflow_run.head_branch, 'moq-cli-v') || + startsWith(github.event.workflow_run.head_branch, 'moq-gst-v')) runs-on: ubuntu-latest + # Write credentials live in the `publish` Environment, which only protected + # refs can deploy to; workflow_run runs on `main`. + environment: publish steps: + # head_branch names a branch as readily as a tag, and anyone who can push + # may name a branch and a workflow after a release. Only a tag the + # `release tags` ruleset protects, at the run's commit, is a release. + - name: Require a release tag at the run's commit + shell: bash + env: + GH_TOKEN: ${{ github.token }} + TAG: ${{ github.event.workflow_run.head_branch }} + SHA: ${{ github.event.workflow_run.head_sha }} + run: | + commit=$(gh api "repos/$GITHUB_REPOSITORY/commits/refs/tags/$TAG" --jq .sha) + if [[ "$commit" != "$SHA" ]]; then + echo "::error::$TAG is not a release tag at $SHA" + exit 1 + fi + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: # workflow_run runs the default branch's file, whose checkout would diff --git a/.github/workflows/release-go-ffi.yml b/.github/workflows/release-go-ffi.yml index b6e00eaa348..03da4869121 100644 --- a/.github/workflows/release-go-ffi.yml +++ b/.github/workflows/release-go-ffi.yml @@ -118,6 +118,9 @@ jobs: needs: [ffi, package] runs-on: ubuntu-latest if: github.ref_type == 'tag' + # The moq-bot key lives in the `publish` Environment, which only protected + # refs can deploy to, `moq-ffi-v*` tags among them. + environment: publish steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 diff --git a/.github/workflows/release-go.yml b/.github/workflows/release-go.yml index 260ac06393d..80997e46a33 100644 --- a/.github/workflows/release-go.yml +++ b/.github/workflows/release-go.yml @@ -65,6 +65,22 @@ jobs: with: ref: ${{ github.sha }} persist-credentials: false + # head_branch names a branch as readily as a tag, and anyone who can push + # may name a branch and a workflow after a release. Only a tag the + # `release tags` ruleset protects, at the run's commit, is a release. + - name: Require a release tag at the run's commit + if: github.event_name == 'workflow_run' + shell: bash + env: + GH_TOKEN: ${{ github.token }} + TAG: ${{ github.event.workflow_run.head_branch }} + SHA: ${{ github.event.workflow_run.head_sha }} + run: | + commit=$(gh api "repos/$GITHUB_REPOSITORY/commits/refs/tags/$TAG" --jq .sha) + if [[ "$commit" != "$SHA" ]]; then + echo "::error::$TAG is not a release tag at $SHA" + exit 1 + fi - uses: ./.github/actions/just - name: Check independent publisher id: publisher @@ -202,6 +218,9 @@ jobs: needs: [ffi-release, build] if: needs.build.outputs.skip != 'true' runs-on: ubuntu-latest + # The moq-bot key lives in the `publish` Environment, which only protected + # refs can deploy to: `release`, and `main` for workflow_run. + environment: publish steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 diff --git a/.github/workflows/release-js.yml b/.github/workflows/release-js.yml index 7a29475abea..be19133516f 100644 --- a/.github/workflows/release-js.yml +++ b/.github/workflows/release-js.yml @@ -27,6 +27,11 @@ jobs: name: Release JS Packages runs-on: ubuntu-latest if: github.repository_owner == 'moq-dev' + # npm trusted publishing can require this Environment, which only `release` + # can deploy to, so a branch's OIDC identity cannot publish. It also holds + # the JSR token, since JSR's OIDC trust cannot require an Environment. + # Dry-runs skip it. + environment: ${{ github.event_name == 'push' && 'npm' || '' }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 @@ -52,5 +57,8 @@ jobs: - name: Release packages env: - DRY_RUN: ${{ github.event_name == 'pull_request' }} + DRY_RUN: ${{ github.event_name != 'push' }} + # Only the `npm` Environment holds it, so dry-runs get none. + # release.ts hands it to `deno publish` alone. + JSR_TOKEN: ${{ secrets.JSR_TOKEN }} run: nix develop --command bun --filter '*' release diff --git a/.github/workflows/release-kt-ffi.yml b/.github/workflows/release-kt-ffi.yml index cffcab9534d..dff23c72461 100644 --- a/.github/workflows/release-kt-ffi.yml +++ b/.github/workflows/release-kt-ffi.yml @@ -44,6 +44,10 @@ jobs: name: Package and publish needs: [ffi] runs-on: ubuntu-latest + # Maven and signing credentials live in the `publish` Environment, which + # only protected refs can deploy to. Only a tag publishes; a dry-run needs + # no secrets, so it can run from any branch. + environment: ${{ github.ref_type == 'tag' && 'publish' || '' }} env: # Picked up by com.vanniktech.maven.publish via Gradle project properties. ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.MAVEN_CENTRAL_USERNAME }} diff --git a/.github/workflows/release-kt-lib.yml b/.github/workflows/release-kt-lib.yml index bc48131eb48..3c159e4c491 100644 --- a/.github/workflows/release-kt-lib.yml +++ b/.github/workflows/release-kt-lib.yml @@ -68,6 +68,22 @@ jobs: with: ref: ${{ github.sha }} persist-credentials: false + # head_branch names a branch as readily as a tag, and anyone who can push + # may name a branch and a workflow after a release. Only a tag the + # `release tags` ruleset protects, at the run's commit, is a release. + - name: Require a release tag at the run's commit + if: github.event_name == 'workflow_run' + shell: bash + env: + GH_TOKEN: ${{ github.token }} + TAG: ${{ github.event.workflow_run.head_branch }} + SHA: ${{ github.event.workflow_run.head_sha }} + run: | + commit=$(gh api "repos/$GITHUB_REPOSITORY/commits/refs/tags/$TAG" --jq .sha) + if [[ "$commit" != "$SHA" ]]; then + echo "::error::$TAG is not a release tag at $SHA" + exit 1 + fi - uses: ./.github/actions/just - name: Check independent publisher id: publisher @@ -178,6 +194,9 @@ jobs: group: release-kt-lib-${{ github.event_name == 'pull_request' && github.ref || 'publish' }} cancel-in-progress: ${{ github.event_name == 'pull_request' }} runs-on: ubuntu-latest + # Maven and signing credentials live in the `publish` Environment, which + # only protected refs can deploy to. A dry-run needs no secrets. + environment: ${{ github.event_name != 'pull_request' && 'publish' || '' }} env: # Picked up by com.vanniktech.maven.publish via Gradle project properties. ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.MAVEN_CENTRAL_USERNAME }} diff --git a/.github/workflows/release-py.yml b/.github/workflows/release-py.yml index 51fb6f5bd52..e48ce6c85bd 100644 --- a/.github/workflows/release-py.yml +++ b/.github/workflows/release-py.yml @@ -50,6 +50,23 @@ jobs: publish: ${{ steps.pypi.outputs.exists == 'false' && (github.event_name == 'workflow_run' || (steps.ffi-gate.outputs.unreleased != 'true' && steps.ffi-pypi.outputs.exists == 'true')) }} steps: + # head_branch names a branch as readily as a tag, and anyone who can push + # may name a branch and a workflow after a release. Only a tag the + # `release tags` ruleset protects, at the run's commit, is a release. + - name: Require a release tag at the run's commit + if: github.event_name == 'workflow_run' + shell: bash + env: + GH_TOKEN: ${{ github.token }} + TAG: ${{ github.event.workflow_run.head_branch }} + SHA: ${{ github.event.workflow_run.head_sha }} + run: | + commit=$(gh api "repos/$GITHUB_REPOSITORY/commits/refs/tags/$TAG" --jq .sha) + if [[ "$commit" != "$SHA" ]]; then + echo "::error::$TAG is not a release tag at $SHA" + exit 1 + fi + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: ref: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.head_sha || github.sha }} diff --git a/.github/workflows/release-rs.yml b/.github/workflows/release-rs.yml index de27458731d..4cb904781fb 100644 --- a/.github/workflows/release-rs.yml +++ b/.github/workflows/release-rs.yml @@ -36,6 +36,9 @@ jobs: runs-on: ubuntu-latest if: ${{ github.repository_owner == 'moq-dev' && github.ref == 'refs/heads/release' }} + # Write credentials live in the `publish` Environment, which only protected + # refs can deploy to. The R2 cache pair stays the repository's read key. + environment: publish env: # release-plz copies each crate into its own temp directory to compare it diff --git a/.github/workflows/release-swift-ffi.yml b/.github/workflows/release-swift-ffi.yml index 28a06528b7d..5080405c45b 100644 --- a/.github/workflows/release-swift-ffi.yml +++ b/.github/workflows/release-swift-ffi.yml @@ -157,6 +157,9 @@ jobs: name: Publish to Swift Package mirror needs: [ffi, verify] runs-on: ubuntu-latest + # The moq-bot key lives in the `publish` Environment, which only protected + # refs can deploy to: `moq-ffi-v*` tags, and `main` for Nightly. + environment: publish steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 diff --git a/.github/workflows/release-swift-lib.yml b/.github/workflows/release-swift-lib.yml index 48be6c84c03..8a853c67298 100644 --- a/.github/workflows/release-swift-lib.yml +++ b/.github/workflows/release-swift-lib.yml @@ -66,6 +66,22 @@ jobs: with: ref: ${{ github.sha }} persist-credentials: false + # head_branch names a branch as readily as a tag, and anyone who can push + # may name a branch and a workflow after a release. Only a tag the + # `release tags` ruleset protects, at the run's commit, is a release. + - name: Require a release tag at the run's commit + if: github.event_name == 'workflow_run' + shell: bash + env: + GH_TOKEN: ${{ github.token }} + TAG: ${{ github.event.workflow_run.head_branch }} + SHA: ${{ github.event.workflow_run.head_sha }} + run: | + commit=$(gh api "repos/$GITHUB_REPOSITORY/commits/refs/tags/$TAG" --jq .sha) + if [[ "$commit" != "$SHA" ]]; then + echo "::error::$TAG is not a release tag at $SHA" + exit 1 + fi - uses: ./.github/actions/just - name: Check independent publisher id: publisher @@ -212,6 +228,9 @@ jobs: needs: [ffi-release, build, verify] runs-on: ubuntu-latest if: ${{ needs.build.outputs.publish == 'true' }} + # The moq-bot key lives in the `publish` Environment, which only protected + # refs can deploy to: `release`, and `main` for workflow_run. + environment: publish steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 diff --git a/.github/workflows/release-winget.yml b/.github/workflows/release-winget.yml index 7e069b78d89..b039f85b19b 100644 --- a/.github/workflows/release-winget.yml +++ b/.github/workflows/release-winget.yml @@ -52,12 +52,36 @@ env: jobs: publish: name: Submit winget manifest - # workflow_run: only after a successful release of a moq-* tag. - # workflow_dispatch: always (it's an explicit dry run). - if: ${{ github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success' && startsWith(github.event.workflow_run.head_branch, 'moq-')) }} + # workflow_run: only after a successful release of a tag the `release tags` + # ruleset protects. workflow_dispatch: always (it's an explicit dry run). + if: >- + github.event_name == 'workflow_dispatch' || + (github.event.workflow_run.conclusion == 'success' && + (startsWith(github.event.workflow_run.head_branch, 'moq-relay-v') || + startsWith(github.event.workflow_run.head_branch, 'moq-cli-v'))) runs-on: windows-latest + # WINGET_TOKEN lives in the `publish` Environment, which only protected refs + # can deploy to. A dry run needs no token, so it can run from any branch. + environment: ${{ github.event_name == 'workflow_run' && 'publish' || '' }} steps: + # head_branch names a branch as readily as a tag, and anyone who can push + # may name a branch and a workflow after a release. Only a tag the + # `release tags` ruleset protects, at the run's commit, is a release. + - name: Require a release tag at the run's commit + if: github.event_name == 'workflow_run' + shell: bash + env: + GH_TOKEN: ${{ github.token }} + TAG: ${{ github.event.workflow_run.head_branch }} + SHA: ${{ github.event.workflow_run.head_sha }} + run: | + commit=$(gh api "repos/$GITHUB_REPOSITORY/commits/refs/tags/$TAG" --jq .sha) + if [[ "$commit" != "$SHA" ]]; then + echo "::error::$TAG is not a release tag at $SHA" + exit 1 + fi + - name: Resolve mode and tag id: mode shell: bash diff --git a/.github/workflows/rpm-repo.yml b/.github/workflows/rpm-repo.yml index 93dc8c4a943..22bee98c37b 100644 --- a/.github/workflows/rpm-repo.yml +++ b/.github/workflows/rpm-repo.yml @@ -26,6 +26,9 @@ jobs: publish: name: Publish to rpm.moq.dev runs-on: ubuntu-22.04 + # Write credentials live in the `publish` Environment, which only protected + # refs can deploy to, so dispatch this from `main`. + environment: publish steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 diff --git a/js/common/release.ts b/js/common/release.ts index f5ccc4dae77..b3923e554d2 100644 --- a/js/common/release.ts +++ b/js/common/release.ts @@ -2,6 +2,11 @@ import { execFileSync } from "node:child_process"; const dryRun = process.argv.includes("--dry-run") || process.env.DRY_RUN === "true"; +// JSR publish token, from the `npm` Environment. Remove it from this process's +// environment so only `deno publish` receives it, not builds or npm. +const jsrToken = process.env.JSR_TOKEN; +delete process.env.JSR_TOKEN; + // Read package.json to get name, version, and whether to also publish to JSR. const pkg = JSON.parse(await Bun.file("package.json").text()); const { name, version } = pkg; @@ -72,9 +77,9 @@ if (publishJsr) { // Call `deno publish` directly (what `jsr publish` wraps) using the deno from // the nix devshell, so parallel package releases don't race on a runtime // jsr/deno download into a shared bunx cache. jsr.json and the generated dist - // leave the tree dirty, so allow it. OIDC trusted publishing kicks in - // automatically under GitHub Actions; locally it falls back to an interactive - // browser login. + // leave the tree dirty, so allow it. Without a token, Deno tries JSR's GitHub + // OIDC trust under GitHub Actions, which the packages should not grant since + // any branch of the repository could claim it, and a browser login locally. const args = [ "publish", "--unstable-bare-node-builtins", @@ -114,6 +119,7 @@ if (publishJsr) { console.log(`⏭️ ${name}@${version} already on JSR, skipping`); } else { console.log(`🚀 Publishing ${name}@${version} to JSR...`); + if (jsrToken) args.push("--token", jsrToken); execFileSync("deno", args, { stdio: "inherit", env }); } }