build.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env bun
  2. import { $ } from "bun"
  3. import path from "path"
  4. import { fileURLToPath } from "url"
  5. const rootDir = fileURLToPath(new URL("../../..", import.meta.url))
  6. process.chdir(rootDir)
  7. const reg = process.env.REGISTRY ?? "ghcr.io/anomalyco"
  8. const tag = process.env.TAG ?? "24.04"
  9. const push = process.argv.includes("--push") || process.env.PUSH === "1"
  10. const root = path.join(rootDir, "package.json")
  11. const pkg = await Bun.file(root).json()
  12. const manager = pkg.packageManager ?? ""
  13. const bun = manager.startsWith("bun@") ? manager.slice(4) : ""
  14. if (!bun) throw new Error("packageManager must be bun@<version>")
  15. const images = ["base", "bun-node", "rust", "tauri-linux", "publish"]
  16. const setup = async () => {
  17. if (!push) return
  18. const list = await $`docker buildx ls`.text()
  19. if (list.includes("opencode")) {
  20. await $`docker buildx use opencode`
  21. return
  22. }
  23. await $`docker buildx create --name opencode --use`
  24. }
  25. await setup()
  26. const platform = "linux/amd64,linux/arm64"
  27. for (const name of images) {
  28. const image = `${reg}/build/${name}:${tag}`
  29. const file = `packages/containers/${name}/Dockerfile`
  30. if (name === "base") {
  31. if (push) {
  32. console.log(`docker buildx build --platform ${platform} -f ${file} -t ${image} --push .`)
  33. await $`docker buildx build --platform ${platform} -f ${file} -t ${image} --push .`
  34. }
  35. if (!push) {
  36. console.log(`docker build -f ${file} -t ${image} .`)
  37. await $`docker build -f ${file} -t ${image} .`
  38. }
  39. }
  40. if (name === "bun-node") {
  41. if (push) {
  42. console.log(
  43. `docker buildx build --platform ${platform} -f ${file} -t ${image} --build-arg REGISTRY=${reg} --build-arg BUN_VERSION=${bun} --push .`,
  44. )
  45. await $`docker buildx build --platform ${platform} -f ${file} -t ${image} --build-arg REGISTRY=${reg} --build-arg BUN_VERSION=${bun} --push .`
  46. }
  47. if (!push) {
  48. console.log(`docker build -f ${file} -t ${image} --build-arg REGISTRY=${reg} --build-arg BUN_VERSION=${bun} .`)
  49. await $`docker build -f ${file} -t ${image} --build-arg REGISTRY=${reg} --build-arg BUN_VERSION=${bun} .`
  50. }
  51. }
  52. if (name !== "base" && name !== "bun-node") {
  53. if (push) {
  54. console.log(
  55. `docker buildx build --platform ${platform} -f ${file} -t ${image} --build-arg REGISTRY=${reg} --push .`,
  56. )
  57. await $`docker buildx build --platform ${platform} -f ${file} -t ${image} --build-arg REGISTRY=${reg} --push .`
  58. }
  59. if (!push) {
  60. console.log(`docker build -f ${file} -t ${image} --build-arg REGISTRY=${reg} .`)
  61. await $`docker build -f ${file} -t ${image} --build-arg REGISTRY=${reg} .`
  62. }
  63. }
  64. if (push) {
  65. console.log(`pushed ${image}`)
  66. }
  67. }