World map

 

BM: Baca dalam Bahasa Melayu

World C2: Container Pipelines

Layer cache in CI

You will learn: to keep your kek lapis layers between runs, so builds get fast.

Checked with GitHub Actions 2026-10 on 31 Oct 2026

The problem

On your laptop, a rebuild takes 10 seconds. On the belt, every build takes 4 minutes. Why?

Pick one answer.

Try it

Even with a cache, this recipe bakes npm ci again after every code change. Move the lines so it stays cached when src/cart.js changes.

Each recipe line adds one cake layer. Put the lines in a good order, then build.

Dockerfile (the recipe)

  1. FROM node:24-alpine
  2. WORKDIR /app
  3. COPY . .
  4. COPY package.json package-lock.json ./
  5. RUN npm ci --omit=dev
  6. CMD ["node", "server.js"]

You built it once. Next, you change src/cart.js.

The cake (your image)

  1. CMD ["node", "server.js"]0B
  2. RUN npm ci --omit=dev12.8MB
  3. COPY package.json package-lock.json ./53.2kB
  4. COPY . .36.9kB
  5. WORKDIR /app8.19kB
  6. FROM node:24-alpine181MB

Watch

Tap the line that saves today's layers for next time.

0 of 1 found

The layer cache: Kek lapis layers kept from an earlier build. Unchanged layers are reused, not baked again. can live in the GitHub Actions cache. cache-from reads old layers. cache-to saves new ones. mode=max keeps middle stages too.

Try it

Press Run. Then press Run again, and watch the build station.

Run it twice. Watch the second run take a shortcut.

What happened on GitHub

Workflow file .github/workflows/image.yml

name: Image
on:
push:
branches: [main]
permissions:
contents: read
jobs:
image:
runs-on: ubuntu-latest
steps:
- uses: docker/setup-buildx-action@v4
- uses: docker/build-push-action@v7
with:
tags: kedai-kancil:ci
cache-from: type=gha
cache-to: type=gha,mode=max

The line

Push to main · main

Predict

Now you add a package, so package.json changes. What bakes again on the belt?

Pick one answer.

Check

Good cache habit? Swipe right for yes, left for “needs work”.

Swipe right for good, left for needs work. Or use the buttons.

1 / 4

Copy the package files and install before copying the code

Copy the package files and install before copying the code

You did it!

You can now keep your layer cache between runs.

Go deeper (optional)

GitHub deletes caches nobody used for a while. Each repo also has a size limit. type=registry keeps the cache in GHCR instead. Two images? Give each its own scope, like type=gha,scope=api.

Real rep (optional):

Real mission

0 of 3 steps done · Your ticks are saved.