diff --git a/helpers/repro-run.ts b/helpers/repro-run.ts index 797a732..9f60f38 100644 --- a/helpers/repro-run.ts +++ b/helpers/repro-run.ts @@ -1,3 +1,5 @@ +import { chmod, mkdir, readFile, writeFile } from "node:fs/promises"; +import { join } from "node:path"; import { Writable } from "node:stream"; import { execFile } from "./better-api.js"; @@ -21,3 +23,29 @@ export async function reproRun(opts: { run.child.stderr?.pipe(process.stderr); await run; } + +export async function buildRepro( + cacheName: string, + cacheKey: string, + buildScript: string, + run: (cwd: string) => Promise, + getArtifacts: (cwd: string) => T +): Promise { + const dir = join("cache", cacheName); + await mkdir(dir, { recursive: true }); + const cacheKeyFile = join(dir, "version"); + const output = getArtifacts(dir); + try { + if ((await readFile(cacheKeyFile, "utf-8")) === cacheKey) return output; + } catch {} + + { + const buildScriptPath = join(dir, "build"); + await writeFile(buildScriptPath, buildScript); + await chmod(buildScriptPath, 0o700); + } + await run(dir); + await writeFile(cacheKeyFile, cacheKey); + + return output; +} diff --git a/services/forgejo/build.ts b/services/forgejo/build.ts index 7857a44..d8a1644 100644 --- a/services/forgejo/build.ts +++ b/services/forgejo/build.ts @@ -1,25 +1,15 @@ import { chmod, mkdir, readFile, writeFile } from "node:fs/promises"; import { join } from "node:path"; -import { reproRun } from "../../helpers/repro-run.js"; +import { buildRepro, reproRun } from "../../helpers/repro-run.js"; const FORGEJO_VERSION = "v1.18.3-0"; // returns path to statically compiled binary export async function buildForgejo(): Promise { - const dir = "cache/forgejo"; - await mkdir(dir, { recursive: true }); - const versionFile = join(dir, "version"); - const output = join(dir, "rootfs/forgejo"); - try { - if ((await readFile(versionFile, "utf-8")) === FORGEJO_VERSION) - return output; - } catch {} - - { - const buildScript = join(dir, "build"); - await writeFile( - buildScript, - `#!/bin/sh -e + return await buildRepro( + "forgejo", + FORGEJO_VERSION, + `#!/bin/sh -e runprint() { echo "==> $@" "$@" @@ -33,20 +23,17 @@ cd forgejo runprint env GOOS=linux GOARCH=amd64 LDFLAGS="-linkmode external -extldflags '-static' $LDFLAGS" TAGS="bindata sqlite sqlite_unlock_notify" make build mv gitea /forgejo -` - ); - await chmod(buildScript, 0o700); - } - await reproRun({ - cwd: dir, - command: "/src/build", - cache: [ - "/home/repro/.cache/go-build", - "/home/repro/go", - "/home/repro/.npm", - ], - }); - await writeFile(versionFile, FORGEJO_VERSION); - - return output; +`, + (dir) => + reproRun({ + cwd: dir, + command: "/src/build", + cache: [ + "/home/repro/.cache/go-build", + "/home/repro/go", + "/home/repro/.npm", + ], + }), + (dir) => join(dir, "rootfs/forgejo") + ); } diff --git a/services/loki/build.ts b/services/loki/build.ts index 17a3f2b..0e12925 100644 --- a/services/loki/build.ts +++ b/services/loki/build.ts @@ -1,24 +1,14 @@ -import { chmod, mkdir, readFile, writeFile } from "node:fs/promises"; import { join } from "node:path"; -import { reproRun } from "../../helpers/repro-run.js"; +import { buildRepro, reproRun } from "../../helpers/repro-run.js"; const LOKI_VERSION = "v2.7.3"; // returns path to statically compiled binary -export async function buildLoki(): Promise { - const dir = "cache/loki"; - await mkdir(dir, { recursive: true }); - const versionFile = join(dir, "version"); - const output = join(dir, "rootfs/loki"); - try { - if ((await readFile(versionFile, "utf-8")) === LOKI_VERSION) return output; - } catch {} - - { - const buildScript = join(dir, "build"); - await writeFile( - buildScript, - `#!/bin/sh -e +export function buildLoki(): Promise { + return buildRepro( + "loki", + LOKI_VERSION, + `#!/bin/sh -e runprint() { echo "==> $@" "$@" @@ -33,20 +23,17 @@ cd loki runprint make -j1 GOMOD=readonly logcli loki mv cmd/loki/loki /loki -` - ); - await chmod(buildScript, 0o700); - } - await reproRun({ - cwd: dir, - command: "/src/build", - cache: [ - "/home/repro/.cache/go-build", - "/home/repro/go", - "/home/repro/.npm", - ], - }); - await writeFile(versionFile, LOKI_VERSION); - - return output; +`, + (dir) => + reproRun({ + cwd: dir, + command: "/src/build", + cache: [ + "/home/repro/.cache/go-build", + "/home/repro/go", + "/home/repro/.npm", + ], + }), + (dir) => join(dir, "rootfs/loki") + ); }