Files
tuliprox/bin/build_docker.sh
T
Goldy 1299d50d1b Refactor Docker build process to support multi-platform images and streamline binary selection
- Introduced a new stage in the Dockerfile to select binaries based on target architecture.
- Updated the build script to copy architecture-specific binaries for multi-platform support.
2025-08-17 17:17:49 +02:00

169 lines
4.8 KiB
Bash

#!/bin/bash
set -euo pipefail
# Check for required environment variable
if [ ! -f "${HOME}/.ghcr.io" ]; then
echo "🧨 Error: ${HOME}/.ghcr.io file not found"
exit 1
fi
source "${HOME}/.ghcr.io"
# Function to print usage instructions
print_usage() {
echo "Usage: $(basename "$0") <branch>"
echo
echo "Arguments:"
echo " branch Git branch name (only 'master' and 'develop' are supported)"
echo
echo "Examples:"
echo " $(basename "$0") master # Builds and pushes with :latest tag"
echo " $(basename "$0") develop # Builds and pushes with :dev tag"
exit 1
}
# Validate arguments
if [ $# -ne 1 ]; then
echo "🧨 Error: Exactly one argument required"
print_usage
fi
BRANCH="$1"
# Validate branch
case "$BRANCH" in
master)
TAG_SUFFIX="latest"
;;
develop)
TAG_SUFFIX="dev"
;;
*)
echo "🧨 Error: Branch '$BRANCH' is not supported. Only 'master' and 'develop' are allowed."
exit 1
;;
esac
echo "🚀 Building Docker images for branch: $BRANCH (tag: $TAG_SUFFIX)"
# Set up directories
WORKING_DIR=$(pwd)
BIN_DIR="${WORKING_DIR}/bin"
RESOURCES_DIR="${WORKING_DIR}/resources"
DOCKER_DIR="${WORKING_DIR}/docker"
BACKEND_DIR="${WORKING_DIR}/backend"
FRONTEND_DIR="${WORKING_DIR}/frontend"
FRONTEND_BUILD_DIR="${FRONTEND_DIR}/dist"
# Define architectures and their corresponding builds
declare -A ARCHITECTURES=(
[LINUX]=x86_64-unknown-linux-musl
[AARCH64]=aarch64-unknown-linux-musl
)
# Images to build with multi-platform support
declare -A MULTI_PLATFORM_IMAGES=(
[tuliprox]="scratch-final"
[tuliprox-alpine]="alpine-final"
)
# Get version from Cargo.toml
VERSION=$(grep -Po '^version\s*=\s*"\K[0-9\.]+' "${BACKEND_DIR}/Cargo.toml")
if [ -z "${VERSION}" ]; then
echo "🧨 Error: Failed to determine the version from Cargo.toml"
exit 1
fi
echo "📦 Version: ${VERSION}"
# Build resources if needed
if [ ! -f "${BIN_DIR}/build_resources.sh" ]; then
echo "🛠️ Building resources..."
"${BIN_DIR}/build_resources.sh"
fi
# Build frontend
echo "🎨 Building frontend..."
rm -rf "${FRONTEND_BUILD_DIR}"
cd "${FRONTEND_DIR}" && env RUSTFLAGS="--remap-path-prefix $HOME=~" trunk build --release
# Check if the frontend build directory exists
if [ ! -d "${FRONTEND_BUILD_DIR}" ]; then
echo "🧨 Error: Frontend build directory '${FRONTEND_BUILD_DIR}' does not exist"
exit 1
fi
cd "$WORKING_DIR"
# Build binaries for all architectures first
echo "🏗️ Building binaries for all architectures..."
for PLATFORM in "${!ARCHITECTURES[@]}"; do
ARCHITECTURE=${ARCHITECTURES[$PLATFORM]}
echo "🔨 Building binary for architecture: $ARCHITECTURE"
cargo clean || true
env RUSTFLAGS="--remap-path-prefix $HOME=~" cross build -p tuliprox --release --target "$ARCHITECTURE"
BINARY_PATH="${WORKING_DIR}/target/${ARCHITECTURE}/release/tuliprox"
if [ ! -f "$BINARY_PATH" ]; then
echo "🧨 Error: Binary $BINARY_PATH does not exist"
exit 1
fi
# Copy binary with architecture suffix for multi-platform build
mkdir -p "${DOCKER_DIR}/binaries"
cp "$BINARY_PATH" "${DOCKER_DIR}/binaries/tuliprox-${ARCHITECTURE}"
done
# Prepare common Docker context
echo "📋 Preparing Docker context..."
rm -rf "${DOCKER_DIR}/web"
rm -rf "${DOCKER_DIR}/resources"
cp -r "${FRONTEND_BUILD_DIR}" "${DOCKER_DIR}/web"
cp -r "${RESOURCES_DIR}" "${DOCKER_DIR}/resources"
cd "${DOCKER_DIR}"
# Login to GitHub Container Registry (needed before buildx push)
echo "🔑 Logging into GitHub Container Registry..."
docker login ghcr.io -u euzu -p "${GHCR_IO_TOKEN}"
declare -a BUILT_IMAGES=()
# Build multi-platform images
for IMAGE_NAME in "${!MULTI_PLATFORM_IMAGES[@]}"; do
BUILD_TARGET="${MULTI_PLATFORM_IMAGES[$IMAGE_NAME]}"
echo "🎯 Building multi-platform image: ${IMAGE_NAME} with target ${BUILD_TARGET}"
# Build and push multi-platform image directly
docker buildx build -f Dockerfile-manual \
-t "ghcr.io/euzu/${IMAGE_NAME}:${VERSION}" \
-t "ghcr.io/euzu/${IMAGE_NAME}:${TAG_SUFFIX}" \
--target "$BUILD_TARGET" \
--platform "linux/amd64,linux/arm64" \
--push \
.
BUILT_IMAGES+=("ghcr.io/euzu/${IMAGE_NAME}:${VERSION}")
BUILT_IMAGES+=("ghcr.io/euzu/${IMAGE_NAME}:${TAG_SUFFIX}")
done
# Clean up Docker context
echo "🗑️ Cleaning up binaries..."
rm -rf "${DOCKER_DIR}/binaries"
cd "$WORKING_DIR"
# Final cleanup
echo "🗑️ Cleaning up Docker context..."
rm -rf "${DOCKER_DIR}/web"
rm -f "${DOCKER_DIR}/tuliprox"
rm -rf "${DOCKER_DIR}/resources"
echo "🎉 Docker images for branch '$BRANCH' (version ${VERSION}) have been successfully built and pushed!"
echo "📋 Built images:"
for img in "${BUILT_IMAGES[@]}"; do
echo " - $img"
done