Unverified Commit 1da7a0a0 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[15.5] [ci] Use OIDC tokens to read private preview builds (#97259)

Backports https://github.com/vercel/next.js/pull/96919

🤖 Generated with [Claude Code](https://claude.com/claude-code)
parent 70ab0fe6
...@@ -603,6 +603,11 @@ jobs: ...@@ -603,6 +603,11 @@ jobs:
matrix: matrix:
group: [1/5, 2/5, 3/5, 4/5, 5/5] group: [1/5, 2/5, 3/5, 4/5, 5/5]
permissions:
contents: read
# Minting GitHub OIDC tokens to read private preview builds. Dormant in
# this repo since builds here are public.
id-token: write
uses: ./.github/workflows/build_reusable.yml uses: ./.github/workflows/build_reusable.yml
with: with:
afterBuild: | afterBuild: |
......
...@@ -105,9 +105,7 @@ env: ...@@ -105,9 +105,7 @@ env:
VERCEL_TEST_TEAM: vtest314-next-e2e-tests VERCEL_TEST_TEAM: vtest314-next-e2e-tests
NEXT_TEST_PREFER_OFFLINE: 1 NEXT_TEST_PREFER_OFFLINE: 1
NEXT_CI_RUNNER: ${{ inputs.runs_on_labels }} NEXT_CI_RUNNER: ${{ inputs.runs_on_labels }}
# Read token for the auth-protected preview-builds npm mirror, used by deploy PREVIEW_BUILDS_ACCESS: ${{ vars.PREVIEW_BUILDS_ACCESS }}
# tests to install Next.js artifacts during the remote build.
PREVIEW_BUILDS_READ_TOKEN: ${{ secrets.PREVIEW_BUILDS_READ_TOKEN }}
jobs: jobs:
build: build:
......
...@@ -3,6 +3,38 @@ import execa from 'execa' ...@@ -3,6 +3,38 @@ import execa from 'execa'
import yargs from 'yargs' import yargs from 'yargs'
import getChangedTests from './get-changed-tests.mjs' import getChangedTests from './get-changed-tests.mjs'
/**
* Mints a GitHub Actions OIDC token for the given audience.
*
* @param {string} audience
* @returns {Promise<string>}
*/
async function mintGitHubActionsOidcToken(audience) {
const requestUrl = process.env.ACTIONS_ID_TOKEN_REQUEST_URL
const requestToken = process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN
if (!requestUrl || !requestToken) {
throw new Error(
'Preview builds are private (PREVIEW_BUILDS_ACCESS=private) ' +
'but no GitHub Actions OIDC token can be minted. ' +
'Grant the job the `id-token: write` permission.'
)
}
const url = new URL(requestUrl)
url.searchParams.set('audience', audience)
const response = await fetch(url, {
headers: { authorization: `Bearer ${requestToken}` },
})
if (!response.ok) {
throw new Error(
`Failed to mint GitHub OIDC token: ${response.status} ${await response.text()}`
)
}
const { value } = await response.json()
return value
}
/** /**
* Run tests for added/changed tests in the current branch * Run tests for added/changed tests in the current branch
* CLI Options: * CLI Options:
...@@ -90,10 +122,14 @@ async function main() { ...@@ -90,10 +122,14 @@ async function main() {
? `${previewBuildsBaseUrl}/commits/${commitSha}/next` ? `${previewBuildsBaseUrl}/commits/${commitSha}/next`
: undefined : undefined
const previewBuildsReadToken = process.env.PREVIEW_BUILDS_READ_TOKEN
if (nextTestVersion) { if (nextTestVersion) {
console.log(`Verifying artifacts for commit ${commitSha}`) console.log(`Verifying artifacts for commit ${commitSha}`)
// Private preview builds authenticate with a GitHub Actions OIDC token
// minted on demand. Public builds need no credentials.
const previewBuildsReadToken =
process.env.PREVIEW_BUILDS_ACCESS === 'private'
? await mintGitHubActionsOidcToken('https://vercel-packages.vercel.app')
: null
// Attempt to fetch the deploy artifacts for the commit // Attempt to fetch the deploy artifacts for the commit
// These might take a moment to become available, so we'll retry a few times // These might take a moment to become available, so we'll retry a few times
const fetchHeaders = previewBuildsReadToken const fetchHeaders = previewBuildsReadToken
......
...@@ -169,18 +169,26 @@ export class NextDeployInstance extends NextInstance { ...@@ -169,18 +169,26 @@ export class NextDeployInstance extends NextInstance {
this._cliOutput = buildLogs.stdout + buildLogs.stderr this._cliOutput = buildLogs.stdout + buildLogs.stderr
} }
// When the preview-builds npm mirror is auth-protected, the deploy build // When preview builds are private, the deploy build installs Next.js
// installs Next.js artifacts from it and needs credentials. We write an // artifacts from an auth-protected route and needs credentials. The build
// `.npmrc` with a read token (provided as a CI secret) so the remote install // authenticates with the Vercel OIDC token that Vercel automatically
// can authenticate. Only written when the token is set, so unprotected and // provides to builds (vercel-packages accepts it for allowlisted teams), so
// local deploy runs are unaffected. // we write an `.npmrc` referencing it. Referencing the environment variable
// instead of inlining a token keeps credentials out of the uploaded
// deployment source. Only written for private preview builds since public
// ones need no credentials and pnpm fails when an `.npmrc` references an
// unset environment variable.
// TODO: pnpm >= 10.34.2 no longer expands environment variables in
// repository .npmrc files (GHSA-3qhv-2rgh-x77r). An install command writing
// to the user-level pnpm config (like vercel/front does) did not work with
// `vercel deploy` and needs more investigation.
private async writeMirrorNpmrcIfNecessary(): Promise<void> { private async writeMirrorNpmrcIfNecessary(): Promise<void> {
const token = process.env.PREVIEW_BUILDS_READ_TOKEN
const baseUrlRaw = process.env.NEXT_TEST_PREVIEW_BUILDS_BASE_URL const baseUrlRaw = process.env.NEXT_TEST_PREVIEW_BUILDS_BASE_URL
const access = process.env.PREVIEW_BUILDS_ACCESS
if (!token || !baseUrlRaw) { if (!baseUrlRaw || access !== 'private') {
require('console').log( require('console').log(
`Skipping .npmrc write for preview-builds mirror: missing token or base URL` `Skipping .npmrc write for preview-builds mirror: missing base URL or preview builds are public`
) )
return return
} }
...@@ -195,7 +203,7 @@ export class NextDeployInstance extends NextInstance { ...@@ -195,7 +203,7 @@ export class NextDeployInstance extends NextInstance {
) )
await fs.writeFile( await fs.writeFile(
path.join(this.testDir, '.npmrc'), path.join(this.testDir, '.npmrc'),
`${registryKey}:_authToken=${token}\n` `${registryKey}:_authToken=\${VERCEL_OIDC_TOKEN}\n`
) )
} }
......
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