Files
Mateusz Gruszczyński a28362b82c
Go quality gates / test (push) Waiting to run
v0.1.2
2026-12-01 00:16:15 +01:00

138 lines
4.4 KiB
Bash
Executable File

#!/bin/sh
# Build standalone EasyDNS UI .deb packages using Docker (no Go or dpkg on host).
set -eu
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
IMAGE=${IMAGE:-golang:1.23-bookworm}
BUILD_ID=${BUILD_ID:-}
VERSION=${VERSION:-0.1.2-1}
ARCH=${ARCH:-${GOARCH:-all}}
usage() {
cat <<'HELP'
Usage: ./scripts/build_deb.sh [--arch all|amd64|arm64] [--version VERSION] [--image IMAGE]
Build Linux Debian packages inside a Docker container. Only Docker is required
on the host; Go, dpkg-deb, and cross-compilation tools run in the container.
Options:
--arch Architectures to build (default: all = amd64 + arm64)
--version Debian package version (default: 0.1.2-1)
--image Debian-based Go image (default: golang:1.23-bookworm)
--build-id Optional unique build stamp for hashed static filenames
-h, --help Show this message
Environment alternatives: ARCH, GOARCH, VERSION, IMAGE.
Packages are written to ./dist/easydns-ui_VERSION_ARCH.deb.
Examples:
./scripts/build_deb.sh
./scripts/build_deb.sh --arch amd64 --version 0.1.2-1
IMAGE=golang:1.23-bookworm ./scripts/build_deb.sh --arch arm64
HELP
}
while [ "$#" -gt 0 ]; do
case "$1" in
--arch|--version|--image|--build-id)
option=$1
shift
if [ "$#" -eq 0 ]; then
printf 'Missing value for %s\n' "$option" >&2
usage >&2
exit 2
fi
case "$option" in
--arch) ARCH=$1 ;;
--version) VERSION=$1 ;;
--image) IMAGE=$1 ;;
--build-id) BUILD_ID=$1 ;;
esac
;;
-h|--help) usage; exit 0 ;;
*) printf 'Unknown argument: %s\n' "$1" >&2; usage >&2; exit 2 ;;
esac
shift
done
case "$ARCH" in
all) ARCHES='amd64 arm64' ;;
amd64|arm64) ARCHES=$ARCH ;;
*) printf 'Unsupported architecture: %s (use all, amd64 or arm64)\n' "$ARCH" >&2; exit 2 ;;
esac
# Debian requires a digit at the start of the version. Reject shell metacharacters,
# path separators, whitespace and newlines; the native builder validates it too.
case "$VERSION" in
''|[!0-9]*|*[!A-Za-z0-9.+~:-]*)
printf 'Invalid Debian package version: %s\n' "$VERSION" >&2
exit 2
;;
esac
case "$BUILD_ID" in
''|*[!A-Za-z0-9._-]*)
if [ -n "$BUILD_ID" ]; then echo "Invalid BUILD_ID" >&2; exit 2; fi ;;
esac
case "$IMAGE" in
''|*[!A-Za-z0-9._:/@-]*)
printf 'Invalid Docker image reference: %s\n' "$IMAGE" >&2
exit 2
;;
esac
if ! command -v docker >/dev/null 2>&1; then
printf 'Docker is not installed or not in PATH. Install Docker Engine/Desktop first.\n' >&2
exit 1
fi
if ! docker info >/dev/null 2>&1; then
printf 'Cannot connect to Docker daemon (is Docker running and your user authorized?).\n' >&2
exit 1
fi
# Pre-create as host user: output files will not be owned by container root.
mkdir -p "$ROOT/dist"
uid=$(id -u)
gid=$(id -g)
printf 'Building EasyDNS UI %s for %s using Docker image %s\n' "$VERSION" "$ARCHES" "$IMAGE"
# Native Debian packaging script is the single source of truth for package
# contents, scripts, systemd unit, config, and control metadata. Running one
# normal Docker platform with CGO disabled builds both Linux architectures
# without QEMU, multiarch buildx or a compiler installed on the host.
docker run --rm \
--network none \
--user "$uid:$gid" \
--volume "$ROOT:/workspace:rw" \
--workdir /workspace \
--env "VERSION=$VERSION" \
--env "BUILD_ID=$BUILD_ID" \
--env "ARCHES=$ARCHES" \
--env GOOS=linux \
--env CGO_ENABLED=0 \
--env GOPROXY=off \
--env GOTOOLCHAIN=local \
--env GOFLAGS=-buildvcs=false \
--env HOME=/tmp \
--env GOCACHE=/tmp/easydns-go-cache \
--env GOPATH=/tmp/easydns-go-path \
"$IMAGE" sh -ec '
command -v go >/dev/null 2>&1 || { echo "Docker image does not contain Go" >&2; exit 1; }
command -v dpkg-deb >/dev/null 2>&1 || { echo "Docker image does not contain dpkg-deb (use a Debian-based Go image)" >&2; exit 1; }
for arch in $ARCHES; do
echo "==> Building linux/$arch"
GOARCH="$arch" VERSION="$VERSION" ./scripts/build-deb.sh
file="dist/easydns-ui_${VERSION}_${arch}.deb"
test -s "$file"
test "$(dpkg-deb -f "$file" Architecture)" = "$arch"
test "$(dpkg-deb -f "$file" Version)" = "$VERSION"
echo "==> Valid package: $file"
done
'
printf '\nCompleted. Packages:\n'
for arch in $ARCHES; do
output="$ROOT/dist/easydns-ui_${VERSION}_${arch}.deb"
[ -s "$output" ] || { printf 'Missing package: %s\n' "$output" >&2; exit 1; }
printf ' %s\n' "$output"
done