diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000000..eefb2f75e1 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,7 @@ +# Release and signing paths need the release owner's review. The branch ruleset on main and 0.22 +# requires code-owner review and no other approvals, so other changes merge as before. +# Target: jobs that hold release secrets run only these paths. Until the Android helper signing +# split lands, the Build job in release.yml is the known exception. See docs/agents/pull-requests.md. +/.github/ @thymikee +/scripts/release-* @thymikee +/scripts/*android-helper* @thymikee diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 91d202235c..d4f2d515ad 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -330,42 +330,135 @@ jobs: DEPLOY_HOOK: ${{ secrets.VERCEL_SITE_DEPLOY_HOOK }} run: curl -fsS -X POST "$DEPLOY_HOOK" > /dev/null - mark-released: - name: Move main past ${{ needs.plan.outputs.version }} + prepare-next-version: + name: Prepare the version after ${{ needs.plan.outputs.version }} needs: [plan, publish] if: needs.plan.outputs.mode == 'stable' runs-on: blacksmith-4vcpu-ubuntu-2404 timeout-minutes: 10 permissions: - contents: write + contents: read + outputs: + artifact_id: ${{ steps.upload.outputs.artifact-id }} + next: ${{ steps.advance.outputs.next }} steps: - name: Checkout main uses: useblacksmith/checkout@2b9fe87d796feea153b455ec2f3125b471a7b0e7 # v1.8.2 with: ref: main + persist-credentials: false - name: Setup toolchain uses: ./.github/actions/setup-node-pnpm - # main must never carry a published version. Retried because merges can - # land on main while the release runs. + - name: Compute the next development version + id: advance + env: + VERSION: ${{ needs.plan.outputs.version }} + run: | + set -euo pipefail + next="$(node scripts/release-version.mjs advance "$VERSION")" + printf 'next=%s\n' "$next" >> "$GITHUB_OUTPUT" + git diff -U0 --no-color --no-ext-diff > "$RUNNER_TEMP/next-version.patch" + + - name: Upload the version patch + id: upload + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: next-version-${{ needs.plan.outputs.version }} + path: ${{ runner.temp }}/next-version.patch + # The push job may wait for an environment approval or a rerun. + retention-days: 30 + + # main must never carry a published version. The ruleset on main lets only admins and deploy + # keys push without a PR. The push key is a deploy key that only the `release` environment holds, + # so this job runs no repository code: it applies the patch only after it proves that the patch + # changes version strings and nothing else. + mark-released: + name: Move main past ${{ needs.plan.outputs.version }} + needs: [plan, prepare-next-version] + runs-on: ubuntu-latest + timeout-minutes: 10 + environment: release + permissions: + contents: read + steps: + - name: Checkout main + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + with: + ref: main + ssh-key: ${{ secrets.RELEASE_PUSH_DEPLOY_KEY }} + + - name: Require the patch artifact ID + env: + ARTIFACT_ID: ${{ needs.prepare-next-version.outputs.artifact_id }} + run: '[[ "$ARTIFACT_ID" =~ ^[0-9]+$ ]]' + + - name: Download the version patch + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 + with: + artifact-ids: ${{ needs.prepare-next-version.outputs.artifact_id }} + path: ${{ runner.temp }}/next-version + merge-multiple: true + + - name: Require a patch that changes only version strings + env: + NEXT: ${{ needs.prepare-next-version.outputs.next }} + PATCH_FILE: ${{ runner.temp }}/next-version/next-version.patch + run: | + set -euo pipefail + [[ "$NEXT" =~ ^[0-9]+\.[0-9]+\.[0-9]+-dev$ ]] || { echo "Unexpected next version: $NEXT"; exit 1; } + awk -v next_version="$NEXT" ' + function fail(why) { print "Rejected version patch line " NR ": " why > "/dev/stderr"; bad = 1; exit 1 } + function allowed(file) { return file ~ /^(package\.json|server\.json|packages\/[a-z0-9-]+\/package\.json)$/ } + /^diff --git / { + file = substr($4, 3) + if (NF != 4 || $3 != "a/" file || !allowed(file)) fail("unexpected file") + next + } + /^index [0-9a-f]+\.\.[0-9a-f]+ 100644$/ { next } + /^--- a\// { if ($0 != "--- a/" file) fail("unexpected file"); next } + /^\+\+\+ b\// { if ($0 != "+++ b/" file) fail("unexpected file"); next } + /^@@ -[0-9]+(,1)? \+[0-9]+(,1)? @@/ { next } + /^-[ \t]*"version": "[^"]+",?$/ { next } + /^\+[ \t]*"version": "[^"]+",?$/ { + if (index($0, "\"version\": \"" next_version "\"") == 0) fail("unexpected version") + next + } + { fail("unexpected content") } + ' "$PATCH_FILE" + - name: Commit the next development version env: VERSION: ${{ needs.plan.outputs.version }} + NEXT: ${{ needs.prepare-next-version.outputs.next }} + PATCH_FILE: ${{ runner.temp }}/next-version/next-version.patch run: | set -euo pipefail + if [ ! -s "$PATCH_FILE" ]; then + echo "main already builds towards $NEXT." + exit 0 + fi git config user.name 'github-actions[bot]' git config user.email '41898282+github-actions[bot]@users.noreply.github.com' + # Retried because merges can land on main while the release runs. for attempt in 1 2 3; do git fetch origin main git reset --hard origin/main - next="$(node scripts/release-version.mjs advance "$VERSION")" - if git diff --quiet; then - echo "main already builds towards $next." - exit 0 + if ! git apply --unidiff-zero "$PATCH_FILE"; then + # Another release or a manual commit can move main first. + current="$(node -e 'process.stdout.write(JSON.parse(require("fs").readFileSync("package.json", "utf8")).version ?? "")')" + if [ -n "$current" ] && [ "$current" != "$VERSION-dev" ]; then + echo "main already moved past $VERSION to $current." + exit 0 + fi + echo "The version patch does not apply to main at $current." + exit 1 fi git add -u - git commit -m "chore: mark $VERSION as released" + # A deploy-key push starts workflows. Without a CI run, the release planner reads a + # version-only head's CI from its parent, as it did for GITHUB_TOKEN pushes. + git commit -m "chore: mark $VERSION as released [skip ci]" if git push origin HEAD:main; then exit 0 fi diff --git a/docs/agents/pull-requests.md b/docs/agents/pull-requests.md index b45736e0f5..a9a9482fde 100644 --- a/docs/agents/pull-requests.md +++ b/docs/agents/pull-requests.md @@ -64,3 +64,8 @@ Update the body after each push. Open as draft only when requested or incomplete - `## Validation`: the tested SHA and receipt, command results, CI obligations, and unresolved risks. Use evidence accessible remotely. For docs-only changes, explain why runtime tests do not apply. + +## Release and signing changes + +A job that holds a release secret runs only paths that `.github/CODEOWNERS` owns. +The release owner merges their own owned-path PR with the admin bypass and records the reason. diff --git a/test/ci/release-workflow.test.ts b/test/ci/release-workflow.test.ts index d8e78c3efd..369aed807f 100644 --- a/test/ci/release-workflow.test.ts +++ b/test/ci/release-workflow.test.ts @@ -66,6 +66,7 @@ test('the publisher authenticates through OIDC alone', () => { expect(withSecrets).toEqual([ ['build', ['ANDROID_HELPER_KEYSTORE_BASE64', 'ANDROID_HELPER_KEYSTORE_PASSWORD']], ['redeploy-site', ['VERCEL_SITE_DEPLOY_HOOK']], + ['mark-released', ['RELEASE_PUSH_DEPLOY_KEY']], ]); const setupNode = workflow.jobs.publish?.steps?.find((step) => step.uses?.startsWith('actions/setup-node@'), @@ -103,6 +104,21 @@ test('the Android signing rehearsal validates version changes without release cr expect(rehearsal.jobs['package-helpers']?.needs).toEqual(['compile-helpers', 'sign-helpers']); }); +// The deploy key bypasses the branch ruleset on main, so its job runs no repository code: it +// checks out main, validates the patch from the unprivileged job, and pushes. +test('the release push key reaches only a job that runs no repository code', () => { + const job = workflow.jobs['mark-released']; + expect(job?.environment).toBe('release'); + expect(job?.needs).toEqual(['plan', 'prepare-next-version']); + const steps = job?.steps ?? []; + expect(steps.filter((step) => step.uses?.startsWith('./'))).toEqual([]); + expect(JSON.stringify(steps)).not.toMatch(/pnpm|scripts\//); + // An ID download without merge-multiple nests the file under the artifact name. + const download = steps.find((step) => step.uses?.startsWith('actions/download-artifact@')); + expect(download?.with?.['merge-multiple']).toBe(true); + expect(secretsOf(workflow.jobs['prepare-next-version'] ?? {})).toEqual([]); +}); + test('release publication follows the build directly', () => { expect(workflow.jobs.build?.needs).toBe('plan'); expect(workflow.jobs.publish?.needs).toEqual(['plan', 'build']);