Unverified Commit 25647f00 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

ci: optimize PR stack gate polling and test action

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 077feaf8
# PR Stack CI Gate
The `build_and_test` workflow calls this read-only action for same-repository PRs.
It infers branch-based stacks from open PR head/base branch names; no GitHub
stack API or Graphite integration is required. Fork PRs bypass the checkout and
this action entirely and run full CI immediately.
The first three PRs and every leaf run immediately. An internal PR polls the
closest three predecessors every five minutes: **any** current-head/current-base
`thank you, next` success releases full CI, while three terminal failures fail
the required gate. Unresolved waits open after five hours (six-hour job timeout).
Transient GitHub 5xx/429 errors retry every five minutes; other errors start
full CI so they cannot accidentally mark the aggregate green.
## Development
```bash
pnpm --dir .github/actions/pr-stack-ci-gate install --frozen-lockfile --ignore-scripts
pnpm --dir .github/actions/pr-stack-ci-gate types
pnpm --dir .github/actions/pr-stack-ci-gate build
pnpm --dir .github/actions/pr-stack-ci-gate test
```
Commit changes to `src/` **and** the generated `dist/index.js` and
`dist/licenses.txt`. Tests use an isolated Jest config because the repository's
main Jest config searches only the `test/` and package trees. A captured shape
from #99095's actual required-check response lives in `fixtures/`. The workflow
sparse-checks out only `action.yml` and `dist/index.js` from `${{ github.sha }}`;
that is the test-merge commit on PR runs. The caller and action job grant only
`checks: read`, `contents: read` and `pull-requests: read`. No secrets are
inherited, and `persist-credentials` is false.
## API budget and correctness
A stable middle-PR poll rechecks its own PR, finds successors, walks three
predecessor links and checks all three required results: **8 REST calls** versus
11 before this action. The `pulls.list` response already contains head/base
SHAs, so the three extra predecessor `pulls.get` calls are unnecessary while
waiting. Before opening or failing, the action re-reads current PR metadata and
the decisive predecessor(s) and check(s). We cannot stop at the first pending
predecessor: an older PR can already have passed; nor can we permanently cache
a terminal failure because that PR can succeed on rerun. Rechecking live branch
topology on *every* five-minute poll catches retargeted, closed, or leaf PRs at
the original cadence.
GitHub's `GITHUB_TOKEN` primary budget is typically **1,000/hour/repository**
for both REST requests and GraphQL points. Most REST GETs cost one point.
GraphQL may batch data into a one-point query, but the check-run-to-PR base-SHA
association and nested pagination would need independent correctness testing;
this implementation instead takes a measured REST reduction without weakening
the already verified current-base guard. An `ubuntu-slim` runner is unsuitable:
its 15-minute job limit is shorter than the gate's multi-hour wait.
name: 'PR Stack CI Gate'
description: 'Delay expensive CI for branch-based stacked pull requests until prior CI succeeds.'
runs:
using: 'node24'
main: 'dist/index.js'
This diff is collapsed.
This diff is collapsed.
{
"id": 107285638067,
"name": "thank you, next",
"status": "completed",
"conclusion": "success",
"head_sha": "360b493601939d820d35a77c2d4ec691ce12089d",
"html_url": "https://github.com/vercel/next.js/actions/runs/35887957948/job/107285638067",
"app": { "slug": "github-actions" },
"pull_requests": [
{
"number": 99095,
"head": { "sha": "360b493601939d820d35a77c2d4ec691ce12089d" },
"base": {
"ref": "ci/branch-stack-polling-gate",
"sha": "939d61265dcb4a6f91485eb08b0907062f71ab51"
}
}
]
}
This diff is collapsed.
This diff is collapsed.
const ts = require('typescript')
// Jest runs the TypeScript action source without emitting temporary JS into
// the repository checkout (where repo-wide lint would discover it).
module.exports = {
process(source, filename) {
return {
code: ts.transpileModule(source, {
fileName: filename,
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2024,
esModuleInterop: true,
},
}).outputText,
}
},
}
module.exports = {
rootDir: __dirname,
testEnvironment: 'node',
testMatch: ['<rootDir>/**/*.test.js'],
transform: { '^.+\\.ts$': '<rootDir>/jest-typescript-transform.cjs' },
clearMocks: true,
}
{
"private": true,
"description": "Read-only CI gate for branch-based pull request stacks",
"scripts": {
"build": "ncc build src/index.ts -m -o dist --license licenses.txt",
"types": "tsc --noEmit",
"test": "../../../node_modules/.bin/jest --config jest.config.cjs --runInBand"
},
"dependencies": {
"@actions/core": "1.11.1",
"@actions/github": "6.0.0"
},
"devDependencies": {
"@vercel/ncc": "0.38.4",
"typescript": "6.0.2"
}
}
import * as core from '@actions/core'
import { context, getOctokit } from '@actions/github'
import { runGate } from './gate'
async function main(): Promise<void> {
// Never accept a separately supplied token: the workflow's read-only job
// token is the only credential this action should see.
const token = process.env.GITHUB_TOKEN
if (!token) throw new Error('GITHUB_TOKEN is unavailable')
await runGate({ github: getOctokit(token), context, core })
}
main().catch((error: unknown) => {
// An unavailable token or unexpected entry-point error cannot make CI green:
// open the gate so the normal, required full-CI graph still runs.
core.warning(
`PR stack gate could not start; running full CI: ${String(error)}`
)
core.setOutput('skip', 'false')
})
{
"compilerOptions": {
"target": "es2024",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"rootDir": "./src",
"strict": true,
"skipLibCheck": false,
"esModuleInterop": true
},
"include": ["src/**/*.ts"]
}
...@@ -75,6 +75,22 @@ importers: ...@@ -75,6 +75,22 @@ importers:
specifier: 0.38.4 specifier: 0.38.4
version: 0.38.4 version: 0.38.4
actions/pr-stack-ci-gate:
dependencies:
'@actions/core':
specifier: 1.11.1
version: 1.11.1
'@actions/github':
specifier: 6.0.0
version: 6.0.0
devDependencies:
'@vercel/ncc':
specifier: 0.38.4
version: 0.38.4
typescript:
specifier: 6.0.2
version: 6.0.2
actions/validate-docs-links: actions/validate-docs-links:
dependencies: dependencies:
'@actions/core': '@actions/core':
......
...@@ -249,7 +249,10 @@ jobs: ...@@ -249,7 +249,10 @@ jobs:
skipNativeBuild: 'yes' skipNativeBuild: 'yes'
skipNativeInstall: 'yes' skipNativeInstall: 'yes'
afterBuild: | afterBuild: |
node --test .github/actions/pr-stack-ci-gate/gate.test.mjs pnpm --dir .github/actions/pr-stack-ci-gate install --frozen-lockfile --ignore-scripts
pnpm --dir .github/actions/pr-stack-ci-gate types
pnpm --dir .github/actions/pr-stack-ci-gate build
pnpm --dir .github/actions/pr-stack-ci-gate test
pnpm lint-no-typescript pnpm lint-no-typescript
pnpm check-examples pnpm check-examples
pnpm validate-externals-doc pnpm validate-externals-doc
......
...@@ -48,22 +48,21 @@ jobs: ...@@ -48,22 +48,21 @@ jobs:
echo '- Reason: fork PRs always run immediately' echo '- Reason: fork PRs always run immediately'
} >> "$GITHUB_STEP_SUMMARY" } >> "$GITHUB_STEP_SUMMARY"
# Fetch only the gate script, from the exact commit this CI run tests. # Fetch the compiled action from the exact commit this CI run tests.
- name: Check out stack gate from this CI commit - name: Check out stack gate from this CI commit
if: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository }} if: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository }}
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with: with:
ref: ${{ github.sha }} ref: ${{ github.sha }}
sparse-checkout: .github/actions/pr-stack-ci-gate/gate.cjs sparse-checkout: |
.github/actions/pr-stack-ci-gate/action.yml
.github/actions/pr-stack-ci-gate/dist/index.js
sparse-checkout-cone-mode: false sparse-checkout-cone-mode: false
persist-credentials: false persist-credentials: false
- name: Wait for previous stack CI - name: Wait for previous stack CI
id: gate id: gate
if: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository }} if: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository }}
uses: actions/github-script@d746ffe35508b1917358783b479e04febd2b8f71 # v9.0.0 uses: ./.github/actions/pr-stack-ci-gate
with: env:
github-token: ${{ github.token }} GITHUB_TOKEN: ${{ github.token }}
script: |
const gate = require('./.github/actions/pr-stack-ci-gate/gate.cjs')
await gate({ github, context, core })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment