65 lines
2.1 KiB
JavaScript
65 lines
2.1 KiB
JavaScript
import { createHash } from "node:crypto";
|
|
import { createReadStream } from "node:fs";
|
|
import { mkdir, rm, stat, writeFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { spawnSync } from "node:child_process";
|
|
|
|
const root = path.resolve(import.meta.dirname, "../..");
|
|
const output = path.resolve(root, process.env.OUTPUT_DIR || "dist/docker-images");
|
|
const tag = process.env.IMAGE_TAG || "latest";
|
|
const platform = process.env.DOCKER_PLATFORM || "linux/amd64";
|
|
const images = [
|
|
{ name: "personal-brand-api", dockerfile: "apps/api/Dockerfile", context: "." },
|
|
{ name: "personal-brand-web", dockerfile: "apps/web/Dockerfile", context: "." },
|
|
];
|
|
|
|
function run(command, args, environment = {}) {
|
|
const result = spawnSync(command, args, {
|
|
cwd: root,
|
|
stdio: "inherit",
|
|
env: { ...process.env, ...environment },
|
|
});
|
|
if (result.status !== 0) process.exit(result.status ?? 1);
|
|
}
|
|
|
|
run("docker", [
|
|
"compose",
|
|
"-f",
|
|
"docker-compose.prod.yml",
|
|
"--env-file",
|
|
path.join(root, ".env.production.example"),
|
|
"config",
|
|
"--quiet",
|
|
]);
|
|
|
|
await rm(output, { recursive: true, force: true });
|
|
await mkdir(output, { recursive: true });
|
|
|
|
for (const image of images) {
|
|
run("docker", [
|
|
"build",
|
|
"--platform",
|
|
platform,
|
|
"--file",
|
|
image.dockerfile,
|
|
"--tag",
|
|
`${image.name}:${tag}`,
|
|
image.context,
|
|
]);
|
|
if (platform !== "linux/amd64") {
|
|
run("docker", ["pull", `--platform=${platform}`, `${image.name}:${tag}`]);
|
|
}
|
|
const archive = path.join(output, `${image.name}-${tag}.tar`);
|
|
run("docker", ["save", "--output", archive, `${image.name}:${tag}`]);
|
|
const fileStat = await stat(archive);
|
|
const digest = createHash("sha256");
|
|
await new Promise((resolve, reject) => {
|
|
const stream = createReadStream(archive);
|
|
stream.on("data", (chunk) => digest.update(chunk));
|
|
stream.on("end", resolve);
|
|
stream.on("error", reject);
|
|
});
|
|
await writeFile(`${archive}.sha256`, `${digest.digest("hex")} ${path.basename(archive)}\n`);
|
|
console.log(`${path.relative(root, archive)}: ${(fileStat.size / 1024 / 1024).toFixed(1)} MiB`);
|
|
}
|