fix(release): retry bounded Gitea uploads and support mirror recovery
This commit is contained in:
1 parent
07031f58a0
commit
9a9e857606
3 files changed
+154
-20
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { afterEach, describe, expect, test } from "bun:test";
|
||||
import { execFileSync, spawnSync } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
@@ -32,6 +33,48 @@ function repository() {
|
||||
return { root, git, version: () => JSON.parse(readFileSync(join(root, "package.json"), "utf8")).version };
|
||||
}
|
||||
|
||||
function mirrorFixture(mode: "retry" | "skip" | "concurrency", mirrorOnly = false) {
|
||||
const f = repository();
|
||||
const tools = join(f.root, "tools"), published = join(f.root, "published");
|
||||
mkdirSync(tools); mkdirSync(published); mkdirSync(join(f.root, "dist/bin"), { recursive: true });
|
||||
const names = mode === "concurrency" ? Array.from({ length: 5 }, (_, i) => `hose-use-fixture-${i}`) : ["hose-use-linux-x64"];
|
||||
const manifest = names.map(name => {
|
||||
const data = `published bytes for ${name}`;
|
||||
writeFileSync(join(published, name), data);
|
||||
return `${createHash("sha256").update(data).digest("hex")} ${name}\n`;
|
||||
}).join("");
|
||||
writeFileSync(join(published, "checksums.txt"), manifest);
|
||||
const files = [...names, "checksums.txt"];
|
||||
if (!mirrorOnly) for (const name of files) writeFileSync(join(f.root, "dist/bin", name), readFileSync(join(published, name)));
|
||||
writeFileSync(join(f.root, "info.json"), JSON.stringify({ assets: mode === "skip" ? files.map((name, i) => ({ id: i + 1, name })) : [] }));
|
||||
writeFileSync(join(tools, "gh"), '#!/bin/sh\nprintf "%s\\n" "$*" > "$MIRROR_FIXTURE/gh.log"\ncp "$MIRROR_FIXTURE"/published/* dist/bin/\n', { mode: 0o755 });
|
||||
writeFileSync(join(tools, "curl"), `#!/bin/bash
|
||||
url=""
|
||||
for arg in "$@"; do case "$arg" in https://*) url="$arg" ;; esac; done
|
||||
case "$url" in
|
||||
*/assets?*)
|
||||
name="\${url##*name=}"
|
||||
printf 'start\\n' >> "$MIRROR_FIXTURE/events"
|
||||
if [ "$MIRROR_MODE" = "skip" ]; then printf 'unexpected upload\\n' >> "$MIRROR_FIXTURE/uploads"; exit 22; fi
|
||||
count_file="$MIRROR_FIXTURE/count-$name"
|
||||
count=0; [ ! -f "$count_file" ] || count=$(cat "$count_file")
|
||||
count=$((count + 1)); printf '%s' "$count" > "$count_file"
|
||||
if [ "$MIRROR_MODE" = "retry" ] && [ "$count" -eq 1 ]; then printf 'end\\n' >> "$MIRROR_FIXTURE/events"; exit 28; fi
|
||||
if [ "$MIRROR_MODE" = "concurrency" ]; then sleep 0.1; fi
|
||||
printf 'end\\n' >> "$MIRROR_FIXTURE/events"
|
||||
printf '{"id":100}\\nHTTP_STATUS:201'
|
||||
;;
|
||||
*/download/*) cat "$MIRROR_FIXTURE/published/\${url##*/}" ;;
|
||||
*/releases) printf '{"id":42}' ;;
|
||||
*/releases/42) cat "$MIRROR_FIXTURE/info.json" ;;
|
||||
*) exit 22 ;;
|
||||
esac
|
||||
`, { mode: 0o755 });
|
||||
const workflow = parse(readFileSync(join(import.meta.dir, "../.github/workflows/release.yml"), "utf8"));
|
||||
const step = workflow.jobs["build-and-publish"].steps.find((s: { name?: string }) => s.name === "Publish Gitea Release");
|
||||
return { ...f, files, run: () => spawnSync("bash", ["-eo", "pipefail", "-c", step.run], { cwd: f.root, env: { ...process.env, PATH: `${tools}:${process.env.PATH}`, GITEA_TOKEN: "fixture", GITEA_RETRY_DELAY: "0", GITEA_HOST: "https://example.test", GITEA_REPO: "test/repo", RELEASE_TAG: "v1.0.0", MIRROR_ONLY: String(mirrorOnly), GITHUB_REPOSITORY: "test/repo", MIRROR_FIXTURE: f.root, MIRROR_MODE: mode }, encoding: "utf8", timeout: 5000 }) };
|
||||
}
|
||||
|
||||
describe("release preparation and publication guards", () => {
|
||||
test("bump rejects wrong branches, dirty trees and invalid/non-increasing versions before writes", () => {
|
||||
const f = repository();
|
||||
@@ -93,6 +136,10 @@ describe("release preparation and publication guards", () => {
|
||||
expect(runs.some((s: string) => s.includes("sha256sum hose-use-* > checksums.txt"))).toBe(true);
|
||||
for (const step of [...ci.jobs.validate.steps, ...steps]) if (step.uses) expect(step.uses).toMatch(/@[a-f0-9]{40}$/);
|
||||
expect(JSON.parse(readFileSync(join(root, ".mcp.json"), "utf8")).mcpServers["hose-use"].command).toBe("bun");
|
||||
expect(release.on.workflow_dispatch.inputs.mirror_only.type).toBe("boolean");
|
||||
for (const name of ["Compile multi-target binaries", "Package release archives", "Publish GitHub Release"]) {
|
||||
expect(steps.find((s: { name?: string }) => s.name === name).if).toBe("${{ !inputs.mirror_only }}");
|
||||
}
|
||||
});
|
||||
|
||||
test("a failed background Gitea upload makes the Release step fail", () => {
|
||||
@@ -112,9 +159,42 @@ for arg in "$@"; do
|
||||
done
|
||||
exit 22
|
||||
`, { mode: 0o755 });
|
||||
const result = spawnSync("bash", ["-eo", "pipefail", "-c", step.run], { cwd: f.root, env: { ...process.env, PATH: `${join(f.root, "tools")}:${process.env.PATH}`, GITEA_TOKEN: "fixture", GITEA_HOST: "https://example.test", GITEA_REPO: "test/repo", RELEASE_TAG: "v1.0.0" }, encoding: "utf8", timeout: 5000 });
|
||||
const result = spawnSync("bash", ["-eo", "pipefail", "-c", step.run], { cwd: f.root, env: { ...process.env, PATH: `${join(f.root, "tools")}:${process.env.PATH}`, GITEA_TOKEN: "fixture", GITEA_RETRY_DELAY: "0", GITEA_HOST: "https://example.test", GITEA_REPO: "test/repo", RELEASE_TAG: "v1.0.0" }, encoding: "utf8", timeout: 5000 });
|
||||
expect(result.status).toBe(1);
|
||||
expect(result.stdout).toContain("assets failed to upload");
|
||||
expect(result.stdout).not.toContain("Successfully published");
|
||||
});
|
||||
|
||||
test("transient asset upload failures are retried", () => {
|
||||
const f = mirrorFixture("retry");
|
||||
const result = f.run();
|
||||
expect(result.status).toBe(0);
|
||||
for (const name of f.files) expect(readFileSync(join(f.root, `count-${name}`), "utf8")).toBe("2");
|
||||
expect(result.stdout).toContain("attempt 2/3");
|
||||
});
|
||||
|
||||
test("correct mirror assets are verified and retained without uploading replacements", () => {
|
||||
const f = mirrorFixture("skip");
|
||||
const result = f.run();
|
||||
expect(result.status).toBe(0);
|
||||
expect(result.stdout.match(/already matches SHA-256/g)).toHaveLength(f.files.length);
|
||||
});
|
||||
|
||||
test("mirror-only recovery downloads and verifies the existing GitHub assets", () => {
|
||||
const f = mirrorFixture("skip", true);
|
||||
expect(f.run().status).toBe(0);
|
||||
expect(readFileSync(join(f.root, "gh.log"), "utf8")).toContain("release download v1.0.0");
|
||||
});
|
||||
|
||||
test("asset transfers never exceed two concurrent workers", () => {
|
||||
const f = mirrorFixture("concurrency");
|
||||
expect(f.run().status).toBe(0);
|
||||
let active = 0, peak = 0;
|
||||
for (const event of readFileSync(join(f.root, "events"), "utf8").trim().split("\n")) {
|
||||
active += event === "start" ? 1 : -1;
|
||||
peak = Math.max(peak, active);
|
||||
}
|
||||
expect(active).toBe(0);
|
||||
expect(peak).toBeLessThanOrEqual(2);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user