Skip to content

ci: run Chromatic on authorized fork PRs, skip unauthorized ones#383

Open
mkitti wants to merge 1 commit into
mainfrom
chromatic-fork-prs
Open

ci: run Chromatic on authorized fork PRs, skip unauthorized ones#383
mkitti wants to merge 1 commit into
mainfrom
chromatic-fork-prs

Conversation

@mkitti
Copy link
Copy Markdown
Contributor

@mkitti mkitti commented Jun 4, 2026

Make Chromatic work for fork PRs

A plain pull_request event withholds repository secrets from forks, so CHROMATIC_PROJECT_TOKEN is empty for pull requests opened from a fork and the Chromatic job fails. This reworks the workflow so fork PRs are handled deliberately.

Changes

  • Trigger → pull_request_target so the CHROMATIC_PROJECT_TOKEN secret is available even for fork PRs.
  • Job gate (if:) — the job runs for pushes, manual dispatch, same-repo PRs, and fork PRs whose author is a trusted collaborator (OWNER / MEMBER / COLLABORATOR, via author_association). PRs from unauthorized forks fall through the condition and the job is skipped (neutral) rather than failing.
  • Checkout now pins the PR head commit from the contributor's fork (repository: head.repo.full_name, ref: head.sha), so authorized fork PRs build the actual fork branch. Falls back to the base repo for pushes/same-repo PRs.
  • Fixed two pre-existing bugs: the CHROMATIC_* env vars were attached to the checkout step (where the Chromatic action could not read them) — moved to job-level env — and the CHROMATIC_SHA push fallback used github.ref (a ref name) instead of github.sha.

Security note

pull_request_target runs with secrets in scope, and this workflow checks out the PR head. The if: gate is the mitigation: untrusted fork code is never reached because the job is skipped for non-collaborator authors, and collaborators already have write access to the repo. Authorization uses author_association (no extra API call); note MEMBER is a slightly loose proxy for repo write access.

🤖 Generated with Claude Code

A plain pull_request event withholds repository secrets from forks, so
CHROMATIC_PROJECT_TOKEN was empty for fork PRs and Chromatic failed.

Switch the trigger to pull_request_target so the token is available, and
gate the job: it runs for pushes, manual dispatch, same-repo PRs, and
fork PRs whose author is a trusted collaborator (OWNER/MEMBER/
COLLABORATOR). PRs from unauthorized forks fall through the condition
and the job is skipped (neutral) instead of failing. The checkout now
pins the PR head commit from the contributor's fork.

Also move the CHROMATIC_* env vars from the checkout step (where the
Chromatic action could not read them) to the job, and fix the
CHROMATIC_SHA push fallback to use github.sha rather than github.ref.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the Chromatic GitHub Actions workflow to support fork-based pull requests by switching to pull_request_target, adding an authorization gate to skip untrusted fork PRs, and checking out the PR head SHA so authorized forks build the contributor’s actual code while still having access to CHROMATIC_PROJECT_TOKEN.

Changes:

  • Switch workflow trigger from pull_request to pull_request_target to allow secrets on fork PRs.
  • Add a job-level if: gate using author_association and same-repo checks to skip unauthorized fork PRs.
  • Move CHROMATIC_* variables to job-level env and fix SHA selection; update checkout to use PR head repository + SHA.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +19 to +23
# Run for pushes/manual dispatch and for same-repository PRs. For PRs from a
# fork, only run when the author is a trusted collaborator (OWNER, MEMBER, or
# COLLABORATOR) so the secret is exposed only to users who already have write
# access. PRs from unauthorized forks fall through this condition and the job
# is skipped (neutral) rather than failing.
Comment on lines 39 to +43
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}
env:
CHROMATIC_BRANCH: ${{ github.event.pull_request.head.ref || github.ref_name }}
CHROMATIC_SHA: ${{ github.event.pull_request.head.sha || github.ref }}
CHROMATIC_SLUG: ${{ github.repository }}
# For fork PRs, check out the contributor's head commit from their fork.
# Falls back to the base repository for pushes and same-repo PRs.
repository: ${{ github.event.pull_request.head.repo.full_name || github.repository }}
ref: ${{ github.event.pull_request.head.sha || github.sha }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants