refactor: 使用 buildx 跨平台构建并发布镜像

This commit is contained in:
tigerenwork 2026-08-07 22:47:35 +08:00
parent b1cfc10517
commit 9cf1c79f2a
3 changed files with 41 additions and 10 deletions

View File

@ -15,11 +15,21 @@ Use `publish.sh` — it reads the current version from this file, bumps the mino
./publish.sh 2.0 # publish an explicit version instead
```
## Cross-building
The dev machine is an Apple Silicon Mac (arm64) while the target server is
Ubuntu 22.04 x86_64, so images are cross-built with `docker buildx` for
`linux/amd64` and pushed straight to the registry (`--push`). Requires a
buildx builder with amd64 support (OrbStack / Docker Desktop provide one by
default, e.g. via QEMU emulation).
## Build & push (manual)
```sh
docker build --build-arg VERSION=1.10 -t 192.168.2.212:3000/tigeren/metube:1.10 .
docker push 192.168.2.212:3000/tigeren/metube:1.10
docker buildx build --platform linux/amd64 \
--build-arg VERSION=1.10 \
-t 192.168.2.212:3000/tigeren/metube:1.10 \
--push .
```
## Run

View File

@ -1,4 +1,6 @@
FROM node:lts-alpine AS builder
# Pin the UI builder to the build host platform: its output is just static
# files, so there is no need to run node under QEMU when cross-building.
FROM --platform=$BUILDPLATFORM node:lts-alpine AS builder
WORKDIR /metube
COPY ui ./

View File

@ -5,7 +5,8 @@
# - The version recorded in DEPLOY.md (checked into git) is the current
# online version.
# - Publishing bumps the minor number of that version, updates DEPLOY.md,
# builds the image with --build-arg VERSION=<new> and pushes it.
# cross-builds the image for the target platform (buildx) with
# --build-arg VERSION=<new> and pushes it to the registry.
#
# Usage:
# ./publish.sh # bump minor of the current version (e.g. 1.10 -> 1.11)
@ -16,8 +17,13 @@ cd "$(dirname "$0")"
DEPLOY_FILE="DEPLOY.md"
# Current image reference (with tag), taken from the build command in DEPLOY.md
CURRENT_REF=$(grep -oP '(?<=-t )\S+:[0-9]+\.[0-9]+' "$DEPLOY_FILE" | head -1)
# Deployment target platform (Ubuntu 22.04 x86_64). The local dev machine is
# Apple Silicon (arm64), so builds go through `docker buildx`.
PLATFORM="linux/amd64"
# Current image reference (with tag), taken from the build command in DEPLOY.md.
# Written with POSIX sed so it works with macOS's BSD userland (no GNU grep -P).
CURRENT_REF=$(sed -n 's/.*-t \([^ ]*:[0-9][0-9]*\.[0-9][0-9]*\).*/\1/p' "$DEPLOY_FILE" | head -1)
if [[ -z "$CURRENT_REF" ]]; then
echo "Error: could not find an image reference like '-t <registry>/<image>:<major>.<minor>' in $DEPLOY_FILE" >&2
exit 1
@ -46,11 +52,24 @@ fi
echo "Publishing $IMAGE:$NEW_VERSION (current: $CURRENT_VERSION)"
# Record the new version as the current online version
sed -i "s/VERSION=${CURRENT_VERSION}\b/VERSION=${NEW_VERSION}/g; s/:${CURRENT_VERSION}\b/:${NEW_VERSION}/g" "$DEPLOY_FILE"
# Record the new version as the current online version. BSD sed has no `\b`,
# so require a non-digit (or end of line) after the version to avoid matching
# e.g. 1.1 inside 1.10.
sed -i '' \
-e "s/VERSION=${CURRENT_VERSION}\([^0-9.]\)/VERSION=${NEW_VERSION}\1/g" \
-e "s/VERSION=${CURRENT_VERSION}\$/VERSION=${NEW_VERSION}/g" \
-e "s/:${CURRENT_VERSION}\([^0-9.]\)/:${NEW_VERSION}\1/g" \
-e "s/:${CURRENT_VERSION}\$/:${NEW_VERSION}/g" \
"$DEPLOY_FILE"
docker build --build-arg "VERSION=$NEW_VERSION" -t "$IMAGE:$NEW_VERSION" .
docker push "$IMAGE:$NEW_VERSION"
# Cross-build from the local machine (e.g. Apple Silicon) for the deployment
# target platform and push directly to the registry in one buildx invocation.
docker buildx build \
--platform "$PLATFORM" \
--build-arg "VERSION=$NEW_VERSION" \
-t "$IMAGE:$NEW_VERSION" \
--push \
.
echo
echo "Published $IMAGE:$NEW_VERSION"