refactor: build.sh 改用 buildx bake 并行构建

This commit is contained in:
zetaloop
2026-04-01 06:11:28 +08:00
parent 93e6b5609c
commit 3eb44d8a73
2 changed files with 51 additions and 24 deletions
+48 -21
View File
@@ -2,33 +2,31 @@
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "$0")/../.." && pwd)"
IMAGE_PREFIX="juwan"
IMAGE_PREFIX="${IMAGE_PREFIX:-juwan}"
IMAGE_TAG="${1:-dev}"
cd "$ROOT_DIR"
find app -mindepth 2 -maxdepth 2 -type d \( -name "api" -o -name "rpc" -o -name "mq" \) | sort | while read -r service_dir; do
service_type=$(basename "$service_dir")
service_name=$(basename "$(dirname "$service_dir")")
bakefile=$(mktemp "${TMPDIR:-/tmp}/juwan-bake-XXXXXX")
mv "$bakefile" "${bakefile}.json"
bakefile="${bakefile}.json"
entry_file=$(grep -rl "package main" "$service_dir"/*.go 2>/dev/null | head -n 1 || true)
[[ -z "$entry_file" ]] && continue
python3 - "$IMAGE_PREFIX" "$IMAGE_TAG" "$bakefile" <<'PYEOF'
import json, os, subprocess, sys, glob
config_file=$(ls "$service_dir/etc/"*.yaml 2>/dev/null | head -n 1 || true)
config_name="${config_file:+$(basename "$config_file")}"
config_name="${config_name:-config.yaml}"
prefix, tag, outpath = sys.argv[1], sys.argv[2], sys.argv[3]
image_name="${IMAGE_PREFIX}/${service_name}-${service_type}:${IMAGE_TAG}"
echo "--- $image_name ---"
cat > Dockerfile.tmp <<EOF
dockerfile_tpl = """\
# syntax=docker/dockerfile:1.7
FROM golang:1.25-alpine AS builder
RUN apk add --no-cache tzdata
WORKDIR /build
COPY go.mod go.sum ./
RUN go mod download
RUN --mount=type=cache,target=/go/pkg/mod go mod download
COPY . .
RUN go build -ldflags="-s -w" -o /app/main ./$service_dir
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go build -ldflags="-s -w" -o /app/main ./{service_dir}
FROM alpine:latest
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
@@ -36,10 +34,39 @@ COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /usr/share/zoneinfo/Asia/S
ENV TZ=Asia/Shanghai
WORKDIR /app
COPY --from=builder /app/main /app/main
COPY $service_dir/etc /app/etc
CMD ["./main", "-f", "etc/$config_name"]
EOF
COPY {service_dir}/etc /app/etc
CMD ["./main", "-f", "etc/{config_name}"]
"""
docker build -f Dockerfile.tmp -t "$image_name" . && echo "OK: $image_name" || echo "FAIL: $image_name"
rm -f Dockerfile.tmp
done
targets = {}
for service_dir in sorted(glob.glob("app/*/*")):
service_type = os.path.basename(service_dir)
if service_type not in ("api", "rpc", "mq"):
continue
go_files = glob.glob(os.path.join(service_dir, "*.go"))
has_main = any("package main" in open(f).read() for f in go_files) if go_files else False
if not has_main:
continue
yamls = glob.glob(os.path.join(service_dir, "etc", "*.yaml"))
config_name = os.path.basename(yamls[0]) if yamls else "config.yaml"
service_name = os.path.basename(os.path.dirname(service_dir))
target_name = f"{service_name}-{service_type}"
targets[target_name] = {
"dockerfile-inline": dockerfile_tpl.format(service_dir=service_dir, config_name=config_name),
"tags": [f"{prefix}/{target_name}:{tag}"],
}
bake = {
"group": {"default": {"targets": list(targets.keys())}},
"target": targets,
}
with open(outpath, "w") as f:
json.dump(bake, f, indent=2)
print(f"Generated {len(targets)} targets -> {outpath}")
PYEOF
docker buildx bake --load -f "$bakefile"
rm -f "$bakefile"