-
Notifications
You must be signed in to change notification settings - Fork 13
chore(vercel): gate preview builds on pull request readiness #341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
imshashank
wants to merge
22
commits into
main
Choose a base branch
from
chore/gate-preview-builds
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
e311851
chore(vercel): gate preview builds on pull request readiness
imshashank 8620b54
test(vercel): cover the build gate, and watch the root tsconfig
imshashank 2da9f89
fix(vercel): require a complete pull request payload before skipping
imshashank 2a78c65
docs(vercel): design the preview deployment gate
imshashank 918908f
Merge remote-tracking branch 'origin/main' into chore/gate-preview-bu…
imshashank e255616
docs(vercel): tighten the preview gate plan
imshashank ae5fefc
feat(ci): define preview deployment policy
imshashank 2e359cd
fix(ci): harden preview event schemas
imshashank 756ea88
fix(ci): validate preview deployment identity
imshashank a861e38
docs(vercel): finalize the preview controller contract
imshashank 89201b1
feat(ci): deploy previews after successful checks
imshashank cb219bf
fix(ci): harden preview reconciliation
imshashank 73a42cc
docs(vercel): harden preview workflow plan
imshashank d4fe89e
chore(vercel): gate previews after CI
imshashank b6ea842
test(vercel): lock preview workflow contract
imshashank 6b362df
test(vercel): keep legacy setting scan clean
imshashank 5998d8c
fix(ci): harden preview deployment reconciliation
imshashank 5adec4b
chore(ci): apply preview review cleanups
imshashank 618ecb6
fix(ci): cancel superseded preview deployments
imshashank dee4f81
Merge remote-tracking branch 'source/main' into chore/gate-preview-bu…
imshashank 6556e05
fix(ci): declare read-only workflow permissions
imshashank 99d8c63
Merge main into chore/gate-preview-builds
imshashank File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| name: Vercel Preview | ||
|
|
||
| on: | ||
| pull_request_target: | ||
| branches: [main] | ||
| types: [opened, reopened, ready_for_review, converted_to_draft, labeled, unlabeled, closed] | ||
| workflow_run: | ||
| workflows: [CI] | ||
| types: [completed] | ||
| repository_dispatch: | ||
| types: [vercel-preview-reconcile] | ||
|
|
||
| permissions: | ||
| actions: read | ||
| contents: read | ||
| pull-requests: read | ||
|
|
||
| concurrency: | ||
| group: vercel-preview-${{ github.event.pull_request.number || github.event.workflow_run.pull_requests[0].number || github.event.client_payload.pull_request || github.event.workflow_run.head_sha || github.run_id }} | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| reconcile: | ||
| if: >- | ||
| github.event_name == 'pull_request_target' || | ||
| github.event_name == 'repository_dispatch' || | ||
| (github.event_name == 'workflow_run' && | ||
| github.event.workflow_run.event == 'pull_request' && | ||
| github.event.workflow_run.conclusion == 'success') | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 25 | ||
| steps: | ||
| - name: Check out trusted controller | ||
| uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 | ||
| with: | ||
| repository: ${{ github.repository }} | ||
| ref: ${{ github.sha }} | ||
| persist-credentials: false | ||
| submodules: false | ||
| lfs: false | ||
| - name: Set up Bun | ||
| uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 | ||
| with: | ||
| bun-version: "1.3.14" | ||
| - name: Install trusted controller dependencies | ||
| run: bun install --frozen-lockfile --ignore-scripts | ||
| - name: Reconcile Vercel Preview | ||
| run: bun scripts/vercel-preview-deploy.ts | ||
| env: | ||
| GITHUB_TOKEN: ${{ github.token }} | ||
| VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} | ||
| VERCEL_TEAM_ID: ${{ vars.VERCEL_TEAM_ID }} | ||
| VERCEL_PROJECT_ID: ${{ vars.VERCEL_PROJECT_ID }} | ||
| VERCEL_PROJECT_NAME: ${{ vars.VERCEL_PROJECT_NAME }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| # Vercel Preview deployment gate | ||
|
|
||
| Orbit creates Vercel Preview deployments only after the exact pull request head | ||
| has passed CI and still satisfies the repository policy. Production deployments | ||
| from `main` remain enabled through the Vercel Git integration. | ||
|
|
||
| ## Eligibility | ||
|
|
||
| The controller evaluates the current pull request from GitHub on every event. | ||
| `no-preview` takes precedence over every other state. | ||
|
|
||
| | Pull request state | Labels | Result after exact-head CI succeeds | | ||
| | --- | --- | --- | | ||
| | Ready for review | neither managed label | eligible | | ||
| | Ready for review | `preview` | eligible | | ||
| | Ready for review | `no-preview`, with or without `preview` | ineligible | | ||
| | Draft | `preview` without `no-preview` | eligible | | ||
| | Draft | neither managed label | ineligible | | ||
| | Draft | `no-preview`, with or without `preview` | ineligible | | ||
| | Closed | any labels | ineligible | | ||
| | Fork | any state or labels | never eligible for an automatic Preview | | ||
|
|
||
| An eligible pull request must also target `main`, come from the same repository, | ||
| and change at least one web-impacting path: | ||
|
|
||
| - `apps/web/**` | ||
| - `packages/**` | ||
| - `package.json` | ||
| - `bun.lock` | ||
| - `tsconfig.base.json` | ||
|
|
||
| For a renamed file, either the current filename or GitHub's validated | ||
| `previous_filename` can make the change web-impacting. Moving code out of | ||
| `apps/web/**` or `packages/**` therefore still requires a Preview. | ||
|
|
||
| Ready status or the `preview` label does not establish trust. The controller | ||
| also proves that the newest `CI` run belongs to the current head SHA, is | ||
| associated with the same pull request and current `main`, and completed | ||
| successfully. A state event can create a Preview immediately when that proof | ||
| already exists. Otherwise the successful `workflow_run` event reconciles the | ||
| pull request after CI finishes. A later non-green run blocks an older success. | ||
| Before Create, the controller repeats the CI proof and then refetches the pull | ||
| request once more. Any head, identity, state, draft, or label change during that | ||
| proof prevents the POST. | ||
|
|
||
| ## Trust boundary | ||
|
|
||
| `Vercel Preview` is a privileged default-branch workflow. It checks out | ||
| `${{ github.sha }}`, which is the trusted base or default-branch commit for its | ||
| three event types. It never selects, fetches, installs, caches, downloads an | ||
| artifact from, builds, or executes pull request code. Dependency lifecycle | ||
| scripts are disabled. The only operational command is | ||
| `bun scripts/vercel-preview-deploy.ts`, and `VERCEL_TOKEN` exists only on that | ||
| step. | ||
|
|
||
| Only the trusted GitHub controller is isolated from pull request code. The | ||
| API-created Vercel Preview still builds same-repository pull request code with | ||
| the project Preview environment scope. Git Fork Protection must remain enabled, | ||
| and forks are rejected by the controller, but maintainers must still treat the | ||
| Preview environment as available to same-repository pull request code. | ||
|
|
||
| The `git.deploymentEnabled` map in `apps/web/vercel.json` disables automatic Git | ||
| deployments for `**` and enables them for `main`. This is a repository-controlled | ||
| cost policy, not a security boundary. A repository change can alter that policy, | ||
| so security depends on the trusted workflow and controller validation. | ||
|
|
||
| Each API create remains a Vercel deployment. Canceled attempts and reused | ||
| deployments can remain visible in Vercel deployment history and counts. The gate | ||
| reduces unnecessary creation, but it does not promise that an ignored or | ||
| canceled attempt is free. | ||
|
|
||
| ## Reconciliation and Vercel API behavior | ||
|
|
||
| The controller uses one deployment path: | ||
|
|
||
| 1. Vercel v7 lists Preview deployments by team, project, branch, and, when | ||
| creating, exact head SHA. | ||
| 2. Vercel v13 creates or reads a deployment with the same-repository GitHub | ||
| repository ID, head ref, exact head SHA, and Orbit metadata. It omits a | ||
| target so Vercel uses the project's Preview environment. | ||
| 3. Vercel v12 cancels matching active deployments. | ||
|
|
||
| Deployment IDs are accepted only when they contain ASCII letters, digits, | ||
| underscores, and hyphens within the controller's fixed bound. The controller | ||
| checks IDs and URLs against both tokens before they can enter a result, and URL | ||
| encodes every deployment ID used as an API path segment. | ||
|
|
||
| `QUEUED`, `INITIALIZING`, and `BUILDING` deployments are active. Making a pull | ||
| request ineligible by closing it, converting it to draft without `preview`, | ||
| removing `preview` from an otherwise ineligible draft, or adding `no-preview` | ||
| cancels matching active Preview work. A deployment that is already `READY` is | ||
| not canceled, so its ready URL remains available. | ||
|
|
||
| Per-pull-request workflow runs remain serialized with in-progress cancellation | ||
| disabled. While the owner polls a queued, initializing, or building deployment, | ||
| it refetches the current pull request after every active detail response. If the | ||
| same exact head becomes closed or ineligible, that owner cancels only its exact | ||
| deployment and returns a canceled result. If the head or repository identity | ||
| changed, the old owner stops without canceling the different head. The queued | ||
| state event then reconciles the latest state. | ||
|
|
||
| Events for stale heads cannot create or cancel work for the current head. An | ||
| existing exact ready or active deployment is reused. Terminal deployment | ||
| history can cause one forced create for the exact head, using the same v13 | ||
| endpoint rather than an alternate build path. | ||
|
|
||
| ## Repository and Vercel setup | ||
|
|
||
| The GitHub repository must provide: | ||
|
|
||
| - Secret `VERCEL_TOKEN` | ||
| - Variable `VERCEL_TEAM_ID` | ||
| - Variable `VERCEL_PROJECT_ID` | ||
| - Variable `VERCEL_PROJECT_NAME` | ||
|
|
||
| Keep Vercel Git Fork Protection enabled. Synchronize the managed `preview` and | ||
| `no-preview` labels with the rest of the repository labels: | ||
|
|
||
| ```bash | ||
| bun run labels:sync | ||
| bun run labels:sync --apply | ||
| ``` | ||
|
|
||
| The first command is a dry run. Review its plan before applying it. | ||
|
|
||
| After `.github/workflows/vercel-preview.yml` is present on `main`, remove these | ||
| legacy Vercel environment values: | ||
|
|
||
| - `BUILD_GATE_GITHUB_TOKEN` | ||
| - `BUILD_GATE_WATCH_PATHS` | ||
| - `BUILD_GATE_READY_LABEL` | ||
| - `BUILD_GATE_BLOCK_LABEL` | ||
|
|
||
| They belonged to the removed Ignored Build Step and are not read by the trusted | ||
| controller. | ||
|
|
||
| ## Manual recovery | ||
|
|
||
| A maintainer can reconcile a positive numeric pull request number with an | ||
| authenticated repository dispatch: | ||
|
|
||
| ```bash | ||
| gh api repos/Noveum/orbit/dispatches \ | ||
| --method POST \ | ||
| -f event_type=vercel-preview-reconcile \ | ||
| -F 'client_payload[pull_request]=341' | ||
| ``` | ||
|
|
||
| The event type must be `vercel-preview-reconcile`, and | ||
| `client_payload.pull_request` must be a positive number. The event shares the | ||
| same per-pull-request concurrency group as state and CI events. Malformed input | ||
| can form an unused group but is rejected before any Vercel call. | ||
|
|
||
| Do not add or use `workflow_dispatch` for recovery. A caller can select a | ||
| non-default ref for that trigger. GitHub runs `repository_dispatch` from the | ||
| last commit on the default branch, preserving the controller trust boundary. | ||
|
|
||
| ## Post-merge canary | ||
|
|
||
| Run this procedure only after the workflow exists on `main`: | ||
|
|
||
| 1. Confirm Vercel Git Fork Protection is enabled. Configure `VERCEL_TOKEN` and | ||
| the `VERCEL_TEAM_ID`, `VERCEL_PROJECT_ID`, and `VERCEL_PROJECT_NAME` | ||
| repository variables. | ||
| 2. Remove the four legacy values only after the workflow is on `main`. | ||
| 3. Open a same-repository, web-impacting draft at head A. Confirm it gets no | ||
| Preview, apply `preview`, let CI succeed for exact head A, and wait for its | ||
| deployment to reach `READY`. Record and retain head A's ready URL. | ||
| 4. Keep `preview` applied and push a web-impacting head B. Let exact-head CI | ||
| succeed and wait until B's deployment is `QUEUED`, `INITIALIZING`, or | ||
| `BUILDING`. Apply `no-preview`. Confirm the polling owner observes the new | ||
| live state and cancels the deployment whose metadata names head B before it | ||
| reaches `READY`, while head A's recorded ready URL remains available. | ||
| 5. Keep `no-preview` applied, make the pull request ready for review, and push a | ||
| web-impacting head C. Let exact-head CI succeed and confirm no deployment is | ||
| created for C while the label remains. Remove `no-preview`, then confirm a | ||
| deployment is created for exact head C. Confirm no new deployment is created | ||
| for head B and no SHA other than C is selected by this reconciliation. | ||
| 6. Open a ready same-repository pull request with only a docs change, let its | ||
| exact-head CI succeed, and confirm it receives no automatic Preview. | ||
| 7. Open a ready fork pull request with a web-impacting change, let its exact-head | ||
| CI succeed, and confirm it receives no automatic Preview. | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.