reutilizar código de buildear cacheado reprorun

This commit is contained in:
Cat /dev/Nulo 2023-02-16 21:37:00 -03:00
parent cb813b19b1
commit bcd0280e69
3 changed files with 65 additions and 63 deletions

View file

@ -1,3 +1,5 @@
import { chmod, mkdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { Writable } from "node:stream"; import { Writable } from "node:stream";
import { execFile } from "./better-api.js"; import { execFile } from "./better-api.js";
@ -21,3 +23,29 @@ export async function reproRun(opts: {
run.child.stderr?.pipe(process.stderr); run.child.stderr?.pipe(process.stderr);
await run; await run;
} }
export async function buildRepro<T>(
cacheName: string,
cacheKey: string,
buildScript: string,
run: (cwd: string) => Promise<void>,
getArtifacts: (cwd: string) => T
): Promise<T> {
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;
}

View file

@ -1,24 +1,14 @@
import { chmod, mkdir, readFile, writeFile } from "node:fs/promises"; import { chmod, mkdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path"; 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"; const FORGEJO_VERSION = "v1.18.3-0";
// returns path to statically compiled binary // returns path to statically compiled binary
export async function buildForgejo(): Promise<string> { export async function buildForgejo(): Promise<string> {
const dir = "cache/forgejo"; return await buildRepro(
await mkdir(dir, { recursive: true }); "forgejo",
const versionFile = join(dir, "version"); FORGEJO_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 `#!/bin/sh -e
runprint() { runprint() {
echo "==> $@" echo "==> $@"
@ -33,11 +23,9 @@ cd forgejo
runprint env GOOS=linux GOARCH=amd64 LDFLAGS="-linkmode external -extldflags '-static' $LDFLAGS" TAGS="bindata sqlite sqlite_unlock_notify" make build runprint env GOOS=linux GOARCH=amd64 LDFLAGS="-linkmode external -extldflags '-static' $LDFLAGS" TAGS="bindata sqlite sqlite_unlock_notify" make build
mv gitea /forgejo mv gitea /forgejo
` `,
); (dir) =>
await chmod(buildScript, 0o700); reproRun({
}
await reproRun({
cwd: dir, cwd: dir,
command: "/src/build", command: "/src/build",
cache: [ cache: [
@ -45,8 +33,7 @@ mv gitea /forgejo
"/home/repro/go", "/home/repro/go",
"/home/repro/.npm", "/home/repro/.npm",
], ],
}); }),
await writeFile(versionFile, FORGEJO_VERSION); (dir) => join(dir, "rootfs/forgejo")
);
return output;
} }

View file

@ -1,23 +1,13 @@
import { chmod, mkdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path"; 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"; const LOKI_VERSION = "v2.7.3";
// returns path to statically compiled binary // returns path to statically compiled binary
export async function buildLoki(): Promise<string> { export function buildLoki(): Promise<string> {
const dir = "cache/loki"; return buildRepro(
await mkdir(dir, { recursive: true }); "loki",
const versionFile = join(dir, "version"); LOKI_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 `#!/bin/sh -e
runprint() { runprint() {
echo "==> $@" echo "==> $@"
@ -33,11 +23,9 @@ cd loki
runprint make -j1 GOMOD=readonly logcli loki runprint make -j1 GOMOD=readonly logcli loki
mv cmd/loki/loki /loki mv cmd/loki/loki /loki
` `,
); (dir) =>
await chmod(buildScript, 0o700); reproRun({
}
await reproRun({
cwd: dir, cwd: dir,
command: "/src/build", command: "/src/build",
cache: [ cache: [
@ -45,8 +33,7 @@ mv cmd/loki/loki /loki
"/home/repro/go", "/home/repro/go",
"/home/repro/.npm", "/home/repro/.npm",
], ],
}); }),
await writeFile(versionFile, LOKI_VERSION); (dir) => join(dir, "rootfs/loki")
);
return output;
} }