|
| 1 | +import path from "node:path"; |
| 2 | + |
| 3 | +import { $ } from "execa"; |
| 4 | + |
| 5 | +import { projectRoot } from "@/scripts/project.js"; |
| 6 | +import { $$ } from "@/scripts/shell.js"; |
| 7 | +import { getVersionTag } from "@/scripts/tasks/build.js"; |
| 8 | +import { deploy } from "@/scripts/tasks/deploy.js"; |
| 9 | + |
| 10 | +const platforms: string[] = ["linux/amd64", "linux/arm64"]; |
| 11 | + |
| 12 | +const packagePath = path.resolve(projectRoot, ".local/docker"); |
| 13 | + |
| 14 | +async function buildImage(registry: string, imageName: string, tags: string[], publish: boolean) { |
| 15 | + console.log("Building multi-arch Docker image..."); |
| 16 | + |
| 17 | + const dockerfilePath = path.join(projectRoot, "docker/Dockerfile"); |
| 18 | + const dockerPlatforms = platforms.join(","); |
| 19 | + |
| 20 | + const args = ["buildx", "build", "--file", dockerfilePath, "--platform", dockerPlatforms]; |
| 21 | + |
| 22 | + for (const tag of tags) { |
| 23 | + args.push("--tag", `${registry}/${imageName}:${tag}`); |
| 24 | + } |
| 25 | + |
| 26 | + if (publish) { |
| 27 | + args.push("--push"); |
| 28 | + } |
| 29 | + |
| 30 | + args.push(projectRoot); |
| 31 | + |
| 32 | + await $$`docker ${args}`; |
| 33 | + console.log("Docker image built successfully."); |
| 34 | +} |
| 35 | + |
| 36 | +const registry = process.env.REGISTRY ?? "local"; |
| 37 | +const imageName = process.env.IMAGE_NAME ?? "setup"; |
| 38 | +const version = await getVersionTag(); |
| 39 | +const tags = [version, "latest"]; |
| 40 | +const token = process.env.REGISTRY_TOKEN; |
| 41 | +const publish = !!token; |
| 42 | + |
| 43 | +if (publish) { |
| 44 | + await $({ verbose: "full", input: token })`docker login ${registry} --username USERNAME --password-stdin`; |
| 45 | +} |
| 46 | + |
| 47 | +await deploy(packagePath); |
| 48 | +await buildImage(registry, imageName, tags, publish); |
0 commit comments