diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 21ec7ca114..020a7deaaf 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -924,29 +924,50 @@ jobs: run: | FAILED=0 + # 0 when npm already has this version with these contents, 2 when it has it with others, 1 when it has none. + on_npm() { + local published + published="$(npm view "$1@${VERSION}" dist.integrity --registry https://registry.npmjs.org/ 2>/dev/null)" || return 1 + [ -n "$published" ] || return 1 + [ "$published" = "$2" ] || return 2 + } + + # A publish can fail on the way (a signing log that answers late, a dropped connection) with the package + # on npm or not. So each try asks npm first, and a failed try is tried again, three in all. publish_tarball() { local name="$1" local tarball="$2" - local integrity published + local integrity try state integrity="sha512-$(openssl dgst -sha512 -binary "$tarball" | base64 -w0)" - if published="$(npm view "${name}@${VERSION}" dist.integrity 2>/dev/null)" && [ -n "$published" ]; then - if [ "$published" = "$integrity" ]; then - echo "⏭️ ${name}@${VERSION} already published with these contents — skipping" - else + for try in 1 2 3 last; do + state=0 + on_npm "$name" "$integrity" || state=$? + if [ "$state" -eq 0 ]; then + if [ "$try" = 1 ]; then + echo "⏭️ ${name}@${VERSION} already published with these contents — skipping" + else + echo "✅ ${name}@${VERSION} is on npm with these contents" + fi + return 0 + fi + if [ "$state" -eq 2 ]; then echo "::error::${name}@${VERSION} is already on npm, and this run did not publish it. Find out who did before releasing this version." FAILED=1 + return 0 fi - return 0 - fi + [ "$try" != last ] || break - echo "📦 Publishing ${name}@${VERSION}..." - if npm publish "$tarball" --access public --tag "$DIST_TAG" --registry https://registry.npmjs.org/ --ignore-scripts; then - echo "✅ ${name}@${VERSION} published" - else - echo "❌ ${name}@${VERSION} failed to publish" - FAILED=1 - fi + echo "📦 Publishing ${name}@${VERSION} (try ${try} of 3)..." + if npm publish "$tarball" --access public --tag "$DIST_TAG" --registry https://registry.npmjs.org/ --ignore-scripts; then + echo "✅ ${name}@${VERSION} published" + return 0 + fi + echo "::warning::${name}@${VERSION} did not publish on try ${try}" + sleep "${PUBLISH_RETRY_SECONDS:-20}" + done + echo "❌ ${name}@${VERSION} failed to publish" + FAILED=1 } while IFS=$'\t' read -r name tarball; do diff --git a/scripts/publish-workflow.test.mjs b/scripts/publish-workflow.test.mjs index 7283e5d6bd..8ad667032f 100644 --- a/scripts/publish-workflow.test.mjs +++ b/scripts/publish-workflow.test.mjs @@ -859,6 +859,85 @@ test("publishing skips only identical uploads and fails on a version it did not assert.doesNotMatch(rerun.calls, /npm publish/); }); +test("a publish that fails on the way is tried again, three times in all, asking npm first each time", () => { + const step = stepOf(publish, "Publish packages"); + // The fake npm fails its first `failures` publishes; with `lands` the package is on npm after a failed one. + const run = ({ failures, lands = "" }) => { + const temp = mkdtempSync(join(tmpdir(), "hyperframes-publish-retry-")); + try { + const bin = join(temp, "bin"); + mkdirSync(bin); + const log = join(temp, "npm.log"); + const seen = join(temp, "on-npm"); + writeFileSync(log, ""); + writeFileSync( + join(bin, "npm"), + `#!/bin/sh +echo "npm $1" >> "${log}" +if [ "$1" = view ]; then [ -s "${seen}" ] || exit 1; cat "${seen}"; exit 0; fi +tried=$(grep -c "^npm publish" "${log}") +if [ "$tried" -le "$FAILURES" ]; then [ -z "$LANDS" ] || echo "$LANDS" > "${seen}"; exit 1; fi +echo "$INTEGRITY" > "${seen}" +`, + ); + chmodSync(join(bin, "npm"), 0o755); + packTarball(temp, "core.tgz", { name: "@hyperframes/core", version: "1.2.3-alpha.1" }); + writeFileSync( + join(temp, "publish-order.tsv"), + `@hyperframes/core\t${join(temp, "core.tgz")}\n`, + ); + const integrity = `sha512-${createHash("sha512") + .update(readFileSync(join(temp, "core.tgz"))) + .digest("base64")}`; + const result = runStep(step, { + cwd: temp, + path: `${bin}:${process.env.PATH}`, + env: { + VERSION: "1.2.3-alpha.1", + DIST_TAG: "alpha", + NODE_AUTH_TOKEN: "", + PUBLISH_RETRY_SECONDS: "0", + FAILURES: String(failures), + LANDS: lands === "same" ? integrity : lands, + INTEGRITY: integrity, + }, + }); + const calls = readFileSync(log, "utf8").trim().split("\n"); + return { ...result, calls, publishes: calls.filter((c) => c === "npm publish").length }; + } finally { + rmSync(temp, { recursive: true, force: true }); + } + }; + + // fails without the fix: one failed publish failed the release, with the rest of the packages already out. + const second = run({ failures: 1 }); + assert.equal(second.status, 0, second.stdout + second.stderr); + assert.equal(second.publishes, 2); + assert.deepEqual(second.calls, ["npm view", "npm publish", "npm view", "npm publish"]); + + const third = run({ failures: 2 }); + assert.equal(third.status, 0, third.stdout); + assert.equal(third.publishes, 3); + + const never = run({ failures: 9 }); + assert.equal(never.status, 1); + assert.equal(never.publishes, 3); + assert.match(never.stdout, /failed to publish/); + + // The publish answered with a failure but the package went out: it is not sent a second time. + const landed = run({ failures: 9, lands: "same" }); + assert.equal(landed.status, 0, landed.stdout); + assert.equal(landed.publishes, 1); + const landedLast = run({ failures: 9, lands: "" }); + assert.equal(landedLast.calls.at(-1), "npm view", "npm is asked once more after the last try"); + + // Someone else's contents under this version, seen between tries: no further try, and the release fails. + const others = run({ failures: 9, lands: "sha512-other" }); + assert.equal(others.status, 1); + assert.equal(others.publishes, 1); + assert.match(others.stdout, /this run did not publish it/); +}); + test("every publish runs in npm-publish, the environment npm trusted publishing names", () => { assert.equal(publish.environment, "npm-publish"); const jobsWithEnvironment = Object.entries(config.jobs).filter(([, job]) => job.environment);