From 6e2362ec09eb7cd0b15746d612a0c2a2965ed384 Mon Sep 17 00:00:00 2001 From: Johnathan Falk Date: Fri, 6 Nov 2026 20:39:52 -0500 Subject: [PATCH 1/3] ci(release): push tags and changelog as the CI bot GitHub App reusable-release: build-go checks out with the app token it mints (checkout persisted GITHUB_TOKEN, whose extraheader overrode the app token in gha-release-go's push URL) and collect-changelog tries the app token first for the direct push to the default branch. New optional input ci-bot-app-client-id and secret CI_BOT_APP_PRIVATE_KEY select the CI bot app; the legacy CI_APP_* pair still works. Both jobs run in the ci-bot environment and their mints are narrowed with permission-* inputs. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_011To9HQJLrCH6wYa5WCpw3L --- .github/guid-index.json | 2 +- .github/workflows/reusable-release.yml | 255 ++++++++++++++++++++----- changelog.d/ci-bot-app-auth.md | 29 +++ 3 files changed, 236 insertions(+), 50 deletions(-) create mode 100644 changelog.d/ci-bot-app-auth.md diff --git a/.github/guid-index.json b/.github/guid-index.json index 4782a1b0..0c7b96b7 100644 --- a/.github/guid-index.json +++ b/.github/guid-index.json @@ -1003,7 +1003,7 @@ "path": ".github/workflows/reusable-release.yml", "guid": "7b8c9d0e-1f2a-3b4c-5d6e-7f8a9b0c1d2e", "title": "Reusable Release Coordinator", - "version": "2.17.0", + "version": "2.18.0", "header_path": ".github/workflows/reusable-release.yml", "path_mismatch": false }, diff --git a/.github/workflows/reusable-release.yml b/.github/workflows/reusable-release.yml index 96ae84b2..6c312d95 100644 --- a/.github/workflows/reusable-release.yml +++ b/.github/workflows/reusable-release.yml @@ -1,5 +1,5 @@ # file: .github/workflows/reusable-release.yml -# version: 2.17.0 +# version: 2.18.0 # guid: 7b8c9d0e-1f2a-3b4c-5d6e-7f8a9b0c1d2e # last-edited: 2026-11-06 @@ -109,6 +109,24 @@ on: required: false type: string default: '' + ci-bot-app-client-id: + description: "Client ID of the CI bot GitHub App (not secret). Empty falls back to the caller's CI_BOT_APP_CLIENT_ID variable. Used with the CI_BOT_APP_PRIVATE_KEY secret to mint the token that pushes release tags and the changelog commit; when neither is configured the legacy CI_APP_ID / CI_APP_PRIVATE_KEY pair is used." + required: false + type: string + default: '' + secrets: + CI_BOT_APP_PRIVATE_KEY: + description: 'Private key of the CI bot GitHub App named by ci-bot-app-client-id. Optional; callers using `secrets: inherit` get it automatically.' + required: false + CI_APP_ID: + description: 'Legacy CI GitHub App id. Optional; used only when the CI bot app is not configured.' + required: false + CI_APP_PRIVATE_KEY: + description: 'Legacy CI GitHub App private key. Optional; used only when the CI bot app is not configured.' + required: false + JF_CI_GH_PAT: + description: 'Personal access token fallback for the changelog push and pull request. Optional; used only when no app credentials are configured.' + required: false outputs: release-created: description: 'Whether a release was created' @@ -405,39 +423,104 @@ jobs: always() && needs.detect-languages.outputs.has-go == 'true' && (inputs.build-target == 'all' || inputs.build-target == 'go') + # The CI bot app key lives in this environment (the caller limits it to its + # default branch), so a workflow on another branch cannot mint a token that + # bypasses the default branch's ruleset. Environment secrets reach only jobs + # that name the environment, and `secrets: inherit` does not carry them, so + # the job must declare it here. A caller without the environment gets one + # created empty on first use; it then keeps using its repository secrets. + environment: ci-bot steps: - - name: Checkout repository - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - submodules: recursive - fetch-depth: 0 - - # Mint a short-lived GitHub App installation token so the tag-push - # step inside release-go-action can push refs whose reachable - # commits touch .github/workflows/ — the default GITHUB_TOKEN - # can't. Skipped entirely when CI_APP_ID isn't configured so the - # job doesn't emit a noisy error; falls back to GITHUB_TOKEN. - # `if:` cannot reference secrets.* directly in a workflow_call workflow - # ("Unrecognized named-value: 'secrets'"). Materialize CI_APP_ID through - # env in a guard step, then gate the mint step on that step's output. - - name: Check for App credentials - id: check-app + # The app credentials are resolved and minted BEFORE checkout so checkout + # can take the app token. + # + # Which GitHub App credentials this job has. `if:` cannot read secrets.* in + # a workflow_call workflow ("Unrecognized named-value: 'secrets'"), so the + # presence of each is materialised through env first and the mint steps + # below are gated on this step's `mode` output: + # bot CI bot app (client id + CI_BOT_APP_PRIVATE_KEY). Preferred: the + # main-branch ruleset's merge-queue bypass names this app. + # legacy CI_APP_ID + CI_APP_PRIVATE_KEY, kept for consumers that have + # not moved to the bot app. + # none no app; callers without one keep working on their other tokens. + # A half-configured pair is an error, not "none": silently dropping to a + # token that cannot push hides the misconfiguration until a push fails. + # (The one exception is a pull_request run, where secrets are withheld.) + - name: Resolve CI app credentials + id: app-creds env: - CI_APP_ID: ${{ secrets.CI_APP_ID }} + BOT_CLIENT_ID: ${{ inputs.ci-bot-app-client-id || vars.CI_BOT_APP_CLIENT_ID }} + BOT_HAS_KEY: ${{ secrets.CI_BOT_APP_PRIVATE_KEY != '' }} + LEGACY_HAS_ID: ${{ secrets.CI_APP_ID != '' }} + LEGACY_HAS_KEY: ${{ secrets.CI_APP_PRIVATE_KEY != '' }} + EVENT_NAME: ${{ github.event_name }} run: | - if [ -n "$CI_APP_ID" ]; then - echo "have-app=true" >> "$GITHUB_OUTPUT" - else - echo "have-app=false" >> "$GITHUB_OUTPUT" + set -euo pipefail + mode=none + if [ -n "$BOT_CLIENT_ID" ] && [ "$BOT_HAS_KEY" = true ]; then + mode=bot + elif [ "$BOT_HAS_KEY" = true ]; then + echo "::error::CI bot app is half-configured: CI_BOT_APP_PRIVATE_KEY is set but the client id (input ci-bot-app-client-id or variable CI_BOT_APP_CLIENT_ID) is empty." + exit 1 + elif [ -n "$BOT_CLIENT_ID" ]; then + # Repository variables reach every run but secrets are withheld from + # fork and Dependabot pull requests, so a missing key there is + # expected, not a misconfiguration. + case "$EVENT_NAME" in + pull_request|pull_request_target) + echo "::notice::CI bot app secret is not available to this pull request run; continuing without app credentials." ;; + *) + echo "::error::CI bot app is half-configured: the client id is set but the CI_BOT_APP_PRIVATE_KEY secret is empty." + exit 1 ;; + esac fi + if [ "$mode" = none ]; then + if [ "$LEGACY_HAS_ID" = true ] && [ "$LEGACY_HAS_KEY" = true ]; then + mode=legacy + elif [ "$LEGACY_HAS_ID" = true ] || [ "$LEGACY_HAS_KEY" = true ]; then + echo "::error::Legacy CI app is half-configured: CI_APP_ID and CI_APP_PRIVATE_KEY must both be set." + exit 1 + fi + fi + echo "mode=${mode}" >> "$GITHUB_OUTPUT" - - name: Mint App token for tag push - id: app-token - if: steps.check-app.outputs.have-app == 'true' + - name: Mint CI app token for the tag push + id: app-token-bot + if: steps.app-creds.outputs.mode == 'bot' + uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 + with: + client-id: ${{ inputs.ci-bot-app-client-id || vars.CI_BOT_APP_CLIENT_ID }} + private-key: ${{ secrets.CI_BOT_APP_PRIVATE_KEY }} + permission-contents: write + # The release tag points at a commit whose history can include + # .github/workflows/ changes; a push of such a ref is refused without it. + permission-workflows: write + + - name: Mint legacy CI app token for the tag push + id: app-token-legacy + if: steps.app-creds.outputs.mode == 'legacy' uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 with: app-id: ${{ secrets.CI_APP_ID }} private-key: ${{ secrets.CI_APP_PRIVATE_KEY }} + permission-contents: write + # The release tag points at a commit whose history can include + # .github/workflows/ changes; a push of such a ref is refused without it. + permission-workflows: write + + # token: the app token when there is one (else GITHUB_TOKEN, as before). + # Checkout persists it as http.extraheader, and git sends that header in + # preference to credentials embedded in a push URL. gha-release-go pushes + # the tag to a URL carrying its github-token input (the same app token), so + # the two now agree and the tag really is pushed by the app. With + # the default checkout the persisted GITHUB_TOKEN won and the minted app + # token was never used. + - name: Checkout repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + submodules: recursive + fetch-depth: 0 + token: ${{ steps.app-token-bot.outputs.token || steps.app-token-legacy.outputs.token || github.token }} # goreleaser's `{{ .PreviousTag }}` walks back exactly one tag. On a repo # that cuts an RC per merge, that tag is almost always an RC, so release @@ -494,7 +577,7 @@ jobs: pre-build-script: ${{ inputs.pre-build-script }} go-experiment: ${{ inputs.go-experiment }} run-linters: ${{ inputs.go-run-linters }} - github-token: ${{ steps.app-token.outputs.token || secrets.GITHUB_TOKEN }} + github-token: ${{ steps.app-token-bot.outputs.token || steps.app-token-legacy.outputs.token || secrets.GITHUB_TOKEN }} # Python Build build-python: @@ -1181,6 +1264,13 @@ jobs: github.event_name != 'pull_request' && inputs.changelog-collect != 'false' && needs.detect-languages.outputs.auto-prerelease != 'true' + # The CI bot app key lives in this environment (the caller limits it to its + # default branch), so a workflow on another branch cannot mint a token that + # bypasses the default branch's ruleset. Environment secrets reach only jobs + # that name the environment, and `secrets: inherit` does not carry them, so + # the job must declare it here. A caller without the environment gets one + # created empty on first use; it then keeps using its repository secrets. + environment: ci-bot steps: # The released commit (github.sha), so exactly the fragments that shipped # in this version are collected; fragments merged after it stay pending. @@ -1225,31 +1315,82 @@ jobs: fi echo "collect=${collect}" >> "$GITHUB_OUTPUT" - # Same guard as build-go: `if:` cannot read secrets.* in a workflow_call - # workflow, so materialise CI_APP_ID through env first. - - name: Check for App credentials - id: check-app + # The token pushes the changelog commit straight to the default branch and, + # if that is refused, opens the fallback pull request. It must be the app's: + # the default branch's ruleset lists the CI bot app as its bypass actor, and + # a pull request opened with GITHUB_TOKEN starts no pull_request workflows, + # so its required checks would never report. + # + # Which GitHub App credentials this job has. `if:` cannot read secrets.* in + # a workflow_call workflow ("Unrecognized named-value: 'secrets'"), so the + # presence of each is materialised through env first and the mint steps + # below are gated on this step's `mode` output: + # bot CI bot app (client id + CI_BOT_APP_PRIVATE_KEY). Preferred: the + # main-branch ruleset's merge-queue bypass names this app. + # legacy CI_APP_ID + CI_APP_PRIVATE_KEY, kept for consumers that have + # not moved to the bot app. + # none no app; callers without one keep working on their other tokens. + # A half-configured pair is an error, not "none": silently dropping to a + # token that cannot push hides the misconfiguration until a push fails. + # (The one exception is a pull_request run, where secrets are withheld.) + - name: Resolve CI app credentials + id: app-creds if: steps.plan.outputs.collect == 'true' env: - CI_APP_ID: ${{ secrets.CI_APP_ID }} + BOT_CLIENT_ID: ${{ inputs.ci-bot-app-client-id || vars.CI_BOT_APP_CLIENT_ID }} + BOT_HAS_KEY: ${{ secrets.CI_BOT_APP_PRIVATE_KEY != '' }} + LEGACY_HAS_ID: ${{ secrets.CI_APP_ID != '' }} + LEGACY_HAS_KEY: ${{ secrets.CI_APP_PRIVATE_KEY != '' }} + EVENT_NAME: ${{ github.event_name }} run: | - if [ -n "$CI_APP_ID" ]; then - echo "have-app=true" >> "$GITHUB_OUTPUT" - else - echo "have-app=false" >> "$GITHUB_OUTPUT" + set -euo pipefail + mode=none + if [ -n "$BOT_CLIENT_ID" ] && [ "$BOT_HAS_KEY" = true ]; then + mode=bot + elif [ "$BOT_HAS_KEY" = true ]; then + echo "::error::CI bot app is half-configured: CI_BOT_APP_PRIVATE_KEY is set but the client id (input ci-bot-app-client-id or variable CI_BOT_APP_CLIENT_ID) is empty." + exit 1 + elif [ -n "$BOT_CLIENT_ID" ]; then + # Repository variables reach every run but secrets are withheld from + # fork and Dependabot pull requests, so a missing key there is + # expected, not a misconfiguration. + case "$EVENT_NAME" in + pull_request|pull_request_target) + echo "::notice::CI bot app secret is not available to this pull request run; continuing without app credentials." ;; + *) + echo "::error::CI bot app is half-configured: the client id is set but the CI_BOT_APP_PRIVATE_KEY secret is empty." + exit 1 ;; + esac fi + if [ "$mode" = none ]; then + if [ "$LEGACY_HAS_ID" = true ] && [ "$LEGACY_HAS_KEY" = true ]; then + mode=legacy + elif [ "$LEGACY_HAS_ID" = true ] || [ "$LEGACY_HAS_KEY" = true ]; then + echo "::error::Legacy CI app is half-configured: CI_APP_ID and CI_APP_PRIVATE_KEY must both be set." + exit 1 + fi + fi + echo "mode=${mode}" >> "$GITHUB_OUTPUT" - # Used only if the default branch refuses a direct push (a ruleset or a - # merge queue with no bypass). A pull request opened with GITHUB_TOKEN - # starts no pull_request workflows, so its required checks would never - # report; one opened with an App token (or a user PAT) does. - - name: Mint App token for the changelog pull request - id: app-token - if: steps.plan.outputs.collect == 'true' && steps.check-app.outputs.have-app == 'true' + - name: Mint CI app token for the changelog commit + id: app-token-bot + if: steps.plan.outputs.collect == 'true' && steps.app-creds.outputs.mode == 'bot' + uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 + with: + client-id: ${{ inputs.ci-bot-app-client-id || vars.CI_BOT_APP_CLIENT_ID }} + private-key: ${{ secrets.CI_BOT_APP_PRIVATE_KEY }} + permission-contents: write + permission-pull-requests: write + + - name: Mint legacy CI app token for the changelog commit + id: app-token-legacy + if: steps.plan.outputs.collect == 'true' && steps.app-creds.outputs.mode == 'legacy' uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 with: app-id: ${{ secrets.CI_APP_ID }} private-key: ${{ secrets.CI_APP_PRIVATE_KEY }} + permission-contents: write + permission-pull-requests: write - name: Collect changelog fragments if: steps.plan.outputs.collect == 'true' @@ -1257,7 +1398,8 @@ jobs: VERSION: ${{ needs.create-release.outputs.version }} DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} CHANGELOG_PAT: ${{ secrets.JF_CI_GH_PAT }} - APP_TOKEN: ${{ steps.app-token.outputs.token }} + APP_TOKEN: ${{ steps.app-token-bot.outputs.token || steps.app-token-legacy.outputs.token }} + APP_SLUG: ${{ steps.app-token-bot.outputs.app-slug || steps.app-token-legacy.outputs.app-slug }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | set -euo pipefail @@ -1286,8 +1428,21 @@ jobs: python3 -m pip install --quiet scriv - git config user.name "github-actions[bot]" - git config user.email "github-actions[bot]@users.noreply.github.com" + # Commit as the app's bot account when there is one; the user id is + # looked up, never hard-coded, so renaming the app needs no edit here. + # The identity is cosmetic: the token that pushes decides who bypasses. + GIT_NAME="github-actions[bot]" + GIT_EMAIL="github-actions[bot]@users.noreply.github.com" + if [ -n "${APP_TOKEN:-}" ] && [ -n "${APP_SLUG:-}" ]; then + if bot_id="$(GH_TOKEN="$APP_TOKEN" gh api "users/${APP_SLUG}%5Bbot%5D" --jq .id)" && [[ "$bot_id" =~ ^[0-9]+$ ]]; then + GIT_NAME="${APP_SLUG}[bot]" + GIT_EMAIL="${bot_id}+${APP_SLUG}[bot]@users.noreply.github.com" + else + echo "::warning::Could not resolve the user id of ${APP_SLUG}[bot]; committing as github-actions[bot]." + fi + fi + git config user.name "$GIT_NAME" + git config user.email "$GIT_EMAIL" python3 -m scriv collect --version "${VERSION}" @@ -1318,9 +1473,11 @@ jobs: git cherry-pick "${COMMIT}" } - # 1. Direct push, as before, for repositories that allow it. Prefer the - # PAT, fall back to the workflow token. - PUSH_TOKEN="${CHANGELOG_PAT:-$GITHUB_TOKEN}" + # 1. Direct push, for repositories that allow it. Prefer the app: its + # token is the one a ruleset bypass (a merge queue's included) can + # name. Without it the push always hit the ruleset and fell through + # to the pull request below. Then the PAT, then the workflow token. + PUSH_TOKEN="${APP_TOKEN:-${CHANGELOG_PAT:-$GITHUB_TOKEN}}" ERR="$(mktemp)" n=0 while :; do @@ -1395,7 +1552,7 @@ jobs: break done if [ -z "$opened" ]; then - echo "::error::Could not open the changelog pull request for ${VERSION}: set CI_APP_ID/CI_APP_PRIVATE_KEY (App with contents and pull-requests write) or JF_CI_GH_PAT." + echo "::error::Could not open the changelog pull request for ${VERSION}: set CI_BOT_APP_PRIVATE_KEY and the CI_BOT_APP_CLIENT_ID variable (or the legacy CI_APP_ID/CI_APP_PRIVATE_KEY; App with contents and pull-requests write) or JF_CI_GH_PAT." exit 1 fi if [ "$opened" = pat ] && [ -n "${APP_TOKEN:-}" ]; then diff --git a/changelog.d/ci-bot-app-auth.md b/changelog.d/ci-bot-app-auth.md new file mode 100644 index 00000000..20149731 --- /dev/null +++ b/changelog.d/ci-bot-app-auth.md @@ -0,0 +1,29 @@ +### Changed + +#### `reusable-release.yml` — release tag push and changelog commit can run as the CI bot GitHub App + +`build-go` (release tag push) and `collect-changelog` (changelog commit) now +authenticate as the CI bot GitHub App when the caller provides one. A new +optional input `ci-bot-app-client-id` (falling back to the caller's +`CI_BOT_APP_CLIENT_ID` variable) and optional secret `CI_BOT_APP_PRIVATE_KEY` +select it; the legacy `CI_APP_ID` / `CI_APP_PRIVATE_KEY` pair keeps working for +consumers that have not moved, and a caller with neither behaves as before. Both +jobs declare `environment: ci-bot`, so the key can be held as an environment +secret limited to the default branch. A half-configured pair is an error, +except on `pull_request` runs where secrets are withheld. Both mints are +narrowed with `permission-*` inputs. + +#### `reusable-release.yml` `build-go` — the app token is now actually used for the tag push + +The token was minted but never used: checkout persisted `GITHUB_TOKEN` as +`http.extraheader`, and git sends that header in preference to the token +embedded in the push URL. Checkout now takes the app token, so the persisted +credential and the tag push agree. + +#### `reusable-release.yml` `collect-changelog` — direct push tries the app first + +The direct push to the default branch now prefers the app token, then the PAT, +then `GITHUB_TOKEN`. Previously the app was used only for the fallback pull +request, so a ruleset bypass naming the app never helped and every collect fell +through to the pull request. The commit carries the app's bot identity, resolved +at runtime. From b39ff9a89884542c75ed28bbe90115d23a4c8ed5 Mon Sep 17 00:00:00 2001 From: Johnathan Falk Date: Fri, 6 Nov 2026 21:02:23 -0500 Subject: [PATCH 2/3] ci(release): keep build-go on GITHUB_TOKEN; opt-in bot environment; split collect Revert build-go to its original definition: no bot mint, default checkout. A queue-bypassing token must not be persisted into a checkout where dependency installs, tests and GoReleaser run, and an app-pushed tag would start push:tags workflows in consumers. collect-changelog now takes the environment from a bot-environment input (default none) so only a repository that owns the app key can use it, treats a client id without a key as mode none with a warning outside an environment, and is split so the step that installs scriv (pinned to 1.8.0) and collects holds no token; the app token, PAT and GITHUB_TOKEN reach only the publish step, which re-verifies the commit before pushing. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_011To9HQJLrCH6wYa5WCpw3L --- .github/guid-index.json | 2 +- .github/workflows/reusable-release.yml | 262 ++++++++++++------------- changelog.d/ci-bot-app-auth.md | 43 ++-- 3 files changed, 148 insertions(+), 159 deletions(-) diff --git a/.github/guid-index.json b/.github/guid-index.json index 0c7b96b7..11163839 100644 --- a/.github/guid-index.json +++ b/.github/guid-index.json @@ -1003,7 +1003,7 @@ "path": ".github/workflows/reusable-release.yml", "guid": "7b8c9d0e-1f2a-3b4c-5d6e-7f8a9b0c1d2e", "title": "Reusable Release Coordinator", - "version": "2.18.0", + "version": "2.18.1", "header_path": ".github/workflows/reusable-release.yml", "path_mismatch": false }, diff --git a/.github/workflows/reusable-release.yml b/.github/workflows/reusable-release.yml index 6c312d95..00dee5fb 100644 --- a/.github/workflows/reusable-release.yml +++ b/.github/workflows/reusable-release.yml @@ -1,5 +1,5 @@ # file: .github/workflows/reusable-release.yml -# version: 2.18.0 +# version: 2.18.1 # guid: 7b8c9d0e-1f2a-3b4c-5d6e-7f8a9b0c1d2e # last-edited: 2026-11-06 @@ -110,19 +110,24 @@ on: type: string default: '' ci-bot-app-client-id: - description: "Client ID of the CI bot GitHub App (not secret). Empty falls back to the caller's CI_BOT_APP_CLIENT_ID variable. Used with the CI_BOT_APP_PRIVATE_KEY secret to mint the token that pushes release tags and the changelog commit; when neither is configured the legacy CI_APP_ID / CI_APP_PRIVATE_KEY pair is used." + description: "Client ID of the CI bot GitHub App (not secret). Empty falls back to the caller's CI_BOT_APP_CLIENT_ID variable. Only the changelog commit uses the bot app; it needs this and the CI_BOT_APP_PRIVATE_KEY secret." + required: false + type: string + default: '' + bot-environment: + description: "Name of the caller's GitHub Environment that holds CI_BOT_APP_PRIVATE_KEY (for example ci-bot). Empty (the default) runs the changelog job outside any environment, so the bot app is not used. Only a repository that owns the app key and a ruleset bypass for it should set this." required: false type: string default: '' secrets: CI_BOT_APP_PRIVATE_KEY: - description: 'Private key of the CI bot GitHub App named by ci-bot-app-client-id. Optional; callers using `secrets: inherit` get it automatically.' + description: 'Private key of the CI bot GitHub App named by ci-bot-app-client-id. Optional. Lives in the environment named by bot-environment; environment secrets are not carried by `secrets: inherit`.' required: false CI_APP_ID: - description: 'Legacy CI GitHub App id. Optional; used only when the CI bot app is not configured.' + description: 'Legacy CI GitHub App id. Optional; used by the changelog job only when the CI bot app is not configured.' required: false CI_APP_PRIVATE_KEY: - description: 'Legacy CI GitHub App private key. Optional; used only when the CI bot app is not configured.' + description: 'Legacy CI GitHub App private key. Optional; used by the changelog job only when the CI bot app is not configured.' required: false JF_CI_GH_PAT: description: 'Personal access token fallback for the changelog push and pull request. Optional; used only when no app credentials are configured.' @@ -423,104 +428,39 @@ jobs: always() && needs.detect-languages.outputs.has-go == 'true' && (inputs.build-target == 'all' || inputs.build-target == 'go') - # The CI bot app key lives in this environment (the caller limits it to its - # default branch), so a workflow on another branch cannot mint a token that - # bypasses the default branch's ruleset. Environment secrets reach only jobs - # that name the environment, and `secrets: inherit` does not carry them, so - # the job must declare it here. A caller without the environment gets one - # created empty on first use; it then keeps using its repository secrets. - environment: ci-bot steps: - # The app credentials are resolved and minted BEFORE checkout so checkout - # can take the app token. - # - # Which GitHub App credentials this job has. `if:` cannot read secrets.* in - # a workflow_call workflow ("Unrecognized named-value: 'secrets'"), so the - # presence of each is materialised through env first and the mint steps - # below are gated on this step's `mode` output: - # bot CI bot app (client id + CI_BOT_APP_PRIVATE_KEY). Preferred: the - # main-branch ruleset's merge-queue bypass names this app. - # legacy CI_APP_ID + CI_APP_PRIVATE_KEY, kept for consumers that have - # not moved to the bot app. - # none no app; callers without one keep working on their other tokens. - # A half-configured pair is an error, not "none": silently dropping to a - # token that cannot push hides the misconfiguration until a push fails. - # (The one exception is a pull_request run, where secrets are withheld.) - - name: Resolve CI app credentials - id: app-creds + - name: Checkout repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + submodules: recursive + fetch-depth: 0 + + # Mint a short-lived GitHub App installation token so the tag-push + # step inside release-go-action can push refs whose reachable + # commits touch .github/workflows/ — the default GITHUB_TOKEN + # can't. Skipped entirely when CI_APP_ID isn't configured so the + # job doesn't emit a noisy error; falls back to GITHUB_TOKEN. + # `if:` cannot reference secrets.* directly in a workflow_call workflow + # ("Unrecognized named-value: 'secrets'"). Materialize CI_APP_ID through + # env in a guard step, then gate the mint step on that step's output. + - name: Check for App credentials + id: check-app env: - BOT_CLIENT_ID: ${{ inputs.ci-bot-app-client-id || vars.CI_BOT_APP_CLIENT_ID }} - BOT_HAS_KEY: ${{ secrets.CI_BOT_APP_PRIVATE_KEY != '' }} - LEGACY_HAS_ID: ${{ secrets.CI_APP_ID != '' }} - LEGACY_HAS_KEY: ${{ secrets.CI_APP_PRIVATE_KEY != '' }} - EVENT_NAME: ${{ github.event_name }} + CI_APP_ID: ${{ secrets.CI_APP_ID }} run: | - set -euo pipefail - mode=none - if [ -n "$BOT_CLIENT_ID" ] && [ "$BOT_HAS_KEY" = true ]; then - mode=bot - elif [ "$BOT_HAS_KEY" = true ]; then - echo "::error::CI bot app is half-configured: CI_BOT_APP_PRIVATE_KEY is set but the client id (input ci-bot-app-client-id or variable CI_BOT_APP_CLIENT_ID) is empty." - exit 1 - elif [ -n "$BOT_CLIENT_ID" ]; then - # Repository variables reach every run but secrets are withheld from - # fork and Dependabot pull requests, so a missing key there is - # expected, not a misconfiguration. - case "$EVENT_NAME" in - pull_request|pull_request_target) - echo "::notice::CI bot app secret is not available to this pull request run; continuing without app credentials." ;; - *) - echo "::error::CI bot app is half-configured: the client id is set but the CI_BOT_APP_PRIVATE_KEY secret is empty." - exit 1 ;; - esac - fi - if [ "$mode" = none ]; then - if [ "$LEGACY_HAS_ID" = true ] && [ "$LEGACY_HAS_KEY" = true ]; then - mode=legacy - elif [ "$LEGACY_HAS_ID" = true ] || [ "$LEGACY_HAS_KEY" = true ]; then - echo "::error::Legacy CI app is half-configured: CI_APP_ID and CI_APP_PRIVATE_KEY must both be set." - exit 1 - fi + if [ -n "$CI_APP_ID" ]; then + echo "have-app=true" >> "$GITHUB_OUTPUT" + else + echo "have-app=false" >> "$GITHUB_OUTPUT" fi - echo "mode=${mode}" >> "$GITHUB_OUTPUT" - - name: Mint CI app token for the tag push - id: app-token-bot - if: steps.app-creds.outputs.mode == 'bot' - uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 - with: - client-id: ${{ inputs.ci-bot-app-client-id || vars.CI_BOT_APP_CLIENT_ID }} - private-key: ${{ secrets.CI_BOT_APP_PRIVATE_KEY }} - permission-contents: write - # The release tag points at a commit whose history can include - # .github/workflows/ changes; a push of such a ref is refused without it. - permission-workflows: write - - - name: Mint legacy CI app token for the tag push - id: app-token-legacy - if: steps.app-creds.outputs.mode == 'legacy' + - name: Mint App token for tag push + id: app-token + if: steps.check-app.outputs.have-app == 'true' uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 with: app-id: ${{ secrets.CI_APP_ID }} private-key: ${{ secrets.CI_APP_PRIVATE_KEY }} - permission-contents: write - # The release tag points at a commit whose history can include - # .github/workflows/ changes; a push of such a ref is refused without it. - permission-workflows: write - - # token: the app token when there is one (else GITHUB_TOKEN, as before). - # Checkout persists it as http.extraheader, and git sends that header in - # preference to credentials embedded in a push URL. gha-release-go pushes - # the tag to a URL carrying its github-token input (the same app token), so - # the two now agree and the tag really is pushed by the app. With - # the default checkout the persisted GITHUB_TOKEN won and the minted app - # token was never used. - - name: Checkout repository - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - submodules: recursive - fetch-depth: 0 - token: ${{ steps.app-token-bot.outputs.token || steps.app-token-legacy.outputs.token || github.token }} # goreleaser's `{{ .PreviousTag }}` walks back exactly one tag. On a repo # that cuts an RC per merge, that tag is almost always an RC, so release @@ -577,7 +517,7 @@ jobs: pre-build-script: ${{ inputs.pre-build-script }} go-experiment: ${{ inputs.go-experiment }} run-linters: ${{ inputs.go-run-linters }} - github-token: ${{ steps.app-token-bot.outputs.token || steps.app-token-legacy.outputs.token || secrets.GITHUB_TOKEN }} + github-token: ${{ steps.app-token.outputs.token || secrets.GITHUB_TOKEN }} # Python Build build-python: @@ -1264,13 +1204,13 @@ jobs: github.event_name != 'pull_request' && inputs.changelog-collect != 'false' && needs.detect-languages.outputs.auto-prerelease != 'true' - # The CI bot app key lives in this environment (the caller limits it to its - # default branch), so a workflow on another branch cannot mint a token that - # bypasses the default branch's ruleset. Environment secrets reach only jobs - # that name the environment, and `secrets: inherit` does not carry them, so - # the job must declare it here. A caller without the environment gets one - # created empty on first use; it then keeps using its repository secrets. - environment: ci-bot + # Opt-in. A caller that owns the CI bot app key passes bot-environment (for + # example ci-bot) and holds the key as that environment's secret, limited to + # its default branch, so a workflow on another branch cannot mint a token that + # bypasses the ruleset. Environment secrets are not carried by + # `secrets: inherit`; the job must name the environment. Empty means no + # environment, which is every other consumer. + environment: ${{ inputs.bot-environment }} steps: # The released commit (github.sha), so exactly the fragments that shipped # in this version are collected; fragments merged after it stay pending. @@ -1325,23 +1265,23 @@ jobs: # a workflow_call workflow ("Unrecognized named-value: 'secrets'"), so the # presence of each is materialised through env first and the mint steps # below are gated on this step's `mode` output: - # bot CI bot app (client id + CI_BOT_APP_PRIVATE_KEY). Preferred: the - # main-branch ruleset's merge-queue bypass names this app. + # bot CI bot app (client id + CI_BOT_APP_PRIVATE_KEY, from the + # environment named by bot-environment). # legacy CI_APP_ID + CI_APP_PRIVATE_KEY, kept for consumers that have # not moved to the bot app. # none no app; callers without one keep working on their other tokens. - # A half-configured pair is an error, not "none": silently dropping to a - # token that cannot push hides the misconfiguration until a push fails. - # (The one exception is a pull_request run, where secrets are withheld.) + # Outside a bot-environment the key is never visible, so a client id that + # is set without one (an org or repository variable reaches every repo) is + # just "none", with a warning. Inside one, a missing key is a mistake. - name: Resolve CI app credentials id: app-creds if: steps.plan.outputs.collect == 'true' env: BOT_CLIENT_ID: ${{ inputs.ci-bot-app-client-id || vars.CI_BOT_APP_CLIENT_ID }} BOT_HAS_KEY: ${{ secrets.CI_BOT_APP_PRIVATE_KEY != '' }} + BOT_ENV: ${{ inputs.bot-environment }} LEGACY_HAS_ID: ${{ secrets.CI_APP_ID != '' }} LEGACY_HAS_KEY: ${{ secrets.CI_APP_PRIVATE_KEY != '' }} - EVENT_NAME: ${{ github.event_name }} run: | set -euo pipefail mode=none @@ -1351,16 +1291,11 @@ jobs: echo "::error::CI bot app is half-configured: CI_BOT_APP_PRIVATE_KEY is set but the client id (input ci-bot-app-client-id or variable CI_BOT_APP_CLIENT_ID) is empty." exit 1 elif [ -n "$BOT_CLIENT_ID" ]; then - # Repository variables reach every run but secrets are withheld from - # fork and Dependabot pull requests, so a missing key there is - # expected, not a misconfiguration. - case "$EVENT_NAME" in - pull_request|pull_request_target) - echo "::notice::CI bot app secret is not available to this pull request run; continuing without app credentials." ;; - *) - echo "::error::CI bot app is half-configured: the client id is set but the CI_BOT_APP_PRIVATE_KEY secret is empty." - exit 1 ;; - esac + if [ -n "$BOT_ENV" ]; then + echo "::error::bot-environment is '${BOT_ENV}' and the client id is set, but the CI_BOT_APP_PRIVATE_KEY secret is empty (is it an environment secret of '${BOT_ENV}'?)." + exit 1 + fi + echo "::warning::CI bot app client id is set but no CI_BOT_APP_PRIVATE_KEY is visible (no bot-environment given); not using the bot app." fi if [ "$mode" = none ]; then if [ "$LEGACY_HAS_ID" = true ] && [ "$LEGACY_HAS_KEY" = true ]; then @@ -1392,15 +1327,14 @@ jobs: permission-contents: write permission-pull-requests: write - - name: Collect changelog fragments + # Needs a token only to list pull requests, and runs nothing but `gh`. The + # step that installs and runs scriv (below) gets no token at all. + - name: Refuse while an earlier changelog pull request is open if: steps.plan.outputs.collect == 'true' env: VERSION: ${{ needs.create-release.outputs.version }} - DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} CHANGELOG_PAT: ${{ secrets.JF_CI_GH_PAT }} APP_TOKEN: ${{ steps.app-token-bot.outputs.token || steps.app-token-legacy.outputs.token }} - APP_SLUG: ${{ steps.app-token-bot.outputs.app-slug || steps.app-token-legacy.outputs.app-slug }} - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | set -euo pipefail BRANCH="changelog/collect-${VERSION}" @@ -1426,23 +1360,29 @@ jobs: fi fi - python3 -m pip install --quiet scriv + # NO credentials in this step's environment, on purpose: it installs a + # package from PyPI and runs scriv, which executes code from outside this + # repository. The tokens are only given to the publish step below, which + # runs nothing but git and gh. The step's output is a local commit that the + # publish step verifies before pushing. + # + # scriv is pinned to an exact version. Its transitive dependencies are not + # hash-pinned: that needs about 375 lines of hashes inline in this file for + # 13 packages, and the step holds no token to steal. + - name: Collect changelog fragments + id: collect + if: steps.plan.outputs.collect == 'true' + env: + VERSION: ${{ needs.create-release.outputs.version }} + run: | + set -euo pipefail - # Commit as the app's bot account when there is one; the user id is - # looked up, never hard-coded, so renaming the app needs no edit here. - # The identity is cosmetic: the token that pushes decides who bypasses. - GIT_NAME="github-actions[bot]" - GIT_EMAIL="github-actions[bot]@users.noreply.github.com" - if [ -n "${APP_TOKEN:-}" ] && [ -n "${APP_SLUG:-}" ]; then - if bot_id="$(GH_TOKEN="$APP_TOKEN" gh api "users/${APP_SLUG}%5Bbot%5D" --jq .id)" && [[ "$bot_id" =~ ^[0-9]+$ ]]; then - GIT_NAME="${APP_SLUG}[bot]" - GIT_EMAIL="${bot_id}+${APP_SLUG}[bot]@users.noreply.github.com" - else - echo "::warning::Could not resolve the user id of ${APP_SLUG}[bot]; committing as github-actions[bot]." - fi - fi - git config user.name "$GIT_NAME" - git config user.email "$GIT_EMAIL" + python3 -m pip install --quiet 'scriv==1.8.0' + + # Placeholder identity; the publish step rewrites the author to the + # app's bot account when there is one. + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" python3 -m scriv collect --version "${VERSION}" @@ -1464,6 +1404,50 @@ jobs: echo "::error::The changelog commit touches files outside CHANGELOG.md and changelog.d/: ${other}" exit 1 fi + echo "commit=${COMMIT}" >> "$GITHUB_OUTPUT" + + - name: Publish changelog commit + if: steps.plan.outputs.collect == 'true' && steps.collect.outputs.commit != '' + env: + VERSION: ${{ needs.create-release.outputs.version }} + DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} + COMMIT: ${{ steps.collect.outputs.commit }} + CHANGELOG_PAT: ${{ secrets.JF_CI_GH_PAT }} + APP_TOKEN: ${{ steps.app-token-bot.outputs.token || steps.app-token-legacy.outputs.token }} + APP_SLUG: ${{ steps.app-token-bot.outputs.app-slug || steps.app-token-legacy.outputs.app-slug }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + set -euo pipefail + BRANCH="changelog/collect-${VERSION}" + + # Re-check what is about to travel: it must still be exactly the one + # commit made above, touching only the changelog. + if [ "$(git rev-parse HEAD)" != "$COMMIT" ]; then + echo "::error::HEAD is not the changelog commit made by the previous step." + exit 1 + fi + other="$(git diff --name-only "${COMMIT}^" "${COMMIT}" | grep -vE '^(CHANGELOG\.md|changelog\.d/)' || true)" + if [ -n "$other" ]; then + echo "::error::The changelog commit touches files outside CHANGELOG.md and changelog.d/: ${other}" + exit 1 + fi + + # Author the commit as the app's bot account when there is one; the + # user id is looked up, never hard-coded, so renaming the app needs no + # edit here. The identity is cosmetic: the token that pushes decides who + # bypasses. A failed lookup only costs the nicer attribution. + GIT_NAME="github-actions[bot]" + GIT_EMAIL="github-actions[bot]@users.noreply.github.com" + if [ -n "${APP_TOKEN:-}" ] && [ -n "${APP_SLUG:-}" ]; then + if bot_id="$(GH_TOKEN="$APP_TOKEN" gh api "users/${APP_SLUG}%5Bbot%5D" --jq .id)" && [[ "$bot_id" =~ ^[0-9]+$ ]]; then + GIT_NAME="${APP_SLUG}[bot]" + GIT_EMAIL="${bot_id}+${APP_SLUG}[bot]@users.noreply.github.com" + else + echo "::warning::Could not resolve the user id of ${APP_SLUG}[bot]; committing as github-actions[bot]." + fi + fi + git config user.name "$GIT_NAME" + git config user.email "$GIT_EMAIL" # Put the one changelog commit on a fresh copy of the default branch's # tip. Never push the checkout's own history. @@ -1471,6 +1455,8 @@ jobs: git fetch --quiet "https://x-access-token:${1}@github.com/${GITHUB_REPOSITORY}.git" "${DEFAULT_BRANCH}" git checkout --quiet --detach FETCH_HEAD git cherry-pick "${COMMIT}" + # cherry-pick keeps the original author; take the identity set above. + git commit --quiet --amend --reset-author --no-edit } # 1. Direct push, for repositories that allow it. Prefer the app: its diff --git a/changelog.d/ci-bot-app-auth.md b/changelog.d/ci-bot-app-auth.md index 20149731..713fd733 100644 --- a/changelog.d/ci-bot-app-auth.md +++ b/changelog.d/ci-bot-app-auth.md @@ -1,29 +1,32 @@ ### Changed -#### `reusable-release.yml` — release tag push and changelog commit can run as the CI bot GitHub App +#### `reusable-release.yml` `collect-changelog` — direct push tries the CI bot app first -`build-go` (release tag push) and `collect-changelog` (changelog commit) now -authenticate as the CI bot GitHub App when the caller provides one. A new +The changelog commit can now be pushed as the CI bot GitHub App. The direct push +to the default branch prefers the app token, then the PAT, then `GITHUB_TOKEN`. +Previously the app was used only for the fallback pull request, so a ruleset +bypass naming the app never helped and every collect fell through to the pull +request. The commit carries the app's bot identity, resolved at runtime. New optional input `ci-bot-app-client-id` (falling back to the caller's `CI_BOT_APP_CLIENT_ID` variable) and optional secret `CI_BOT_APP_PRIVATE_KEY` -select it; the legacy `CI_APP_ID` / `CI_APP_PRIVATE_KEY` pair keeps working for -consumers that have not moved, and a caller with neither behaves as before. Both -jobs declare `environment: ci-bot`, so the key can be held as an environment -secret limited to the default branch. A half-configured pair is an error, -except on `pull_request` runs where secrets are withheld. Both mints are -narrowed with `permission-*` inputs. +select the bot app; the legacy `CI_APP_ID` / `CI_APP_PRIVATE_KEY` pair keeps +working, and a caller with neither behaves as before. The legacy secrets and +`JF_CI_GH_PAT` are now declared as optional `workflow_call` secrets, which +declaring any secret requires. -#### `reusable-release.yml` `build-go` — the app token is now actually used for the tag push +#### `reusable-release.yml` — the bot app key is opt-in through a caller environment -The token was minted but never used: checkout persisted `GITHUB_TOKEN` as -`http.extraheader`, and git sends that header in preference to the token -embedded in the push URL. Checkout now takes the app token, so the persisted -credential and the tag push agree. +New input `bot-environment` (default empty). The `collect-changelog` job runs in +the environment it names, so only a caller that holds the key as that +environment's secret can use the bot app; every other consumer runs outside any +environment and is unchanged. Outside an environment a client id without a key +is treated as "no app" with a warning, not an error. `build-go` is deliberately +untouched: it keeps the default `GITHUB_TOKEN` checkout. -#### `reusable-release.yml` `collect-changelog` — direct push tries the app first +#### `reusable-release.yml` `collect-changelog` — scriv runs without credentials -The direct push to the default branch now prefers the app token, then the PAT, -then `GITHUB_TOKEN`. Previously the app was used only for the fallback pull -request, so a ruleset bypass naming the app never helped and every collect fell -through to the pull request. The commit carries the app's bot identity, resolved -at runtime. +The job is split so the step that installs and runs scriv (pinned to `1.8.0`) +has no token in its environment. The app token, PAT and `GITHUB_TOKEN` reach +only a separate step that re-verifies the single changelog commit (only +`CHANGELOG.md` and `changelog.d/` touched) and then pushes or opens the pull +request. From 5a2317f6e76edd264249f82edeee2052f9111a88 Mon Sep 17 00:00:00 2001 From: Johnathan Falk Date: Fri, 6 Nov 2026 21:28:27 -0500 Subject: [PATCH 3/3] ci(release): isolate scriv in its own job from the app token Separate steps of one job share $GITHUB_ENV, $GITHUB_PATH, git hooks and config, so a malicious scriv dependency could reach the publish step's token. collect-changelog becomes collect-changelog-build (no environment, no secrets, contents: read; installs scriv==1.8.0, collects, uploads a patch) and collect-changelog-publish (bot-environment, installs nothing; fresh checkout, verifies the patch touches only CHANGELOG.md and changelog.d/ as plain regular files, applies it with hooks disabled, commits as the app, pushes or opens the PR). build-status needs both. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_011To9HQJLrCH6wYa5WCpw3L --- .github/guid-index.json | 2 +- .github/workflows/reusable-release.yml | 225 ++++++++++++++++--------- changelog.d/ci-bot-app-auth.md | 19 ++- 3 files changed, 162 insertions(+), 84 deletions(-) diff --git a/.github/guid-index.json b/.github/guid-index.json index 11163839..30144eaf 100644 --- a/.github/guid-index.json +++ b/.github/guid-index.json @@ -1003,7 +1003,7 @@ "path": ".github/workflows/reusable-release.yml", "guid": "7b8c9d0e-1f2a-3b4c-5d6e-7f8a9b0c1d2e", "title": "Reusable Release Coordinator", - "version": "2.18.1", + "version": "2.19.0", "header_path": ".github/workflows/reusable-release.yml", "path_mismatch": false }, diff --git a/.github/workflows/reusable-release.yml b/.github/workflows/reusable-release.yml index 00dee5fb..4c80ddbe 100644 --- a/.github/workflows/reusable-release.yml +++ b/.github/workflows/reusable-release.yml @@ -1,5 +1,5 @@ # file: .github/workflows/reusable-release.yml -# version: 2.18.1 +# version: 2.19.0 # guid: 7b8c9d0e-1f2a-3b4c-5d6e-7f8a9b0c1d2e # last-edited: 2026-11-06 @@ -1193,8 +1193,15 @@ jobs: # and a job-level `if:` without a status function gets an implicit # `success()` that would skip this job too; `!cancelled()` avoids that and # still honours a cancelled run. - collect-changelog: - name: Collect Changelog Fragments + # Changelog collection is two jobs on purpose. scriv and its dependencies come + # from PyPI, and a malicious one could tamper with $GITHUB_ENV, $GITHUB_PATH, + # git hooks or config, or plant a git/gh wrapper. Separate STEPS of one job + # share all of that, so the only real isolation is a separate job (a fresh + # runner): this one has no secrets, no environment and a read-only token, and + # hands over nothing but a patch. The publish job below treats that patch as + # untrusted data, verifies it and installs nothing. + collect-changelog-build: + name: Build Changelog Patch runs-on: ubuntu-latest timeout-minutes: 15 needs: [detect-languages, create-release] @@ -1204,13 +1211,10 @@ jobs: github.event_name != 'pull_request' && inputs.changelog-collect != 'false' && needs.detect-languages.outputs.auto-prerelease != 'true' - # Opt-in. A caller that owns the CI bot app key passes bot-environment (for - # example ci-bot) and holds the key as that environment's secret, limited to - # its default branch, so a workflow on another branch cannot mint a token that - # bypasses the ruleset. Environment secrets are not carried by - # `secrets: inherit`; the job must name the environment. Empty means no - # environment, which is every other consumer. - environment: ${{ inputs.bot-environment }} + permissions: + contents: read + outputs: + publish: ${{ steps.pack.outputs.publish }} steps: # The released commit (github.sha), so exactly the fragments that shipped # in this version are collected; fragments merged after it stay pending. @@ -1255,6 +1259,130 @@ jobs: fi echo "collect=${collect}" >> "$GITHUB_OUTPUT" + # scriv is pinned to an exact version. Its transitive dependencies are not + # hash-pinned: that needs about 375 lines of hashes inline in this file for + # 13 packages, and this job holds no secret to steal. + - name: Collect changelog fragments + id: collect + if: steps.plan.outputs.collect == 'true' + env: + VERSION: ${{ needs.create-release.outputs.version }} + run: | + set -euo pipefail + python3 -m pip install --quiet 'scriv==1.8.0' + python3 -m scriv collect --version "${VERSION}" + + # The only thing that leaves this job: a binary-safe diff of the working + # tree against the released commit, restricted by pathspec to the two paths + # that may change. The publish job re-verifies it and does not trust this. + - name: Package the changelog patch + id: pack + if: steps.plan.outputs.collect == 'true' + run: | + set -euo pipefail + git add -A -- CHANGELOG.md changelog.d + if git diff --cached --quiet; then + echo "::notice::scriv produced no CHANGELOG.md changes." + echo "publish=false" >> "$GITHUB_OUTPUT" + exit 0 + fi + mkdir -p "${RUNNER_TEMP}/changelog-patch" + git diff --cached --binary --full-index -- CHANGELOG.md changelog.d > "${RUNNER_TEMP}/changelog-patch/changelog.patch" + echo "publish=true" >> "$GITHUB_OUTPUT" + + - name: Upload the changelog patch + if: steps.pack.outputs.publish == 'true' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: changelog-patch + path: ${{ runner.temp }}/changelog-patch/changelog.patch + if-no-files-found: error + retention-days: 1 + + # Applies the patch built above and pushes it. Nothing is installed here + # (no pip, no npm): the only code that runs is this workflow, git, gh and the + # pinned actions, so nothing from the build job can reach the app token. + collect-changelog-publish: + name: Publish Changelog + runs-on: ubuntu-latest + timeout-minutes: 15 + needs: [detect-languages, create-release, collect-changelog-build] + if: >- + !cancelled() && + needs.collect-changelog-build.result == 'success' && + needs.collect-changelog-build.outputs.publish == 'true' + # The job writes the default branch (or opens a PR) with GITHUB_TOKEN when no + # app is configured; nothing else from the workflow-level grant is needed. + permissions: + contents: write + # Opt-in. A caller that owns the CI bot app key passes bot-environment (for + # example ci-bot) and holds the key as that environment's secret, limited to + # its default branch, so a workflow on another branch cannot mint a token that + # bypasses the ruleset. Environment secrets are not carried by + # `secrets: inherit`; the job must name the environment. Empty means no + # environment, which is every other consumer. + environment: ${{ inputs.bot-environment }} + steps: + # The released commit again (github.sha): the patch was built against it. + - name: Checkout repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + persist-credentials: false + + - name: Download the changelog patch + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: changelog-patch + path: ${{ runner.temp }}/changelog-patch + + # The patch comes from a job that ran third-party code, so it is data, not + # a trusted input. Apply it to the index and judge the RESULT with git's own + # raw diff, which cannot be spoofed by patch syntax: every entry must be an + # added, modified or deleted regular file (mode 100644) at CHANGELOG.md or + # under changelog.d/. That rejects renames and copies (shown as delete + + # add, so a move to or from another path fails the path test), mode + # changes, symlinks and submodules. Hooks are disabled in this checkout. + - name: Verify and apply the changelog patch + run: | + set -euo pipefail + git config --local core.hooksPath /dev/null + PATCH="${RUNNER_TEMP}/changelog-patch/changelog.patch" + [ -s "$PATCH" ] || { echo "::error::The changelog patch is missing or empty."; exit 1; } + + git -c core.hooksPath=/dev/null apply --index "$PATCH" + + if git diff --cached --quiet; then + echo "::error::The changelog patch changes nothing." + exit 1 + fi + if [ -n "$(git diff --name-only)" ] || [ -n "$(git ls-files --others --exclude-standard)" ]; then + echo "::error::Applying the changelog patch left unstaged or untracked changes." + exit 1 + fi + + bad="" + while IFS= read -r -d '' meta && IFS= read -r -d '' path; do + # meta: ": " + read -r old_mode new_mode _ _ status <<< "${meta#:}" + case "$path" in + CHANGELOG.md|changelog.d/*) ;; + *) bad+=$'\n'" outside the changelog paths: ${path}"; continue ;; + esac + case "$status" in + A) [ "$new_mode" = 100644 ] || bad+=$'\n'" not a regular file: ${path} (${new_mode})" ;; + M) { [ "$old_mode" = 100644 ] && [ "$new_mode" = 100644 ]; } || bad+=$'\n'" mode change: ${path} (${old_mode} to ${new_mode})" ;; + D) [ "$old_mode" = 100644 ] || bad+=$'\n'" not a regular file: ${path} (${old_mode})" ;; + *) bad+=$'\n'" unexpected change '${status}': ${path}" ;; + esac + done < <(git diff --cached --raw -z --no-renames) + if [ -n "$bad" ]; then + echo "::error::Rejecting the changelog patch:${bad}" + exit 1 + fi + echo "Changelog patch verified:" + git diff --cached --stat + # The token pushes the changelog commit straight to the default branch and, # if that is refused, opens the fallback pull request. It must be the app's: # the default branch's ruleset lists the CI bot app as its bypass actor, and @@ -1275,7 +1403,6 @@ jobs: # just "none", with a warning. Inside one, a missing key is a mistake. - name: Resolve CI app credentials id: app-creds - if: steps.plan.outputs.collect == 'true' env: BOT_CLIENT_ID: ${{ inputs.ci-bot-app-client-id || vars.CI_BOT_APP_CLIENT_ID }} BOT_HAS_KEY: ${{ secrets.CI_BOT_APP_PRIVATE_KEY != '' }} @@ -1309,7 +1436,7 @@ jobs: - name: Mint CI app token for the changelog commit id: app-token-bot - if: steps.plan.outputs.collect == 'true' && steps.app-creds.outputs.mode == 'bot' + if: steps.app-creds.outputs.mode == 'bot' uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 with: client-id: ${{ inputs.ci-bot-app-client-id || vars.CI_BOT_APP_CLIENT_ID }} @@ -1319,7 +1446,7 @@ jobs: - name: Mint legacy CI app token for the changelog commit id: app-token-legacy - if: steps.plan.outputs.collect == 'true' && steps.app-creds.outputs.mode == 'legacy' + if: steps.app-creds.outputs.mode == 'legacy' uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 with: app-id: ${{ secrets.CI_APP_ID }} @@ -1327,10 +1454,8 @@ jobs: permission-contents: write permission-pull-requests: write - # Needs a token only to list pull requests, and runs nothing but `gh`. The - # step that installs and runs scriv (below) gets no token at all. + # Needs a token only to list pull requests, and runs nothing but `gh`. - name: Refuse while an earlier changelog pull request is open - if: steps.plan.outputs.collect == 'true' env: VERSION: ${{ needs.create-release.outputs.version }} CHANGELOG_PAT: ${{ secrets.JF_CI_GH_PAT }} @@ -1360,58 +1485,10 @@ jobs: fi fi - # NO credentials in this step's environment, on purpose: it installs a - # package from PyPI and runs scriv, which executes code from outside this - # repository. The tokens are only given to the publish step below, which - # runs nothing but git and gh. The step's output is a local commit that the - # publish step verifies before pushing. - # - # scriv is pinned to an exact version. Its transitive dependencies are not - # hash-pinned: that needs about 375 lines of hashes inline in this file for - # 13 packages, and the step holds no token to steal. - - name: Collect changelog fragments - id: collect - if: steps.plan.outputs.collect == 'true' - env: - VERSION: ${{ needs.create-release.outputs.version }} - run: | - set -euo pipefail - - python3 -m pip install --quiet 'scriv==1.8.0' - - # Placeholder identity; the publish step rewrites the author to the - # app's bot account when there is one. - git config user.name "github-actions[bot]" - git config user.email "github-actions[bot]@users.noreply.github.com" - - python3 -m scriv collect --version "${VERSION}" - - if git diff --quiet && git diff --cached --quiet; then - echo "::notice::scriv produced no CHANGELOG.md changes." - exit 0 - fi - - git add -A CHANGELOG.md changelog.d - # [skip ci] stops a directly pushed changelog commit from retriggering - # release CI. The pull-request path below removes it. - git commit -m "docs(changelog): collect fragments for ${VERSION} [skip ci]" - COMMIT="$(git rev-parse HEAD)" - - # Only the changelog may travel. Anything else means scriv or the - # checkout did something unexpected; never push it. - other="$(git diff --name-only "${COMMIT}^" "${COMMIT}" | grep -vE '^(CHANGELOG\.md|changelog\.d/)' || true)" - if [ -n "$other" ]; then - echo "::error::The changelog commit touches files outside CHANGELOG.md and changelog.d/: ${other}" - exit 1 - fi - echo "commit=${COMMIT}" >> "$GITHUB_OUTPUT" - - name: Publish changelog commit - if: steps.plan.outputs.collect == 'true' && steps.collect.outputs.commit != '' env: VERSION: ${{ needs.create-release.outputs.version }} DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} - COMMIT: ${{ steps.collect.outputs.commit }} CHANGELOG_PAT: ${{ secrets.JF_CI_GH_PAT }} APP_TOKEN: ${{ steps.app-token-bot.outputs.token || steps.app-token-legacy.outputs.token }} APP_SLUG: ${{ steps.app-token-bot.outputs.app-slug || steps.app-token-legacy.outputs.app-slug }} @@ -1420,18 +1497,6 @@ jobs: set -euo pipefail BRANCH="changelog/collect-${VERSION}" - # Re-check what is about to travel: it must still be exactly the one - # commit made above, touching only the changelog. - if [ "$(git rev-parse HEAD)" != "$COMMIT" ]; then - echo "::error::HEAD is not the changelog commit made by the previous step." - exit 1 - fi - other="$(git diff --name-only "${COMMIT}^" "${COMMIT}" | grep -vE '^(CHANGELOG\.md|changelog\.d/)' || true)" - if [ -n "$other" ]; then - echo "::error::The changelog commit touches files outside CHANGELOG.md and changelog.d/: ${other}" - exit 1 - fi - # Author the commit as the app's bot account when there is one; the # user id is looked up, never hard-coded, so renaming the app needs no # edit here. The identity is cosmetic: the token that pushes decides who @@ -1449,6 +1514,11 @@ jobs: git config user.name "$GIT_NAME" git config user.email "$GIT_EMAIL" + # [skip ci] stops a directly pushed changelog commit from retriggering + # release CI. The pull-request path below removes it. + git -c core.hooksPath=/dev/null commit -q -m "docs(changelog): collect fragments for ${VERSION} [skip ci]" + COMMIT="$(git rev-parse HEAD)" + # Put the one changelog commit on a fresh copy of the default branch's # tip. Never push the checkout's own history. land_on_tip() { @@ -1574,7 +1644,8 @@ jobs: create-release, publish-github-packages, update-floating-tags, - collect-changelog, + collect-changelog-build, + collect-changelog-publish, ] if: always() outputs: diff --git a/changelog.d/ci-bot-app-auth.md b/changelog.d/ci-bot-app-auth.md index 713fd733..3f0bfd55 100644 --- a/changelog.d/ci-bot-app-auth.md +++ b/changelog.d/ci-bot-app-auth.md @@ -23,10 +23,17 @@ environment and is unchanged. Outside an environment a client id without a key is treated as "no app" with a warning, not an error. `build-go` is deliberately untouched: it keeps the default `GITHUB_TOKEN` checkout. -#### `reusable-release.yml` `collect-changelog` — scriv runs without credentials +#### `reusable-release.yml` — changelog collection is split into a build job and a publish job -The job is split so the step that installs and runs scriv (pinned to `1.8.0`) -has no token in its environment. The app token, PAT and `GITHUB_TOKEN` reach -only a separate step that re-verifies the single changelog commit (only -`CHANGELOG.md` and `changelog.d/` touched) and then pushes or opens the pull -request. +`collect-changelog` is replaced by `collect-changelog-build` and +`collect-changelog-publish`. The build job has no environment, no secrets and a +read-only token; it installs `scriv==1.8.0`, collects, and uploads a patch +limited to `CHANGELOG.md` and `changelog.d/`. The publish job, which declares +the `bot-environment` and holds the tokens, installs nothing: it checks out +afresh, downloads the patch, applies it with hooks disabled, and refuses +anything but added, modified or deleted regular files under those paths +(renames, mode changes, symlinks and submodules are rejected) before it commits +as the app and pushes or opens the pull request. Separate steps of one job share +`$GITHUB_ENV`, `$GITHUB_PATH` and git config, so a malicious dependency in an +earlier step could reach a later step's token; separate jobs run on separate +runners.