Interview Prep/GitHub Actions/Security Hardening & Supply Chain

A job needs to push a commit back to the PR branch (auto-format). Do it safely.

IntermediateScenarioSecurity Hardening & Supply Chain
Answer

Situation read → Write access to branches, fork limitations, loop risk.

What I'd do →

  1. Only for same-repo PRs: if: github.event.pull_request.head.repo.full_name == github.repository.
  2. permissions: contents: write, checkout ref: ${{ github.head_ref }}.
  3. Commit as github-actions[bot] and push; the push by GITHUB_TOKEN will not retrigger CI (no loop), so run checks on the formatted code in the same job or require a new push.

Why → Fork branches cannot be written with GITHUB_TOKEN, and pushing back with a PAT would trigger loops.

Interview tip → Many teams prefer failing the check with a diff over pushing commits.

All GitHub Actions questions Roadmap: GitHub Actions

More on Security Hardening & Supply Chain