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 { 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<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,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<string> {
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")
);
}

View file

@ -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<string> {
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<string> {
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")
);
}