From 735cbdb93502e0b24dfbaa8edb628bfebecab2bc Mon Sep 17 00:00:00 2001 From: Nick Sieger Date: Thu, 19 Nov 2026 11:01:35 -0600 Subject: [PATCH] feat(offload): add darwin and windows static archives - static distro compiles from source with make ce-release under xx-go --wrap, as deb and rpm do - archives the unsigned binary for every platform, no prebuilt release binaries - tgz for darwin and linux, zip for windows, .sha256 next to each - layout cli-plugins/ and doc/{LICENSE,README.md}, as merged in #571 - static stage gets the Go toolchain, build-env secret, COMMIT, BUILD and DATE - verify-offload-static checks checksum, layout, platform and version - build all distros + static - add go-build stage on BUILDPLATFORM, golang has no darwin or windows image - builder-static takes /usr/local/go from go-build instead of the target-platform go stage - drop offload from PKG_DISTROS so it builds all DISTROS, including static - restore the docker-bake.hcl copyright year Signed-off-by: Nick Sieger --- docker-bake.hcl | 6 +- pkg/offload/Dockerfile | 26 ++++++ pkg/offload/scripts/pkg-static-build.sh | 102 ++++++++++++++++++++++++ pkg/offload/verify.Dockerfile | 56 +++++++++++++ 4 files changed, 186 insertions(+), 4 deletions(-) create mode 100755 pkg/offload/scripts/pkg-static-build.sh diff --git a/docker-bake.hcl b/docker-bake.hcl index 363cf211..38e66420 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -91,8 +91,8 @@ variable "PKG_PLATFORMS_BASE" { model = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm64", "linux/arm/v7", "windows/amd64", "windows/arm64"] # https://github.com/docker/docker-agent/blob/5b9feaabe743a5ad577f2247ed55d5dcb2678e8b/Taskfile.yml#L79 agent = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm64", "windows/amd64", "windows/arm64"] - # Linux packages only; darwin and windows ship through the source repo. - offload = ["linux/amd64", "linux/arm64"] + # darwin and windows are static archives only. The linux platforms also build a static archive. + offload = ["darwin/amd64", "darwin/arm64", "linux/amd64", "linux/arm64", "windows/amd64", "windows/arm64"] } } @@ -105,8 +105,6 @@ variable "PKG_DISTROS" { description = "Package-specific distro mapping. Packages omitted here build for all DISTROS." default = { docker = ["static"] - # Every distro except static: the offload Dockerfile has no builder-static stage. - offload = [for distro in DISTROS : distro if distro != "static"] } } diff --git a/pkg/offload/Dockerfile b/pkg/offload/Dockerfile index 9c1938af..7fed2605 100644 --- a/pkg/offload/Dockerfile +++ b/pkg/offload/Dockerfile @@ -44,6 +44,9 @@ FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx # go base image to retrieve /usr/local/go FROM ${GO_IMAGE}:${GO_VERSION}-${GO_IMAGE_VARIANT} AS go +# go toolchain for the build platform, used by static to cross compile for any target platform +FROM --platform=$BUILDPLATFORM ${GO_IMAGE}:${GO_VERSION}-${GO_IMAGE_VARIANT} AS go-build + # deb FROM ${DISTRO_IMAGE} AS builder-deb COPY --from=xx / / @@ -108,6 +111,29 @@ RUN --mount=type=bind,source=scripts/pkg-rpm-build.sh,target=/usr/local/bin/pkg- --mount=type=secret,id=build-env,target=/run/secrets/build-env \ OUTDIR=/out SRCDIR=/usr/local/src/offload BUILD_ENV_FILE=/run/secrets/build-env pkg-rpm-build +# static +# Compiles from source, like the deb and rpm builders, and archives the unsigned binary. +FROM --platform=$BUILDPLATFORM ${DISTRO_IMAGE} AS builder-static +COPY --from=xx / / +RUN apt-get update && apt-get install -y --no-install-recommends bash file make tar zip +ENV GOPATH="/go" +ENV PATH="$PATH:/usr/local/go/bin:$GOPATH/bin" +ENV GOTOOLCHAIN="local" +ARG PKG_NAME +ARG VERSION +ARG COMMIT +ARG BUILD +ARG DATE +WORKDIR /build +ARG TARGETPLATFORM +RUN --mount=type=bind,source=scripts/pkg-static-build.sh,target=/usr/local/bin/pkg-static-build \ + --mount=type=bind,from=scripts,source=write-sha256sum.sh,target=/usr/local/bin/write-sha256sum \ + --mount=type=bind,from=src,target=/usr/local/src/offload,rw \ + --mount=type=bind,from=go-build,source=/usr/local/go,target=/usr/local/go,rw \ + --mount=type=secret,id=build-env,target=/run/secrets/build-env \ + OUTDIR=/out BUILDDIR=/build SRCDIR=/usr/local/src/offload \ + BUILD_ENV_FILE=/run/secrets/build-env pkg-static-build + FROM builder-${DISTRO_TYPE} AS build-pkg ARG BUILDKIT_SBOM_SCAN_STAGE=true diff --git a/pkg/offload/scripts/pkg-static-build.sh b/pkg/offload/scripts/pkg-static-build.sh new file mode 100755 index 00000000..a6abd095 --- /dev/null +++ b/pkg/offload/scripts/pkg-static-build.sh @@ -0,0 +1,102 @@ +#!/usr/bin/env bash + +# Copyright 2026 Docker Packaging authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +: "${VERSION=}" +: "${COMMIT=}" +: "${BUILD=}" +: "${DATE=}" +: "${BUILD_ENV_FILE=}" + +: "${PKG_NAME=}" + +: "${BUILDDIR=/work/build}" +: "${SRCDIR=/usr/local/src/offload}" +: "${OUTDIR=/out}" + +set -e + +if [ -z "$VERSION" ]; then + echo >&2 "error: VERSION is required" + exit 1 +fi +case "$VERSION" in + v*) ;; + *) echo >&2 "error: VERSION must start with v, got $VERSION"; exit 1 ;; +esac +if [ -z "$PKG_NAME" ]; then + echo >&2 "error: PKG_NAME is required" + exit 1 +fi +if [ -z "$OUTDIR" ]; then + echo >&2 "error: OUTDIR is required" + exit 1 +fi + +if ! command -v xx-info &> /dev/null; then + echo >&2 "error: xx cross compilation helper is required" + exit 1 +fi + +mkdir -p "$OUTDIR" "$BUILDDIR" + +binext=$([ "$(xx-info os)" = "windows" ] && echo ".exe" || true) +bin="${BUILDDIR}/docker-offload${binext}" + +# Build from source, as the deb and rpm builds do. The binaries are unsigned. +( + set -x + xx-go --wrap + make -C "$SRCDIR" ce-release VERSION="$VERSION" COMMIT="$COMMIT" BUILD="$BUILD" DATE="$DATE" \ + BUILD_ENV_FILE="$BUILD_ENV_FILE" + install -m 0755 "${SRCDIR}/dist/docker-offload${binext}" "$bin" + xx-verify --static "$bin" +) + +for doc in LICENSE README.md; do + if [ ! -f "${SRCDIR}/${doc}" ]; then + echo >&2 "error: no ${doc} in ${SRCDIR}; pass the source tree as the src named context" + exit 1 + fi +done + +pkgoutput="$OUTDIR/static/$(xx-info os)/$(xx-info arch)" +if [ -n "$(xx-info variant)" ]; then + pkgoutput="${pkgoutput}/$(xx-info variant)" +fi +mkdir -p "${pkgoutput}" + +workdir=$(mktemp -d -t docker-packaging.XXXXXXXXXX) +mkdir -p "$workdir/cli-plugins" "$workdir/doc" +( + set -x + cp "$bin" "$workdir/cli-plugins/" + cp "${SRCDIR}/LICENSE" "${SRCDIR}/README.md" "$workdir/doc/" +) +if [ "$(xx-info os)" = "windows" ]; then + pkgfile="${pkgoutput}/${PKG_NAME}_${VERSION#v}.zip" + ( + set -x + cd "$workdir" + zip -r "$pkgfile" cli-plugins doc + ) +else + pkgfile="${pkgoutput}/${PKG_NAME}_${VERSION#v}.tgz" + ( + set -x + tar -czf "$pkgfile" -C "$workdir" cli-plugins doc + ) +fi +write-sha256sum "$pkgfile" diff --git a/pkg/offload/verify.Dockerfile b/pkg/offload/verify.Dockerfile index ac2dda47..67b91bd2 100644 --- a/pkg/offload/verify.Dockerfile +++ b/pkg/offload/verify.Dockerfile @@ -104,4 +104,60 @@ RUN --mount=from=bin,target=/build <&2 "error: no static archives found in /build" + exit 1 + fi + for package in $packages; do + ( + set -x + dir="${package%/*}" + name="${package##*/}" + platform="${dir#*/static/}" + os="${platform%%/*}" + arch="${platform#*/}" + arch="${arch%%/*}" + ext="" + [ "$os" = windows ] && ext=".exe" + cd "$dir" + sha256sum -c "${name}.sha256" + tmp=$(mktemp -d) + case "$name" in + *.tgz) tar -xzf "$name" -C "$tmp" ;; + *.zip) unzip -q "$name" -d "$tmp" ;; + esac + test "$(ls "$tmp" | tr '\n' ' ')" = "cli-plugins doc " + test "$(ls "$tmp/cli-plugins")" = "docker-offload${ext}" + test -x "$tmp/cli-plugins/docker-offload${ext}" + test -s "$tmp/doc/LICENSE" + test -s "$tmp/doc/README.md" + TARGETPLATFORM="${os}/${arch}" xx-verify --static "$tmp/cli-plugins/docker-offload${ext}" + if [ "$os/$arch" = "$(xx-info os)/$(xx-info arch)" ]; then + "$tmp/cli-plugins/docker-offload${ext}" docker-cli-plugin-metadata + if [ -n "$VERSION" ]; then + "$tmp/cli-plugins/docker-offload${ext}" docker-cli-plugin-metadata \ + | grep -F "\"Version\": \"$VERSION\"" + fi + elif [ -n "$VERSION" ]; then + # shortcut: substring match, so v0.7.1 also matches a v0.7.10 binary. + # The binary cannot run here; upgrade if a way to read its version appears. + grep -aqF "$VERSION" "$tmp/cli-plugins/docker-offload${ext}" + fi + ) + done +EOT + FROM verify-${DISTRO_TYPE} AS verify