diff --git a/.github/actions/wait-e2e-web-build/action.yml b/.github/actions/wait-e2e-web-build/action.yml index 6d06a0f4095278..3e49d19b951589 100644 --- a/.github/actions/wait-e2e-web-build/action.yml +++ b/.github/actions/wait-e2e-web-build/action.yml @@ -18,4 +18,5 @@ runs: result-encoding: string script: | const waitForBuild = require(`${process.env.E2E_WAIT_ACTION_PATH}/wait.cjs`) - return await waitForBuild({ github, context, core, runAttempt: Number(process.env.GITHUB_RUN_ATTEMPT) }) + const artifact = await waitForBuild({ github, context, core, runAttempt: Number(process.env.GITHUB_RUN_ATTEMPT) }) + return artifact.id diff --git a/.github/actions/wait-e2e-web-build/download.cjs b/.github/actions/wait-e2e-web-build/download.cjs index 92de8b9eed56fe..21c759d69e591e 100644 --- a/.github/actions/wait-e2e-web-build/download.cjs +++ b/.github/actions/wait-e2e-web-build/download.cjs @@ -35,30 +35,13 @@ async function downloadWebBuild({ timeout, maxBuffer: 32 * 1024 * 1024, }) - const api = async (endpoint, paginate = false) => { - const { stdout } = await run('gh', [ - 'api', - endpoint, - ...(paginate ? ['--paginate', '--slurp'] : []), - ]) + const list = async (endpoint) => { + const { stdout } = await run('gh', ['api', endpoint, '--paginate', '--slurp']) return JSON.parse(stdout) } - // An artifact can be listed for the run before its own endpoint and download are - // readable (seen as a 404 right after upload), so retry those reads briefly. - const withRetry = async (operation) => { - for (let attempt = 1; ; attempt++) { - try { - return await operation() - } catch (error) { - if (attempt >= 3) throw error - console.log(`Web build artifact not readable yet (attempt ${attempt}): ${error.message}`) - await sleep(attempt * 5_000) - } - } - } const prefix = `repos/${repository}/actions` let summary = '' - const artifactId = await waitForBuild({ + const artifact = await waitForBuild({ github: { rest: { actions: { listJobsForWorkflowRunAttempt: 'jobs', listWorkflowRunArtifacts: 'artifacts' }, @@ -68,7 +51,7 @@ async function downloadWebBuild({ kind === 'jobs' ? `${prefix}/runs/${runId}/attempts/${runAttempt}/jobs?per_page=100` : `${prefix}/runs/${runId}/artifacts?per_page=100` - const pages = await api(endpoint, true) + const pages = await list(endpoint) return pages.flatMap((page) => page[kind]) }, }, @@ -92,30 +75,34 @@ async function downloadWebBuild({ runAttempt, }) - const artifact = await withRetry(() => api(`${prefix}/artifacts/${artifactId}`)) const directory = await mkdtemp(path.join(os.tmpdir(), 'dify-e2e-web-build-')) try { - // Artifact names are immutable within a run. Use the name selected by wait.cjs, - // including its matching upload timestamp when only consumer jobs are rerun. - await withRetry(() => - run( - 'gh', - [ - 'run', - 'download', - runId, - '--repo', - repository, - '--name', - artifact.name, - '--dir', - directory, - ], - 300_000, - ), - ) + // The run's artifact list already provides the immutable name. The separate + // metadata endpoint can return 404 after the artifact has appeared in that list. + // The archive can also take a moment to become readable after upload. + const downloadArgs = [ + 'run', + 'download', + runId, + '--repo', + repository, + '--name', + artifact.name, + '--dir', + directory, + ] + for (let attempt = 1; ; attempt++) { + try { + await run('gh', downloadArgs, 300_000) + break + } catch (error) { + if (attempt >= 3) throw error + console.log(`Web build artifact not readable yet (attempt ${attempt}): ${error.message}`) + await sleep(attempt * 5_000) + } + } await run('tar', ['-xf', path.join(directory, 'e2e-web-build.tar'), '-C', webDir]) - console.log(`Web build artifact ${artifactId} extracted and ready.`) + console.log(`Web build artifact ${artifact.id} extracted and ready.`) } finally { await rm(directory, { force: true, recursive: true }) } diff --git a/.github/actions/wait-e2e-web-build/download.test.cjs b/.github/actions/wait-e2e-web-build/download.test.cjs index 4b558beaaded55..b68910f91ebaca 100644 --- a/.github/actions/wait-e2e-web-build/download.test.cjs +++ b/.github/actions/wait-e2e-web-build/download.test.cjs @@ -3,14 +3,9 @@ const { access } = require('node:fs/promises') const { test } = require('node:test') const downloadWebBuild = require('./download.cjs') -function fixture({ - downloadFails = false, - producerFails = false, - artifactMissingOnce = false, -} = {}) { +function fixture({ downloadFailures = 0, producerFails = false } = {}) { const calls = [] const sleeps = [] - let artifactLookups = 0 const env = { GITHUB_REPOSITORY: 'owner/repo', GITHUB_RUN_ID: '123', @@ -21,7 +16,10 @@ function fixture({ calls.push({ command, args }) if (command === 'tar') return { stdout: '' } if (args[0] === 'run') { - if (downloadFails) throw new Error('artifact download failed') + if (downloadFailures > 0) { + downloadFailures-- + throw new Error('artifact download failed') + } return { stdout: '' } } if (args[1].includes('/jobs?')) { @@ -64,10 +62,7 @@ function fixture({ ]), } } - assert.equal(args[1], 'repos/owner/repo/actions/artifacts/42') - artifactLookups++ - if (artifactMissingOnce && artifactLookups === 1) throw new Error('gh: Not Found (HTTP 404)') - return { stdout: JSON.stringify({ name: 'core-e2e-web-build-2' }) } + throw new Error(`Unexpected command: ${command} ${args.join(' ')}`) } const sleep = async (ms) => { sleeps.push(ms) @@ -75,9 +70,17 @@ function fixture({ return { calls, sleeps, options: { env, execute, webDir: '/test/web', sleep } } } -test('downloads the producer-selected artifact on consumer-only reruns, including paginated results', async () => { +test('downloads the producer-selected artifact on consumer-only reruns without a second metadata lookup', async () => { const { calls, options } = fixture() - await downloadWebBuild(options) + await downloadWebBuild({ + ...options, + async execute(command, args) { + if (args[0] === 'api' && args[1].endsWith('/artifacts/42')) { + throw new Error('gh: Not Found (HTTP 404)') + } + return options.execute(command, args) + }, + }) assert.match(calls[0].args[1], /runs\/123\/attempts\/4\/jobs/) const download = calls.find(({ args }) => args[0] === 'run') assert.deepEqual(download.args.slice(0, 7), [ @@ -95,15 +98,16 @@ test('downloads the producer-selected artifact on consumer-only reruns, includin await assert.rejects(access(download.args.at(-1)), { code: 'ENOENT' }) }) -test('retries an artifact that is listed before its endpoint is readable', async () => { - const { calls, sleeps, options } = fixture({ artifactMissingOnce: true }) +test('retries a listed artifact whose archive is temporarily unreadable', async () => { + const { calls, sleeps, options } = fixture({ downloadFailures: 1 }) await downloadWebBuild(options) assert.deepEqual(sleeps, [5_000]) + assert.equal(calls.filter(({ args }) => args[0] === 'run').length, 2) assert.equal(calls.at(-1).command, 'tar') }) test('does not extract a failed download and removes its temporary directory', async () => { - const { calls, sleeps, options } = fixture({ downloadFails: true }) + const { calls, sleeps, options } = fixture({ downloadFailures: 3 }) await assert.rejects(downloadWebBuild(options), /artifact download failed/) assert.deepEqual(sleeps, [5_000, 10_000]) assert.equal(calls.filter(({ args }) => args[0] === 'run').length, 3) diff --git a/.github/actions/wait-e2e-web-build/wait.cjs b/.github/actions/wait-e2e-web-build/wait.cjs index 9d8174bc52bd5f..7fc25ad861786b 100644 --- a/.github/actions/wait-e2e-web-build/wait.cjs +++ b/.github/actions/wait-e2e-web-build/wait.cjs @@ -59,7 +59,7 @@ module.exports = async function waitForBuild({ .addHeading('E2E Web build wait') .addRaw(`Artifact wait: ${seconds}s`) .write() - return String(artifact.id) + return { id: String(artifact.id), name: artifact.name } } } diff --git a/.github/actions/wait-e2e-web-build/wait.test.cjs b/.github/actions/wait-e2e-web-build/wait.test.cjs index de9a5c222204da..c1dd659038f805 100644 --- a/.github/actions/wait-e2e-web-build/wait.test.cjs +++ b/.github/actions/wait-e2e-web-build/wait.test.cjs @@ -73,7 +73,7 @@ const successfulProducer = { test('starts consuming the current artifact without waiting for producer cache saves', async () => { const { options, calls } = fixture({ artifacts: (elapsed) => (elapsed ? [artifact] : []) }) - assert.equal(await waitForBuild(options), '42') + assert.deepEqual(await waitForBuild(options), { id: '42', name: 'core-e2e-web-build-2' }) assert.equal(options.now(), 10000) assert.deepEqual( calls.map((call) => call.endpoint), @@ -91,7 +91,7 @@ test('full reruns ignore the old artifact until the rebuilding producer publishe const { options } = fixture({ artifacts: (elapsed) => (elapsed ? [oldArtifact, artifact] : [oldArtifact]), }) - assert.equal(await waitForBuild(options), '42') + assert.equal((await waitForBuild(options)).id, '42') assert.equal(options.now(), 10000) }) @@ -109,7 +109,7 @@ test('consumer-only reruns reuse the actual successful upload despite a relabele artifacts: [oldArtifact, artifact], jobs: [{ ...successfulProducer, run_attempt: 4 }], }) - assert.equal(await waitForBuild(options), '42') + assert.deepEqual(await waitForBuild(options), { id: '42', name: 'core-e2e-web-build-2' }) assert.equal(options.now(), 0) }) @@ -137,7 +137,7 @@ test('ignores unrelated and expired artifacts and stops at the deadline', async test('completed rebuilt producers select their upload instead of the old artifact', async () => { const { options } = fixture({ artifacts: [oldArtifact, artifact], jobs: [successfulProducer] }) - assert.equal(await waitForBuild(options), '42') + assert.equal((await waitForBuild(options)).id, '42') }) test('allows artifact visibility to catch up after a successful producer finishes', async () => { @@ -145,7 +145,7 @@ test('allows artifact visibility to catch up after a successful producer finishe artifacts: (elapsed) => (elapsed ? [oldArtifact, artifact] : [oldArtifact]), jobs: [successfulProducer], }) - assert.equal(await waitForBuild(options), '42') + assert.equal((await waitForBuild(options)).id, '42') assert.equal(options.now(), 10000) }) @@ -185,5 +185,5 @@ test('finds the upload when GitHub skews the upload step timestamps off the arti }, ], }) - assert.equal(await waitForBuild(options), '42') + assert.equal((await waitForBuild(options)).id, '42') }) diff --git a/.github/workflows/web-e2e.yml b/.github/workflows/web-e2e.yml index 73d6182688be0f..27faf3ca41ec26 100644 --- a/.github/workflows/web-e2e.yml +++ b/.github/workflows/web-e2e.yml @@ -166,11 +166,10 @@ jobs: if: ${{ !cancelled() }} uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: - name: e2e-logs-chromium-${{ matrix.shard }} + name: e2e-logs-chromium-${{ matrix.shard }}-attempt-${{ github.run_attempt }} path: e2e/.logs/*.log if-no-files-found: ignore include-hidden-files: true - overwrite: true retention-days: 7 webkit-smoke: @@ -244,11 +243,10 @@ jobs: if: ${{ !cancelled() }} uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: - name: e2e-logs-webkit + name: e2e-logs-webkit-attempt-${{ github.run_attempt }} path: e2e/.logs/*.log if-no-files-found: ignore include-hidden-files: true - overwrite: true retention-days: 7 external-runtime: