diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 09d44bc..7eab68e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -176,6 +176,8 @@ jobs: if: github.event_name == 'push' && github.ref == 'refs/heads/main' runs-on: ubuntu-latest timeout-minutes: 10 + # The deploy key lives in this environment, whose deployment branches are limited to main, so no other branch's workflow can read it. + environment: release permissions: contents: write issues: write @@ -183,10 +185,13 @@ jobs: id-token: write attestations: write steps: + # The default GITHUB_TOKEN cannot bypass a repository ruleset (GitHub does not support naming the github-actions[bot] identity as a bypass actor), so the release commit and tags are pushed over SSH with a deploy key, which a ruleset can name as a bypass actor. The key lives in the `release` environment, whose deployment branches are limited to main. - name: Checkout uses: actions/checkout@v4 with: fetch-depth: 0 + # Credentials are not persisted: checkout fetches with the job's own token and then removes it, leaving nothing in git config for later steps. The deploy key is provisioned apart, below. + persist-credentials: false - name: Setup pnpm uses: pnpm/action-setup@v4 @@ -203,8 +208,28 @@ jobs: - name: Build run: pnpm build + # The last step before Release and the only place the key is provisioned: written to the runner's temp directory with mode 600 from the step's env (never a command line), together with GitHub's ed25519 host key, pinned so the connection is verified rather than trusted on first use (fingerprint SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU, as published at https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/githubs-ssh-key-fingerprints). The step then proves the key has write access to the repository, because semantic-release falls back to an https URL with GITHUB_TOKEN embedded when its own SSH push check fails, and the ruleset would reject that push only after the release had started. The dry run targets a ref name that does not exist, so it can never be rejected as non-fast-forward when main has moved on since checkout (semantic-release itself skips that case cleanly, and the step must not turn it into a failure). GitHub does not evaluate rulesets on a dry run, so this does not prove the key is a bypass actor. BASH_ENV is blanked and ssh and git are called by absolute path so that a variable or PATH entry set by an earlier step does not redirect them. + - name: Provision the deploy key for the Release step + env: + RELEASE_DEPLOY_KEY: ${{ secrets.RELEASE_DEPLOY_KEY }} + # Blanked so a BASH_ENV exported by an earlier step cannot run in the shell that holds the key. + BASH_ENV: '' + run: | + install -d -m 700 "$RUNNER_TEMP/release-ssh" + (umask 077 && printf '%s\n' "${RELEASE_DEPLOY_KEY%$'\n'}" > "$RUNNER_TEMP/release-ssh/id") + printf '%s\n' 'github.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl' > "$RUNNER_TEMP/release-ssh/known_hosts" + # Must match the GIT_SSH_COMMAND of the Release step. + GIT_SSH_COMMAND="/usr/bin/ssh -i $RUNNER_TEMP/release-ssh/id -o IdentitiesOnly=yes -o UserKnownHostsFile=$RUNNER_TEMP/release-ssh/known_hosts -o StrictHostKeyChecking=yes" \ + /usr/bin/git push --dry-run --no-verify "git@github.com:$GITHUB_REPOSITORY.git" "HEAD:refs/heads/release-key-probe-$GITHUB_RUN_ID" + git remote set-url origin "git@github.com:$GITHUB_REPOSITORY.git" - name: Release env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + # Must match the GIT_SSH_COMMAND of the provisioning step. + GIT_SSH_COMMAND: /usr/bin/ssh -i ${{ runner.temp }}/release-ssh/id -o IdentitiesOnly=yes -o UserKnownHostsFile=${{ runner.temp }}/release-ssh/known_hosts -o StrictHostKeyChecking=yes NPM_CONFIG_PROVENANCE: true - run: pnpm exec semantic-release + # semantic-release pushes to its repositoryUrl, which defaults to package.json's https repository field rather than to origin, so the SSH URL is passed explicitly for the push to use the deploy key. + run: pnpm exec semantic-release --repository-url "git@github.com:$GITHUB_REPOSITORY.git" + - name: Remove the deploy key + if: always() + run: rm -rf "$RUNNER_TEMP/release-ssh"