Skip to content

Commit 43c1c60

Browse files
authored
[16.3] [ci] Use OIDC tokens to read private preview builds (#97258)
Backports #96919 🤖 Generated with [Claude Code](https://claude.com/claude-code)
1 parent 9dbb357 commit 43c1c60

5 files changed

Lines changed: 154 additions & 32 deletions

File tree

‎.github/workflows/build_and_test.yml‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,12 @@ jobs:
741741
# Outer bound only. The script gives up first, so a tarball that never
742742
# arrives is reported by it rather than by the runner killing the job.
743743
timeout-minutes: 35
744-
permissions: {}
744+
permissions:
745+
# contents: read is needed to check out in private repos (the mirror).
746+
contents: read
747+
# Minting GitHub OIDC tokens to read private preview builds. Dormant in
748+
# this repo since builds here are public.
749+
id-token: write
745750
steps:
746751
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
747752
with:
@@ -755,7 +760,7 @@ jobs:
755760
- name: Wait for preview tarball
756761
env:
757762
PREVIEW_BUILDS_BASE_URL: ${{ vars.PREVIEW_BUILDS_BASE_URL }}
758-
PREVIEW_BUILDS_READ_TOKEN: ${{ secrets.PREVIEW_BUILDS_READ_TOKEN }}
763+
PREVIEW_BUILDS_ACCESS: ${{ vars.PREVIEW_BUILDS_ACCESS }}
759764
run: |
760765
set -euo pipefail
761766
node scripts/wait-for-preview-tarball.mjs \
@@ -774,6 +779,11 @@ jobs:
774779
matrix:
775780
group: [1/5, 2/5, 3/5, 4/5, 5/5]
776781

782+
permissions:
783+
contents: read
784+
# Minting GitHub OIDC tokens to read private preview builds. Dormant in
785+
# this repo since builds here are public.
786+
id-token: write
777787
uses: ./.github/workflows/build_reusable.yml
778788
with:
779789
# Keep Next.js related env variables in sync with additionalEnv in next-deploy.ts
@@ -803,6 +813,11 @@ jobs:
803813
matrix:
804814
group: [1/5, 2/5, 3/5, 4/5, 5/5]
805815

816+
permissions:
817+
contents: read
818+
# Minting GitHub OIDC tokens to read private preview builds. Dormant in
819+
# this repo since builds here are public.
820+
id-token: write
806821
uses: ./.github/workflows/build_reusable.yml
807822
with:
808823
afterBuild: |

‎.github/workflows/build_reusable.yml‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,7 @@ env:
142142
VERCEL_ADAPTER_TEST_TEAM: vtest314-next-adapter-e2e-tests
143143
VERCEL_TURBOPACK_TEST_TOKEN: ${{ secrets.VERCEL_TURBOPACK_TEST_TOKEN }}
144144
VERCEL_TURBOPACK_TEST_TEAM: vtest314-next-turbo-e2e-tests
145-
# Read token for the auth-protected preview-builds npm mirror, used by deploy
146-
# tests to install Next.js artifacts during the remote build.
147-
PREVIEW_BUILDS_READ_TOKEN: ${{ secrets.PREVIEW_BUILDS_READ_TOKEN }}
145+
PREVIEW_BUILDS_ACCESS: ${{ vars.PREVIEW_BUILDS_ACCESS }}
148146
NEXT_TEST_PREFER_OFFLINE: 1
149147
NEXT_CI_RUNNER: ${{ inputs.runs_on_labels }}
150148
NEXT_TEST_PROXY_ADDRESS: ${{ inputs.overrideProxyAddress || '' }}

‎scripts/test-new-tests.mjs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import yargs from 'yargs'
44
import getChangedTests from './get-changed-tests.mjs'
55
import {
66
assertPreviewTarballPublished,
7+
createPreviewBuildsReadTokenGetter,
78
previewTarballUrl,
89
} from './wait-for-preview-tarball.mjs'
910

@@ -111,7 +112,7 @@ async function main() {
111112
await assertPreviewTarballPublished({
112113
commitSha,
113114
previewBuildsBaseUrl,
114-
readToken: process.env.PREVIEW_BUILDS_READ_TOKEN,
115+
getReadToken: createPreviewBuildsReadTokenGetter(),
115116
})
116117
}
117118

‎scripts/wait-for-preview-tarball.mjs‎

Lines changed: 117 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,76 @@ const DEFAULT_TIMEOUT_MINUTES = 30
1212
const POLL_INTERVAL_MS = 15_000
1313
const PROGRESS_LOG_INTERVAL_MS = 60_000
1414

15+
/**
16+
* Mints a GitHub Actions OIDC token for the given audience.
17+
* Returns null outside of GitHub Actions.
18+
*
19+
* @param {string} audience
20+
* @returns {Promise<string | null>}
21+
*/
22+
async function mintGitHubActionsOidcToken(audience) {
23+
const requestUrl = process.env.ACTIONS_ID_TOKEN_REQUEST_URL
24+
const requestToken = process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN
25+
if (!requestUrl || !requestToken) {
26+
return null
27+
}
28+
29+
const url = new URL(requestUrl)
30+
url.searchParams.set('audience', audience)
31+
const response = await fetch(url, {
32+
headers: { Authorization: `Bearer ${requestToken}` },
33+
})
34+
if (!response.ok) {
35+
throw new Error(
36+
`Failed to mint GitHub OIDC token: ${response.status} ${await response.text()}`
37+
)
38+
}
39+
40+
const { value } = await response.json()
41+
return value
42+
}
43+
44+
/**
45+
* Reads a preview-builds credential, minting a fresh one when needed.
46+
* vercel-packages accepts GitHub Actions OIDC tokens minted for its audience.
47+
* Public preview builds need no credentials and return null. Private builds
48+
* mint a GitHub Actions OIDC token, which expires about five minutes after
49+
* issuance, so the token is cached and re-minted before it expires rather
50+
* than minted once up front.
51+
*
52+
* @returns {() => Promise<string | null>}
53+
*/
54+
export function createPreviewBuildsReadTokenGetter() {
55+
if (process.env.PREVIEW_BUILDS_ACCESS !== 'private') {
56+
return async () => null
57+
}
58+
59+
let cachedToken = null
60+
let cachedTokenExpiresAt = 0
61+
62+
return async () => {
63+
if (cachedToken !== null && cachedTokenExpiresAt - 60_000 > Date.now()) {
64+
return cachedToken
65+
}
66+
const token = await mintGitHubActionsOidcToken(
67+
'https://vercel-packages.vercel.app'
68+
)
69+
if (token === null) {
70+
throw new Error(
71+
'Preview builds are private (PREVIEW_BUILDS_ACCESS=private) ' +
72+
'but no GitHub Actions OIDC token can be minted. ' +
73+
'Grant the job the `id-token: write` permission.'
74+
)
75+
}
76+
const payload = JSON.parse(
77+
Buffer.from(token.split('.')[1], 'base64url').toString()
78+
)
79+
cachedToken = token
80+
cachedTokenExpiresAt = payload.exp * 1000
81+
return token
82+
}
83+
}
84+
1585
/**
1686
* URL of the `next` preview tarball for a commit. `vercel-packages` answers
1787
* with a redirect to Vercel Blob, which only serves the tarball once
@@ -66,40 +136,54 @@ function commitChecksUrl(commitSha) {
66136
*
67137
* @param {string} url
68138
* @param {Record<string, string> | undefined} headers
69-
* @returns {Promise<{ published: boolean, lastResponse: string }>}
139+
* @returns {Promise<{ published: boolean, status: number | null, lastResponse: string, responseHeaders: string | null }>}
70140
*/
71141
async function probeTarball(url, headers) {
72142
try {
73143
const response = await fetch(url, { method: 'HEAD', headers })
74144
return {
75145
published: response.ok,
146+
status: response.status,
76147
lastResponse:
77148
response.status === 404 ? 'not published yet' : `${response.status}`,
149+
// Response headers carry request IDs that help debug failures.
150+
responseHeaders: response.ok
151+
? null
152+
: JSON.stringify(Object.fromEntries(response.headers)),
78153
}
79154
} catch (error) {
80155
return {
81156
published: false,
157+
status: null,
82158
lastResponse: `request failed (${error instanceof Error ? error.message : error})`,
159+
responseHeaders: null,
83160
}
84161
}
85162
}
86163

87164
/**
88-
* @param {string | undefined} readToken
89-
* @returns {Record<string, string> | undefined}
165+
* @param {() => Promise<string | null>} getReadToken
166+
* @returns {Promise<Record<string, string> | undefined>}
90167
*/
91-
function requestHeaders(readToken) {
168+
async function requestHeaders(getReadToken) {
169+
const readToken = await getReadToken()
92170
return readToken ? { Authorization: `Bearer ${readToken}` } : undefined
93171
}
94172

95173
/**
96174
* @param {object} options
97175
* @param {string} options.commitSha
98176
* @param {string} options.lastResponse
177+
* @param {string | null} [options.responseHeaders]
99178
* @param {number} [options.timeoutMs] Omitted when nothing was waited out.
100179
* @returns {Error}
101180
*/
102-
function notPublishedError({ commitSha, lastResponse, timeoutMs }) {
181+
function notPublishedError({
182+
commitSha,
183+
lastResponse,
184+
responseHeaders,
185+
timeoutMs,
186+
}) {
103187
const checksUrl = commitChecksUrl(commitSha)
104188
return new Error(
105189
`Preview tarball for commit ${commitSha} was not published` +
@@ -108,7 +192,8 @@ function notPublishedError({ commitSha, lastResponse, timeoutMs }) {
108192
`The tarball is published by the "upload-preview-tarballs" workflow ` +
109193
`once "build-and-deploy" has completed for this commit, so check ` +
110194
`whether that run failed or is still in progress.` +
111-
(checksUrl ? ` See ${checksUrl}` : '')
195+
(checksUrl ? ` See ${checksUrl}` : '') +
196+
(responseHeaders ? ` Response headers: ${responseHeaders}` : '')
112197
)
113198
}
114199

@@ -120,22 +205,22 @@ function notPublishedError({ commitSha, lastResponse, timeoutMs }) {
120205
* @param {object} options
121206
* @param {string} options.commitSha
122207
* @param {string} [options.previewBuildsBaseUrl]
123-
* @param {string} [options.readToken]
208+
* @param {() => Promise<string | null>} [options.getReadToken]
124209
* @returns {Promise<void>}
125210
*/
126211
export async function assertPreviewTarballPublished({
127212
commitSha,
128213
previewBuildsBaseUrl,
129-
readToken,
214+
getReadToken,
130215
}) {
131216
const url = previewTarballUrl(previewBuildsBaseUrl, commitSha)
132-
const { published, lastResponse } = await probeTarball(
217+
const { published, lastResponse, responseHeaders } = await probeTarball(
133218
url,
134-
requestHeaders(readToken)
219+
await requestHeaders(getReadToken ?? (async () => null))
135220
)
136221

137222
if (!published) {
138-
throw notPublishedError({ commitSha, lastResponse })
223+
throw notPublishedError({ commitSha, lastResponse, responseHeaders })
139224
}
140225

141226
console.info(`Preview tarball for commit ${commitSha} is available at ${url}`)
@@ -151,19 +236,18 @@ export async function assertPreviewTarballPublished({
151236
* @param {string} options.commitSha
152237
* @param {string} [options.previewBuildsBaseUrl]
153238
* @param {number} options.timeoutMs
154-
* @param {string} [options.readToken]
239+
* @param {() => Promise<string | null>} [options.getReadToken]
155240
* @param {number} [options.pollIntervalMs]
156241
* @returns {Promise<void>}
157242
*/
158243
export async function waitForPreviewTarball({
159244
commitSha,
160245
previewBuildsBaseUrl,
161246
timeoutMs,
162-
readToken,
247+
getReadToken = async () => null,
163248
pollIntervalMs = POLL_INTERVAL_MS,
164249
}) {
165250
const url = previewTarballUrl(previewBuildsBaseUrl, commitSha)
166-
const headers = requestHeaders(readToken)
167251
const startedAt = Date.now()
168252
const deadline = startedAt + timeoutMs
169253
let lastProgressLogAt = startedAt
@@ -173,7 +257,10 @@ export async function waitForPreviewTarball({
173257
)
174258

175259
for (;;) {
176-
const { published, lastResponse } = await probeTarball(url, headers)
260+
// A fresh token per probe: the OIDC token expires after five minutes,
261+
// well before the overall timeout.
262+
const { published, status, lastResponse, responseHeaders } =
263+
await probeTarball(url, await requestHeaders(getReadToken))
177264
const now = Date.now()
178265

179266
if (published) {
@@ -183,8 +270,21 @@ export async function waitForPreviewTarball({
183270
return
184271
}
185272

273+
if (status === 401 || status === 403) {
274+
// Retrying won't change the authorization outcome.
275+
throw new Error(
276+
`Not authorized to access the preview tarball at ${url} (last response: ${lastResponse}). ` +
277+
(responseHeaders ? `Response headers: ${responseHeaders}` : '')
278+
)
279+
}
280+
186281
if (now >= deadline) {
187-
throw notPublishedError({ commitSha, lastResponse, timeoutMs })
282+
throw notPublishedError({
283+
commitSha,
284+
lastResponse,
285+
responseHeaders,
286+
timeoutMs,
287+
})
188288
}
189289

190290
if (now - lastProgressLogAt >= PROGRESS_LOG_INTERVAL_MS) {
@@ -231,7 +331,7 @@ async function main() {
231331
commitSha,
232332
previewBuildsBaseUrl: values['preview-builds-base-url'],
233333
timeoutMs: timeoutMinutes * 60_000,
234-
readToken: process.env.PREVIEW_BUILDS_READ_TOKEN,
334+
getReadToken: createPreviewBuildsReadTokenGetter(),
235335
})
236336
}
237337

‎test/lib/next-modes/next-deploy.ts‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -510,18 +510,26 @@ export class NextDeployInstance extends NextInstance {
510510
this.parseIdsFromCliOuput()
511511
}
512512

513-
// When the preview-builds npm mirror is auth-protected, the deploy build
514-
// installs Next.js artifacts from it and needs credentials. We write an
515-
// `.npmrc` with a read token (provided as a CI secret) so the remote install
516-
// can authenticate. Only written when the token is set, so unprotected and
517-
// local deploy runs are unaffected.
513+
// When preview builds are private, the deploy build installs Next.js
514+
// artifacts from an auth-protected route and needs credentials. The build
515+
// authenticates with the Vercel OIDC token that Vercel automatically
516+
// provides to builds (vercel-packages accepts it for allowlisted teams), so
517+
// we write an `.npmrc` referencing it. Referencing the environment variable
518+
// instead of inlining a token keeps credentials out of the uploaded
519+
// deployment source. Only written for private preview builds since public
520+
// ones need no credentials and pnpm fails when an `.npmrc` references an
521+
// unset environment variable.
522+
// TODO: pnpm >= 10.34.2 no longer expands environment variables in
523+
// repository .npmrc files (GHSA-3qhv-2rgh-x77r). An install command writing
524+
// to the user-level pnpm config (like vercel/front does) did not work with
525+
// `vercel deploy` and needs more investigation.
518526
private async writeMirrorNpmrcIfNecessary(): Promise<void> {
519-
const token = process.env.PREVIEW_BUILDS_READ_TOKEN
520527
const baseUrlRaw = process.env.NEXT_TEST_PREVIEW_BUILDS_BASE_URL
528+
const access = process.env.PREVIEW_BUILDS_ACCESS
521529

522-
if (!token || !baseUrlRaw) {
530+
if (!baseUrlRaw || access !== 'private') {
523531
require('console').log(
524-
`Skipping .npmrc write for preview-builds mirror: missing token or base URL`
532+
`Skipping .npmrc write for preview-builds mirror: missing base URL or preview builds are public`
525533
)
526534
return
527535
}
@@ -536,7 +544,7 @@ export class NextDeployInstance extends NextInstance {
536544
)
537545
await fs.writeFile(
538546
path.join(this.testDir, '.npmrc'),
539-
`${registryKey}:_authToken=${token}\n`
547+
`${registryKey}:_authToken=\${VERCEL_OIDC_TOKEN}\n`
540548
)
541549
}
542550

0 commit comments

Comments
 (0)