# Multi-stage Alpine Zero-OS Builder with Smart Caching # Stage 1: Base build environment (cached until Alpine version changes) FROM alpine:3.22 AS base-builder # Install build dependencies (expensive, cache this layer) RUN apk add --no-cache \ # Build tools build-base \ linux-headers \ cmake \ git \ wget \ curl \ cpio \ xz \ gzip \ bc \ perl \ python3 \ # Kernel build dependencies linux-lts-dev \ linux-lts \ elfutils-dev \ openssl-dev \ flex \ bison \ # Archive tools tar \ bzip2 \ unzip \ # Text processing sed \ grep \ findutils \ jq \ diffutils \ # Go for source compilation go \ # Rustup for proper Rust musl builds rustup # Setup Rust toolchain for musl builds RUN rustup-init -y --default-toolchain stable && \ . ~/.cargo/env && \ rustup target add x86_64-unknown-linux-musl # Create standard directories RUN mkdir -p /build/initramfs /build/kernel /build/output /build/github /build/configs/zinit /mnt/zinit /build/source /build/cache WORKDIR /build # Stage 2: Zero-OS components (cached until source repos change) FROM base-builder AS components-builder # Copy component compilation script COPY scripts/compile-components.sh /build/scripts/ RUN chmod +x /build/scripts/compile-components.sh # Create a marker file to track component compilation RUN echo "$(date)" > /build/components-build-time # This stage will be cached unless we explicitly rebuild components # Stage 3: Kernel builder (cached until kernel version/config changes) FROM components-builder AS kernel-builder # Copy kernel configuration and version COPY configs/kernel-version /build/configs/ COPY configs/kernel-config-generic /build/configs/ COPY scripts/build-kernel.sh /build/scripts/ RUN chmod +x /build/scripts/build-kernel.sh # Create kernel build marker RUN echo "Kernel: $(cat /build/configs/kernel-version)" > /build/kernel-build-marker # This stage caches kernel source download and basic setup # Stage 4: Final builder (lightweight layer for initramfs assembly) FROM kernel-builder AS final-builder # Copy all scripts and configs COPY scripts/ /build/scripts/ COPY configs/ /build/configs/ # Make all scripts executable RUN chmod +x /build/scripts/*.sh # Set build arguments ARG TARGETARCH=amd64 ARG BUILDMODE=debug ARG MINIMAL_MODE=false # Set environment variables ENV BUILDMODE=${BUILDMODE} ENV TARGETARCH=${TARGETARCH} ENV MINIMAL_MODE=${MINIMAL_MODE} # Create build configuration RUN echo "BUILDMODE=${BUILDMODE}" > /build/build.conf && \ echo "TARGETARCH=${TARGETARCH}" >> /build/build.conf && \ echo "MINIMAL_MODE=${MINIMAL_MODE}" >> /build/build.conf && \ echo "INITRAMFS_ROOT=/build/initramfs" >> /build/build.conf && \ echo "KERNEL_DIR=/build/kernel" >> /build/build.conf && \ echo "OUTPUT_DIR=/build/output" >> /build/build.conf && \ echo "CONFIG_DIR=/build/configs" >> /build/build.conf # Default command CMD ["/build/scripts/build-initramfs.sh"]