From 9cf1c79f2a31c91b1c4a5833e67fd0fbfc7bbbed Mon Sep 17 00:00:00 2001 From: tigerenwork Date: Fri, 7 Aug 2026 22:47:35 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=20buildx=20?= =?UTF-8?q?=E8=B7=A8=E5=B9=B3=E5=8F=B0=E6=9E=84=E5=BB=BA=E5=B9=B6=E5=8F=91?= =?UTF-8?q?=E5=B8=83=E9=95=9C=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DEPLOY.md | 14 ++++++++++++-- Dockerfile | 4 +++- publish.sh | 33 ++++++++++++++++++++++++++------- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/DEPLOY.md b/DEPLOY.md index 75e36dc..cff3297 100644 --- a/DEPLOY.md +++ b/DEPLOY.md @@ -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 diff --git a/Dockerfile b/Dockerfile index e83f68d..47b8c07 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 ./ diff --git a/publish.sh b/publish.sh index 1198533..a9dca97 100755 --- a/publish.sh +++ b/publish.sh @@ -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= and pushes it. +# cross-builds the image for the target platform (buildx) with +# --build-arg VERSION= 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 /:.' 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"