Optimize Docker build caching for CI: add BuildKit cache mounts for apt, bundler, and yarn; use registry-based cache in CI pipeline
All checks were successful
Build and Push Docker Image / build-and-push-image (push) Successful in 1m22s

This commit is contained in:
Christoph Haas 2026-04-13 00:05:10 +02:00
parent 86c1d8ae5a
commit aacd2a7794
2 changed files with 18 additions and 13 deletions

View file

@ -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

View file

@ -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 . .