Skip to content

Commit cd0aee9

Browse files
authored
Re-query deploy-test build logs until id markers appear (#95243)
Deploy tests recover the `BUILD_ID`, `DEPLOYMENT_ID` and `NEXT_SUPPORTS_IMMUTABLE_ASSETS` values by scraping them out of the deployment's build logs, where the test fixture's `post-build` script prints them as the final lines of the build. We fetch those logs with `vercel inspect --logs` after `vercel deploy` returns, but a deployment can report `Ready` before its full build-log tail has propagated to the log query API, so a single call sometimes returns a truncated prefix that stops before the markers. When that happens `parseIdsFromCliOuput` throws "Failed to get buildId from logs" and fails the whole suite during setup, which is the intermittent failure we have been seeing across the deploy jobs. This change wraps the log fetch in `fetchBuildLogsUntilComplete`, which re-queries `vercel inspect --logs` until the last-printed marker (`NEXT_SUPPORTS_IMMUTABLE_ASSETS`) appears before parsing, bounded to twenty attempts three seconds apart. Gating on the final marker rather than the first ensures a partial read cannot slip past the loop into the parser. Streaming the logs from `vercel deploy --logs` as part of the deploy itself was considered, but it has the same race: the CLI stops streaming once the deployment reports `Ready`, before the `post-build` tail flushes, and a single stream cannot re-query for the missing lines.
1 parent c5645f6 commit cd0aee9

1 file changed

Lines changed: 59 additions & 16 deletions

File tree

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

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import fs from 'fs-extra'
55
import { NextInstance } from './base'
66
import * as projectEnv from '../../../scripts/reset-project.mjs'
77
import { Span } from 'next/dist/trace'
8+
import { setTimeout } from 'timers/promises'
89

910
export class NextDeployInstance extends NextInstance {
1011
private _cliOutput: string
@@ -185,6 +186,54 @@ export class NextDeployInstance extends NextInstance {
185186
)
186187
}
187188

189+
private async fetchBuildLogsUntilComplete(
190+
url: string,
191+
vercelEnv: NodeJS.ProcessEnv,
192+
vercelFlags: string[]
193+
): Promise<string> {
194+
// The fixture's `post-build` script prints the BUILD_ID, DEPLOYMENT_ID and
195+
// NEXT_SUPPORTS_IMMUTABLE_ASSETS markers (in that order) as the final lines
196+
// of the build (see `base.ts`). A deployment can report `Ready` before that
197+
// tail has propagated to the log query API, so `vercel inspect --logs` can
198+
// return a truncated prefix that stops before the markers. Gate on the
199+
// last-printed marker so a partial read can't slip into the parser, and
200+
// re-query until it appears.
201+
const maxAttempts = 20
202+
const retryDelayMs = 3000
203+
let output = ''
204+
205+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
206+
const buildLogs = await execa(
207+
'vercel',
208+
['inspect', '--logs', url, ...vercelFlags],
209+
{
210+
env: vercelEnv,
211+
reject: false,
212+
}
213+
)
214+
if (buildLogs.exitCode !== 0) {
215+
throw new Error(`Failed to get build output logs: ${buildLogs.stderr}`)
216+
}
217+
// Build logs are piped to stderr, so combine both streams.
218+
output = buildLogs.stdout + buildLogs.stderr
219+
220+
if (/NEXT_SUPPORTS_IMMUTABLE_ASSETS: (.+)/.test(output)) {
221+
return output
222+
}
223+
224+
if (attempt < maxAttempts) {
225+
require('console').log(
226+
`Build log markers not yet propagated for ${url} (attempt ${attempt}/${maxAttempts}); the build log tail likely hasn't propagated yet. Retrying in ${retryDelayMs}ms...`
227+
)
228+
await setTimeout(retryDelayMs)
229+
}
230+
}
231+
232+
// The markers never appeared within the retry window; return the last
233+
// output so `parseIdsFromCliOuput` throws a descriptive error including it.
234+
return output
235+
}
236+
188237
public async setup(parentSpan: Span) {
189238
super.setup(parentSpan)
190239
await super.createTestDir({ parentSpan, skipInstall: true })
@@ -434,25 +483,19 @@ export class NextDeployInstance extends NextInstance {
434483

435484
require('console').log(`Deployment URL: ${this._url}`)
436485

437-
// Use the vercel inspect command to get the CLI output from the build.
438-
const buildLogs = await execa(
439-
'vercel',
440-
['inspect', '--logs', this._url, ...vercelFlags],
441-
{
442-
env: vercelEnv,
443-
reject: false,
444-
}
486+
// Fetch the build logs to extract the build/deployment id markers that the
487+
// fixture's `post-build` script prints. The deployment can report `Ready`
488+
// (and `vercel deploy` can return) before its full build-log tail has
489+
// propagated to the log query API, so re-query until the markers appear
490+
// rather than failing on the first incomplete read. TODO: Combine with
491+
// runtime logs (via `vercel logs`)
492+
this._cliOutput = await this.fetchBuildLogsUntilComplete(
493+
this._url,
494+
vercelEnv,
495+
vercelFlags
445496
)
446-
if (buildLogs.exitCode !== 0) {
447-
throw new Error(`Failed to get build output logs: ${buildLogs.stderr}`)
448-
}
449-
// TODO: Combine with runtime logs (via `vercel logs`)
450-
// Build logs seem to be piped to stderr, so we'll combine them to make sure we get all the logs.
451-
this._cliOutput = buildLogs.stdout + buildLogs.stderr
452497

453498
this.parseIdsFromCliOuput()
454-
// Use the stdout from the logs command as the CLI output. The CLI will
455-
// output other unrelated logs to stderr.
456499
}
457500

458501
// When the preview-builds npm mirror is auth-protected, the deploy build

0 commit comments

Comments
 (0)