Skip to content

Commit

Permalink
Caching, take two
Browse files Browse the repository at this point in the history
  • Loading branch information
wengj9 committed Sep 24, 2024
1 parent b0acf10 commit f6fa815
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
36 changes: 21 additions & 15 deletions .github/workflows/build-push-ghcr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,50 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

# Setup QEMU: https://github.com/docker/setup-qemu-action
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
uses: docker/setup-qemu-action@v3
with:
platforms: 'arm64'

# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v2
uses: docker/setup-buildx-action@v6
with:
install: true

- name: Cache Docker layers
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
path: |
/tmp/.buildx-cache
pnpm-store
# Key is named differently to avoid collision
key: ${{ runner.os }}-multi-buildx-${{ github.sha }}
key: ${{ runner.os }}-multi-buildx-${{ hashFiles('pnpm-lock.yaml') }}-${{ hashFiles('Dockerfile') }}
restore-keys: |
${{ runner.os }}-multi-buildx
- name: inject cache into docker
uses: reproducible-containers/[email protected]
with:
cache-map: |
{
"pnpm-store": "/pnpm/store"
}
skip-extraction: ${{ steps.cache.outputs.cache-hit }}

- name: Log in to the Container registry
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
Expand All @@ -68,7 +79,7 @@ jobs:
type=sha
- name: Build and push Docker image
uses: docker/build-push-action@v3
uses: docker/build-push-action@v6
with:
context: .
push: true
Expand All @@ -80,9 +91,4 @@ jobs:
# More: https://github.com/moby/buildkit#--export-cache-options
# And: https://github.com/docker/buildx#--cache-tonametypetypekeyvalue
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new

# prevents running out of space
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache

17 changes: 8 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
FROM node:lts-slim AS base

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable

FROM base AS prod

COPY pnpm-lock.yaml /app
COPY . /app
WORKDIR /app
RUN pnpm fetch --prod

COPY . /app
FROM base AS prod-deps
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile

FROM base AS build
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
RUN pnpm run build

FROM base
COPY --from=prod /app/node_modules /app/node_modules
COPY --from=prod /app/dist /app/dist
COPY --from=prod-deps /app/node_modules /app/node_modules
COPY --from=build /app/dist /app/dist
CMD [ "pnpm", "start" ]

0 comments on commit f6fa815

Please sign in to comment.