fix: bake 文件写入项目根目录以修复 buildx context 缓存问题

bake JSON 原先写入系统临时目录,buildx 默认以 bake 文件所在目录作为
构建 context,导致 COPY . . 复制空目录,go build 命中旧的 mount cache
产出旧二进制,源码变更后镜像内容不变。改为写入项目根目录的 .bake.json。
This commit is contained in:
zetaloop
2026-04-24 08:16:42 +08:00
parent 6cc14479c5
commit 1a2df120af
2 changed files with 8 additions and 7 deletions
+4 -1
View File
@@ -125,4 +125,7 @@ DockerFile
# Go compiled binaries # Go compiled binaries
app/*/api/api app/*/api/api
app/*/rpc/rpc app/*/rpc/rpc
app/*/mq/mq app/*/mq/mq
# Build artifacts
.bake.json
+4 -6
View File
@@ -5,7 +5,6 @@ import json
import os import os
import subprocess import subprocess
import sys import sys
import tempfile
from glob import glob from glob import glob
from pathlib import Path from pathlib import Path
@@ -100,21 +99,20 @@ def main():
"target": targets, "target": targets,
} }
fd, bakefile = tempfile.mkstemp(prefix="juwan-bake-", suffix=".json") bakefile = ROOT_DIR / ".bake.json"
try: try:
with os.fdopen(fd, "w") as f: bakefile.write_text(json.dumps(bake, indent=2))
json.dump(bake, f, indent=2)
print(f"Generated {len(targets)} targets -> {bakefile}") print(f"Generated {len(targets)} targets -> {bakefile}")
names = list(targets.keys()) names = list(targets.keys())
for i in range(0, len(names), BAKE_BATCH_SIZE): for i in range(0, len(names), BAKE_BATCH_SIZE):
batch = names[i : i + BAKE_BATCH_SIZE] batch = names[i : i + BAKE_BATCH_SIZE]
subprocess.run( subprocess.run(
["docker", "buildx", "bake", "--load", "-f", bakefile, *batch], ["docker", "buildx", "bake", "--load", "-f", str(bakefile), *batch],
check=True, check=True,
) )
finally: finally:
os.unlink(bakefile) bakefile.unlink(missing_ok=True)
main() main()