Skip to content

Check out fork PRs of private repositories from the base repository - #450

Draft
Mpdreamz wants to merge 1 commit into
mainfrom
fix/private-repo-pr-checkout
Draft

Mpdreamz wants to merge 1 commit into
mainfrom
fix/private-repo-pr-checkout

Conversation

@Mpdreamz

Copy link
Copy Markdown
Member

Why

Preview deploys fail for pull requests that Elastic employees open from their fork of a private repository. The preview comment is posted ("⏳ Building and deploying preview…") and then never updated, because the build job dies at checkout.

Real example: https://github.com/elastic/cloud/pull/161041 (opened from gigerdo/cloud), run https://github.com/elastic/cloud/actions/runs/43048361021:

deploy / build  Checkout
  with:
    repository: gigerdo/cloud
    ref: eb100fba099e49faa1149d7dd6a760e1bd581192
  ##[error]fatal: repository 'https://github.com/gigerdo/cloud/' not found
  ##[error]The process '/usr/bin/git' failed with exit code 128

deploy-preview, comment and update-link-index are all skipped as a consequence, so nothing ever replaces the in-progress comment.

Why this happens

  1. docs-deploy.yml deliberately lets fork PRs from Elastic org members through (is-fork == 'true' and is-org-member == 'true'). Non-members are rejected by Evaluate and Reject fork checkout. So the fork guard is not skipped by mistake; the member is allowed, which is also why pre-comment posted the comment.
  2. The build job then checks out needs.preflight.outputs.head-repo, which is the fork (gigerdo/cloud).
  3. A fork of a private repository is itself private, and it lives under the user's own account. The GITHUB_TOKEN of the workflow run is scoped to the base repository (contents: read on elastic/cloud) and has no access to gigerdo/cloud. GitHub answers with "repository not found", not 403.

This was never visible for public repositories, where forks are public and can be cloned anonymously. It only shows up for private repositories such as elastic/cloud. Verified for the example: elastic/cloud is private: true, gigerdo/cloud is private: true and fork: true.

Why refs/pull/<n>/head works

The base repository itself exposes every PR head, including fork PRs, as refs/pull/<n>/head. The workflow's token can read the base repository, so no extra permission or secret is needed. For the failing example:

$ git ls-remote git@github.com:elastic/cloud.git refs/pull/161041/head
eb100fba099e49faa1149d7dd6a760e1bd581192	refs/pull/161041/head

That is exactly the head-sha the failed run tried to fetch from the fork.

What

In the build job of docs-deploy.yml, the single Checkout step becomes two mutually exclusive steps:

Situation Checkout
Public repository (any event) Unchanged: head-repo at head-sha
Private repository, push or same-repo PR Unchanged
Private repository, fork PR, PR number known github.repository at refs/pull/<n>/head

The condition is only true when all of these hold: github.event.repository.private == true, the event is pull_request, is-fork == 'true', and pr-number is not empty. The second step is its exact negation, so one of the two always runs and never both.

Nothing else changes. HEAD_REPO is still the fork's name for the later build step (GITHUB_REPOSITORY), and the existing Validate checkout SHA step still runs after either checkout.

Security

  • The trust model is unchanged. The same code is checked out as before (the fork PR's head commit) and only for the same audience: org members. Non-member fork PRs still stop at Evaluate and Reject fork checkout and never reach checkout.
  • No new token, secret or permission. persist-credentials: false and the contents: read permission are kept.
  • Validate checkout SHA compares HEAD with head-sha from the triggering run. If the author pushes again between the build and the deploy, refs/pull/<n>/head points at the newer commit, the check fails and the deploy stops with the existing "branch may have been updated" message, the same behaviour as before for stale SHAs.

Scope: private repositories only

Public repositories are intentionally not touched. They work today and keep the exact same code path. If public forks ever need this, that is a separate decision.

Testing

Done:

  • actionlint .github/workflows/docs-deploy.yml passes.
  • Confirmed with git ls-remote that refs/pull/161041/head in elastic/cloud resolves to the SHA the failed run requested.
  • The two checkout conditions are exact complements.

Not done: an end-to-end run. docs-deploy.yml is a reusable workflow called as elastic/docs-actions/.github/workflows/docs-deploy.yml@v1 and it triggers on workflow_run, so it cannot be exercised from a branch of this repo alone. Suggested verification after merge and the v1 tag move: re-run https://github.com/elastic/cloud/actions/runs/43048361021 (or push to the PR) and check that Checkout succeeds and Validate checkout SHA passes. I also have not checked whether actions/checkout@v7 fetching refs/pull/<n>/head with fetch-depth: 1 behaves differently from the plain-SHA fetch; it is the documented use of the ref input.

Not in this PR

When build fails, the in-progress comment is never updated, so a failed deploy looks like a hung one. That is a separate concern (error handling rather than checkout) and should be its own PR.

Until this ships, the workaround for affected PRs is to push the branch to the private repository itself instead of a fork.

🤖 Generated with Claude Code

Forks of a private repository are private, so the deploy build cannot clone
them with this workflow's token and fails with 'repository not found'. For
private repositories, fetch refs/pull/<n>/head from the base repository
instead. Public repositories keep checking out the fork directly.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@Mpdreamz

Copy link
Copy Markdown
Member Author

@elastic/docs-engineering could you review this? docs-deploy.yml is the reusable workflow behind @v1, so a mistake here affects the preview deploy of every repository that uses it, and it cannot be exercised from a branch of this repo (it is a workflow_run workflow). That is why a careful read matters more than usual.

Where I'd like the most attention:

  • The two checkout conditions are meant to be exact complements, so one always runs and never both. Public repositories and non-fork PRs must keep the original checkout, unchanged.
  • The trust model: the same fork code is checked out as before, for the same audience (org members only), with no new token or permission. Non-members still stop at Evaluate and Reject fork checkout.
  • Whether github.event.repository.private is the right signal inside this workflow_run workflow.

Untested end to end. Proposed verification after merge and the v1 tag move: re-run https://github.com/elastic/cloud/actions/runs/43048361021.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant