From e4639b7003527776a785a092ac7fd3b0d99face7 Mon Sep 17 00:00:00 2001 From: bliatun-code <270035675+bliatun-code@users.noreply.github.com> Date: Wed, 2 Dec 2026 21:47:20 +0100 Subject: [PATCH] Retry a missing Windows release artifact after upload --- .github/workflows/release-build.yml | 18 ++++++-- scripts/download-windows-release-artifact.mjs | 34 ++++++++++++++ scripts/release-workflow-contract.test.mjs | 45 +++++++++++++++++-- 3 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 scripts/download-windows-release-artifact.mjs diff --git a/.github/workflows/release-build.yml b/.github/workflows/release-build.yml index 4234bb7f..b0b03d99 100644 --- a/.github/workflows/release-build.yml +++ b/.github/workflows/release-build.yml @@ -598,6 +598,9 @@ jobs: if: github.event_name == 'push' || github.event.inputs.platform == 'both' || github.event.inputs.platform == 'windows' runs-on: windows-2025 timeout-minutes: 60 + permissions: + actions: read + contents: read outputs: installer-sha256: ${{ steps.verify.outputs.installer-sha256 }} payload-sha256: ${{ steps.verify.outputs.payload-sha256 }} @@ -693,6 +696,7 @@ jobs: "payload-sha256=$($checked.PayloadSha256)" >> $env:GITHUB_OUTPUT - name: Upload verified NSIS artifact + id: upload-nsis uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: filament-manager-windows-nsis-${{ github.run_id }} @@ -704,10 +708,16 @@ jobs: retention-days: 14 - name: Download release NSIS candidate - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 - with: - name: filament-manager-windows-nsis-${{ github.run_id }} - path: ${{ runner.temp }}/filament-manager-windows-release-candidate + shell: pwsh + env: + GH_TOKEN: ${{ github.token }} + ARTIFACT_ID: ${{ steps.upload-nsis.outputs.artifact-id }} + ARTIFACT_SHA256: ${{ steps.upload-nsis.outputs.artifact-digest }} + run: | + $archive = Join-Path $env:RUNNER_TEMP "filament-manager-windows-release.zip" + node ./scripts/download-windows-release-artifact.mjs "$archive" + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + Expand-Archive -LiteralPath $archive -DestinationPath (Join-Path $env:RUNNER_TEMP "filament-manager-windows-release-candidate") - name: Verify downloaded NSIS candidate id: candidate diff --git a/scripts/download-windows-release-artifact.mjs b/scripts/download-windows-release-artifact.mjs new file mode 100644 index 00000000..962b0c9f --- /dev/null +++ b/scripts/download-windows-release-artifact.mjs @@ -0,0 +1,34 @@ +import { createHash } from "node:crypto"; +import { writeFile } from "node:fs/promises"; +import { setTimeout as delay } from "node:timers/promises"; + +export async function downloadWindowsReleaseArtifact(env, request = fetch, wait = delay) { + const { ARTIFACT_ID, ARTIFACT_SHA256, GITHUB_API_URL, GITHUB_REPOSITORY, GH_TOKEN } = env; + if (!/^\d+$/.test(ARTIFACT_ID) || !/^[a-f\d]{64}$/i.test(ARTIFACT_SHA256) || !GH_TOKEN) { + throw new Error("Missing artifact ID, upload digest or Actions token."); + } + const url = `${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/actions/artifacts/${ARTIFACT_ID}/zip`; + for (let attempt = 1; ; attempt++) { + const response = await request(url, { + headers: { Authorization: `Bearer ${GH_TOKEN}` }, + signal: AbortSignal.timeout(120_000), + }); + if (response.status === 404 && attempt < 5) { + await response.body?.cancel(); + const seconds = 2 ** attempt; + console.warn(`Artifact ${ARTIFACT_ID} not found (attempt ${attempt}/5). Retrying in ${seconds}s.`); + await wait(seconds * 1000); + continue; + } + if (!response.ok) throw new Error(`Artifact ${ARTIFACT_ID} download failed (HTTP ${response.status}).`); + const archive = Buffer.from(await response.arrayBuffer()); + if (createHash("sha256").update(archive).digest("hex") !== ARTIFACT_SHA256.toLowerCase()) { + throw new Error(`Artifact ${ARTIFACT_ID} does not match the uploaded archive SHA-256.`); + } + return archive; + } +} + +if (import.meta.main) { + await writeFile(process.argv[2], await downloadWindowsReleaseArtifact(process.env), { flag: "wx" }); +} diff --git a/scripts/release-workflow-contract.test.mjs b/scripts/release-workflow-contract.test.mjs index 7ed44183..0bc5d1a3 100644 --- a/scripts/release-workflow-contract.test.mjs +++ b/scripts/release-workflow-contract.test.mjs @@ -1,6 +1,8 @@ import assert from "node:assert/strict"; import { existsSync, readFileSync } from "node:fs"; import test from "node:test"; +import { createHash } from "node:crypto"; +import { downloadWindowsReleaseArtifact } from "./download-windows-release-artifact.mjs"; const releaseWorkflow = readFileSync( ".github/workflows/release-build.yml", @@ -274,7 +276,11 @@ test("release workflow gates tag and manual installer builds", () => { assert.match(windowsJob, /\$checked\.PayloadSha256 -cne \$env:NSIS_PAYLOAD_SHA256/); assert.match(windowsJob, /name: filament-manager-windows-nsis-\$\{\{ github\.run_id \}\}/); assert.match(windowsJob, /- name: Upload verified NSIS artifact[\s\S]*?if-no-files-found: error\s+overwrite: true\s+retention-days: 14/); - assert.match(windowsJob, /- name: Download release NSIS candidate[\s\S]*?actions\/download-artifact@[a-f0-9]{40} # v8\.\d+\.\d+[\s\S]*?name: filament-manager-windows-nsis-\$\{\{ github\.run_id \}\}[\s\S]*?filament-manager-windows-release-candidate/); + assert.match(windowsJob, /- name: Upload verified NSIS artifact\s+id: upload-nsis/); + const download = readSection(windowsJob, "- name: Download release NSIS candidate", "- name: Verify downloaded NSIS candidate"); + assert.match(download, /ARTIFACT_ID: \$\{\{ steps\.upload-nsis\.outputs\.artifact-id \}\}/); + assert.match(download, /ARTIFACT_SHA256: \$\{\{ steps\.upload-nsis\.outputs\.artifact-digest \}\}/); + assert.match(download, /download-windows-release-artifact\.mjs "\$archive"\s+if \(\$LASTEXITCODE -ne 0\) \{ exit \$LASTEXITCODE \}\s+Expand-Archive[^\n]+filament-manager-windows-release-candidate/); assert.match(windowsJob, /path: \|\s+\$\{\{ runner\.temp \}\}\/filament-manager-windows-release-staging\/\*\.exe\s+\$\{\{ runner\.temp \}\}\/filament-manager-windows-release-staging\/SHA256SUMS-windows\.txt/); assert.match(windowsJob, /NSIS_INSTALLER: \$\{\{ steps\.candidate\.outputs\.installer \}\}/); assert.match(windowsJob, /\.\/scripts\/smoke-windows-msi\.ps1/); @@ -670,7 +676,7 @@ test("release artifacts remain stable across partial workflow reruns", () => { ); assert.equal( countOccurrences(releaseWorkflow, `name: ${windowsArtifactName}`), - 5, + 4, ); assert.equal( countOccurrences(releaseWorkflow, `name: ${sbomArtifactName}`), @@ -699,7 +705,7 @@ test("release artifacts remain stable across partial workflow reruns", () => { ); assert.match( windowsJob, - /- name: Download release NSIS candidate[\s\S]*?name: filament-manager-windows-nsis-\$\{\{ github\.run_id \}\}/, + /- name: Download release NSIS candidate[\s\S]*?ARTIFACT_ID: \$\{\{ steps\.upload-nsis\.outputs\.artifact-id \}\}/, ); assert.match( sbomJob, @@ -2101,3 +2107,36 @@ test("partial Host-Client work deletion resumes only through an authorized clean assert.match(windowsMsiSmoke, /credentialCleanupResult\.process_termination_confirmed -ne \$true/); assert.match(windowsMsiSmoke, /if \(\$packagedHostClientE2eExitCode -ne 0\) \{\s+throw/); }); + +test("Windows release download retries only missing artifacts and checks the upload digest", async () => { + const archive = Buffer.from("release archive"); + const env = { + GITHUB_API_URL: "https://api.github.test", GITHUB_REPOSITORY: "owner/repo", + ARTIFACT_ID: "123", GH_TOKEN: "test-token", + ARTIFACT_SHA256: createHash("sha256").update(archive).digest("hex"), + }; + async function run(statuses, settings = env) { + const waits = []; + const download = downloadWindowsReleaseArtifact(settings, async (url, options) => { + assert.equal(url, "https://api.github.test/repos/owner/repo/actions/artifacts/123/zip"); + assert.equal(options.headers.Authorization, "Bearer test-token"); + assert.ok(statuses.length, "unexpected extra download"); + return new Response(archive, { status: statuses.shift() }); + }, async ms => { waits.push(ms); }); + return { download, waits }; + } + for (const [statuses, delays] of [[[200], []], [[404, 404, 200], [2000, 4000]]]) { + const { download, waits } = await run(statuses); + assert.deepEqual(await download, archive); + assert.deepEqual(waits, delays); + } + for (const [statuses, digest, error, delays] of [ + [[404, 404, 404, 404, 404], env.ARTIFACT_SHA256, /HTTP 404/, [2000, 4000, 8000, 16000]], + [[403], env.ARTIFACT_SHA256, /HTTP 403/, []], + [[200], "0".repeat(64), /SHA-256/, []], + ]) { + const { download, waits } = await run(statuses, { ...env, ARTIFACT_SHA256: digest }); + await assert.rejects(download, error); + assert.deepEqual(waits, delays); + } +});