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
All checks were successful
Build and Push Docker Image / build-and-push-image (push) Successful in 1m22s
This commit is contained in:
parent
86c1d8ae5a
commit
aacd2a7794
2 changed files with 18 additions and 13 deletions
|
|
@ -51,7 +51,7 @@ jobs:
|
||||||
password: ${{ secrets.FORGEJO_TOKEN }}
|
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
|
- name: Build and Push
|
||||||
uses: docker/build-push-action@v6
|
uses: docker/build-push-action@v6
|
||||||
|
|
@ -59,5 +59,5 @@ jobs:
|
||||||
context: .
|
context: .
|
||||||
push: true
|
push: true
|
||||||
tags: git.workaround.org/${{ github.repository }}:latest
|
tags: git.workaround.org/${{ github.repository }}:latest
|
||||||
cache-from: type=gha
|
cache-from: type=registry,ref=git.workaround.org/${{ github.repository }}:buildcache
|
||||||
cache-to: type=gha,mode=max
|
cache-to: type=registry,ref=git.workaround.org/${{ github.repository }}:buildcache,mode=max
|
||||||
|
|
|
||||||
25
Dockerfile
25
Dockerfile
|
|
@ -16,10 +16,11 @@ FROM docker.io/library/ruby:${RUBY_VERSION}-slim AS base
|
||||||
WORKDIR /rails
|
WORKDIR /rails
|
||||||
|
|
||||||
# Install runtime-only system packages
|
# 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 \
|
apt-get install --no-install-recommends -y \
|
||||||
curl libjemalloc2 libvips libpq-dev telnet imagemagick && \
|
curl libjemalloc2 libvips libpq-dev telnet imagemagick
|
||||||
rm -rf /var/lib/apt/lists /var/cache/apt/archives
|
|
||||||
|
|
||||||
# "Heist" Node.js and npm from the node_source stage
|
# "Heist" Node.js and npm from the node_source stage
|
||||||
COPY --from=node_source /usr/local/bin/node /usr/local/bin/node
|
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" \
|
ENV RAILS_ENV="production" \
|
||||||
BUNDLE_DEPLOYMENT="1" \
|
BUNDLE_DEPLOYMENT="1" \
|
||||||
BUNDLE_PATH="/usr/local/bundle" \
|
BUNDLE_PATH="/usr/local/bundle" \
|
||||||
BUNDLE_WITHOUT="development"
|
BUNDLE_WITHOUT="development" \
|
||||||
|
BUNDLE_CACHE=true
|
||||||
|
|
||||||
# --- Stage 3: Build (Compilation & Assets) ---
|
# --- Stage 3: Build (Compilation & Assets) ---
|
||||||
FROM base AS build
|
FROM base AS build
|
||||||
|
|
||||||
# Install packages needed to build gems and node modules
|
# 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 \
|
apt-get install --no-install-recommends -y \
|
||||||
build-essential git libyaml-dev node-gyp pkg-config python-is-python3 && \
|
build-essential git libyaml-dev node-gyp pkg-config python-is-python3
|
||||||
rm -rf /var/lib/apt/lists /var/cache/apt/archives
|
|
||||||
|
|
||||||
# 1. Install Gems (Separate COPY for maximum caching)
|
# 1. Install Gems (Separate COPY for maximum caching)
|
||||||
COPY Gemfile Gemfile.lock ./
|
COPY Gemfile Gemfile.lock ./
|
||||||
RUN bundle install && \
|
RUN --mount=type=cache,target=/usr/local/bundle/cache \
|
||||||
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
|
bundle config set --local path "${BUNDLE_PATH}" && \
|
||||||
|
bundle install && \
|
||||||
bundle exec bootsnap precompile --gemfile
|
bundle exec bootsnap precompile --gemfile
|
||||||
|
|
||||||
# 2. Install JavaScript dependencies (Separate COPY for maximum caching)
|
# 2. Install JavaScript dependencies (Separate COPY for maximum caching)
|
||||||
COPY package.json yarn.lock ./
|
COPY package.json yarn.lock ./
|
||||||
RUN yarn install --immutable
|
RUN --mount=type=cache,target=/tmp/yarn-cache \
|
||||||
|
yarn install --immutable
|
||||||
|
|
||||||
# 3. Copy application code
|
# 3. Copy application code
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue