Dockerfile.build 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # syntax=docker/dockerfile:1.4
  2. # Smartbotic Package Builder
  3. # Debian 13 (trixie) based build container for production binaries
  4. #
  5. # Usage (standard build - installs dependencies each time):
  6. # docker buildx build -f packaging/Dockerfile.build --target packages --output "type=local,dest=dist/" .
  7. #
  8. # Usage (fast build with pre-built base image):
  9. # # First build the base image once:
  10. # docker buildx build -f packaging/Dockerfile.base -t smartbotic-build-base:debian13 .
  11. # # Then use it for fast builds:
  12. # docker buildx build -f packaging/Dockerfile.build \
  13. # --build-arg BASE_IMAGE=smartbotic-build-base:debian13 \
  14. # --build-arg BUILD_VERSION=x.y.z \
  15. # --build-arg BUILD_GIT_COMMIT=$(git rev-parse HEAD) \
  16. # --build-arg BUILD_GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD) \
  17. # --target packages --output "type=local,dest=dist/" .
  18. #
  19. # Build caches are persistent across builds via BuildKit cache mounts:
  20. # - /ccache: C++ compiler cache (ccache)
  21. # - /npm-cache: npm package cache
  22. ARG BASE_IMAGE=debian:trixie
  23. FROM ${BASE_IMAGE} AS builder
  24. ARG BUILD_VERSION=0.0.0
  25. ARG BUILD_TARGET=debian13
  26. ARG BUILD_JOBS=16
  27. ARG BUILD_GIT_COMMIT=unknown
  28. ARG BUILD_GIT_BRANCH=unknown
  29. ENV DEBIAN_FRONTEND=noninteractive
  30. ENV BUILD_VERSION=${BUILD_VERSION}
  31. ENV BUILD_TARGET=${BUILD_TARGET}
  32. ENV BUILD_JOBS=${BUILD_JOBS}
  33. ENV BUILD_GIT_COMMIT=${BUILD_GIT_COMMIT}
  34. ENV BUILD_GIT_BRANCH=${BUILD_GIT_BRANCH}
  35. # ccache configuration
  36. ENV CCACHE_DIR=/ccache
  37. ENV CCACHE_MAXSIZE=5G
  38. ENV CCACHE_COMPRESS=1
  39. ENV CCACHE_COMPRESSLEVEL=6
  40. ENV PATH="/usr/lib/ccache:${PATH}"
  41. # Install build dependencies (skipped if using pre-built base image)
  42. RUN if [ ! -f /etc/smartbotic-build-base ]; then \
  43. apt-get update && apt-get install -y --no-install-recommends \
  44. build-essential cmake ninja-build git pkg-config ca-certificates ccache \
  45. libcurl4-openssl-dev libssl-dev \
  46. nlohmann-json3-dev libspdlog-dev libsystemd-dev \
  47. protobuf-compiler libprotobuf-dev libgrpc++-dev protobuf-compiler-grpc \
  48. liblz4-dev nodejs npm \
  49. && rm -rf /var/lib/apt/lists/*; \
  50. else echo "Using pre-built base image: $(cat /etc/smartbotic-build-base)"; fi
  51. WORKDIR /build
  52. # Copy package files first for better layer caching
  53. COPY webui/package.json webui/package-lock.json ./webui/
  54. # Install npm dependencies with cache mount
  55. RUN --mount=type=cache,target=/npm-cache,id=npm-cache \
  56. cd webui && npm ci --cache /npm-cache
  57. # Copy WebUI source and build
  58. COPY webui/ ./webui/
  59. RUN cd webui && npm run build
  60. # Copy C++ source code (excluding webui which is already copied)
  61. COPY CMakeLists.txt ./
  62. COPY cmake/ ./cmake/
  63. COPY lib/ ./lib/
  64. COPY src/ ./src/
  65. COPY config/ ./config/
  66. COPY packaging/ ./packaging/
  67. COPY external/ ./external/
  68. # Build all targets with ccache and Release optimizations
  69. RUN --mount=type=cache,target=/ccache,id=ccache \
  70. cmake -B build -G Ninja \
  71. -DCMAKE_BUILD_TYPE=Release \
  72. -DCMAKE_C_COMPILER_LAUNCHER=ccache \
  73. -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
  74. -DBUILD_TESTS=OFF \
  75. -DBUNDLE_DATABASE=ON \
  76. -DGIT_COMMIT_OVERRIDE=${BUILD_GIT_COMMIT} \
  77. -DGIT_BRANCH_OVERRIDE=${BUILD_GIT_BRANCH} \
  78. && cmake --build build --parallel ${BUILD_JOBS} \
  79. && ccache --show-stats || true
  80. # Package creation stage
  81. FROM builder AS packager
  82. # Create package output directory
  83. RUN mkdir -p /packages
  84. # Run package creation script
  85. RUN chmod +x /build/packaging/scripts/create-packages.sh && \
  86. /build/packaging/scripts/create-packages.sh
  87. # Output stage - just the packages
  88. FROM scratch AS packages
  89. COPY --from=packager /packages/*.tar.gz /