From aacd2a7794ebc1985899d6e372ed404018d6e74b Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Mon, 13 Apr 2026 00:05:10 +0200 Subject: [PATCH] Optimize Docker build caching for CI: add BuildKit cache mounts for apt, bundler, and yarn; use registry-based cache in CI pipeline --- .forgejo/workflows/build_container.yaml | 6 +++--- Dockerfile | 25 +++++++++++++++---------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/.forgejo/workflows/build_container.yaml b/.forgejo/workflows/build_container.yaml index 8f89ba8..30716e9 100644 --- a/.forgejo/workflows/build_container.yaml +++ b/.forgejo/workflows/build_container.yaml @@ -51,7 +51,7 @@ jobs: password: ${{ secrets.FORGEJO_TOKEN }} # ------------------------------------------------------------------------- - # Build and push image with GitHub Actions cache + # Build and push image with registry-based layer caching # ------------------------------------------------------------------------- - name: Build and Push uses: docker/build-push-action@v6 @@ -59,5 +59,5 @@ jobs: context: . push: true tags: git.workaround.org/${{ github.repository }}:latest - cache-from: type=gha - cache-to: type=gha,mode=max + cache-from: type=registry,ref=git.workaround.org/${{ github.repository }}:buildcache + cache-to: type=registry,ref=git.workaround.org/${{ github.repository }}:buildcache,mode=max diff --git a/Dockerfile b/Dockerfile index 54dfeb5..31311ee 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,10 +16,11 @@ FROM docker.io/library/ruby:${RUBY_VERSION}-slim AS base WORKDIR /rails # Install runtime-only system packages -RUN apt-get update -qq && \ +RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ + --mount=type=cache,target=/var/lib/apt,sharing=locked \ + apt-get update -qq && \ apt-get install --no-install-recommends -y \ - curl libjemalloc2 libvips libpq-dev telnet imagemagick && \ - rm -rf /var/lib/apt/lists /var/cache/apt/archives + curl libjemalloc2 libvips libpq-dev telnet imagemagick # "Heist" Node.js and npm from the node_source stage COPY --from=node_source /usr/local/bin/node /usr/local/bin/node @@ -33,26 +34,30 @@ RUN ln -s /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm && \ ENV RAILS_ENV="production" \ BUNDLE_DEPLOYMENT="1" \ BUNDLE_PATH="/usr/local/bundle" \ - BUNDLE_WITHOUT="development" + BUNDLE_WITHOUT="development" \ + BUNDLE_CACHE=true # --- Stage 3: Build (Compilation & Assets) --- FROM base AS build # Install packages needed to build gems and node modules -RUN apt-get update -qq && \ +RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ + --mount=type=cache,target=/var/lib/apt,sharing=locked \ + apt-get update -qq && \ apt-get install --no-install-recommends -y \ - build-essential git libyaml-dev node-gyp pkg-config python-is-python3 && \ - rm -rf /var/lib/apt/lists /var/cache/apt/archives + build-essential git libyaml-dev node-gyp pkg-config python-is-python3 # 1. Install Gems (Separate COPY for maximum caching) COPY Gemfile Gemfile.lock ./ -RUN bundle install && \ - rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \ +RUN --mount=type=cache,target=/usr/local/bundle/cache \ + bundle config set --local path "${BUNDLE_PATH}" && \ + bundle install && \ bundle exec bootsnap precompile --gemfile # 2. Install JavaScript dependencies (Separate COPY for maximum caching) COPY package.json yarn.lock ./ -RUN yarn install --immutable +RUN --mount=type=cache,target=/tmp/yarn-cache \ + yarn install --immutable # 3. Copy application code COPY . .