import { spawnSync, execFileSync } from "node:child_process"; import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { pathToFileURL } from "node:url"; export function publicationPlan(env) { if (!["push", "workflow_dispatch"].includes(env.CI_EVENT)) return null; const repository = (env.REPOSITORY || "").toLowerCase(); if (!/^[a-z0-9][a-z0-9._-]*\/[a-z0-9][a-z0-9._-]*$/.test(repository)) throw new Error("Invalid REPOSITORY"); const sha = env.COMMIT_SHA || ""; if (!/^[a-f0-9]{40}$/.test(sha)) throw new Error("COMMIT_SHA must be a full Git commit ID"); const image = "git.elijahkuntz.com/" + repository; const tags = [sha]; const ref = env.TARGET_REF || ""; const latest = ref === "refs/heads/" + (env.MAIN_BRANCH || "main"); const version = ref.startsWith("refs/tags/") ? ref.slice("refs/tags/".length) : ""; if (/^v[\w][\w.-]{0,126}$/.test(version)) tags.push(version.toLowerCase()); return { registry: "git.elijahkuntz.com", image, tags, latest, sha }; } export function publishImage(env = process.env, dryRun = false) { const plan = publicationPlan(env); if (!plan) { console.log("This event verifies the image without publishing."); return; } if (dryRun) { console.log( JSON.stringify( { ...plan, latestRequiresCurrentBranchHead: plan.latest }, null, 2, ), ); return; } if (!env.FORGEJO_PAT) throw new Error( "Add FORGEJO_PAT with write:package permission under Calio Settings → Actions → Secrets, then rerun this workflow.", ); if (!env.REGISTRY_USERNAME) throw new Error("REGISTRY_USERNAME must identify the token owner"); const configDir = mkdtempSync(join(tmpdir(), "calio-registry-")); const childEnv = { ...process.env, ...env, DOCKER_CONFIG: configDir }; const sourceImage = env.SOURCE_IMAGE || "calio:ci"; const docker = (args, input) => { const result = spawnSync("docker", args, { env: childEnv, input, stdio: [input === undefined ? "ignore" : "pipe", "inherit", "inherit"], }); if (result.error) throw result.error; if (result.status !== 0) throw new Error(`docker ${args[0]} failed`); }; try { docker( [ "login", plan.registry, "--username", env.REGISTRY_USERNAME, "--password-stdin", ], env.FORGEJO_PAT, ); for (const tag of plan.tags) { const target = `${plan.image}:${tag}`; docker(["tag", sourceImage, target]); docker(["push", target]); } if (plan.latest) { const head = execFileSync( "git", ["ls-remote", "origin", `refs/heads/${env.MAIN_BRANCH || "main"}`], { encoding: "utf8" }, ) .trim() .split(/\s+/)[0]; if (head !== plan.sha) { console.log( "Main has a newer commit; published the commit tag and left latest unchanged.", ); return; } docker(["tag", sourceImage, `${plan.image}:latest`]); docker(["push", `${plan.image}:latest`]); } } finally { rmSync(configDir, { recursive: true, force: true }); } } if ( process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href ) { try { publishImage(process.env, process.argv.includes("--dry-run")); } catch (error) { console.error(error.message); process.exitCode = 1; } }