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)
FROM node:24-alpineWORKDIR /appCOPY . .COPY package.json package-lock.json ./RUN npm ci --omit=devCMD ["node", "server.js"]
You built it once. Next, you change src/cart.js.
The cake (your image)
CMD ["node", "server.js"]RUN npm ci --omit=devCOPY package.json package-lock.json ./COPY . .WORKDIR /appFROM node:24-alpine
Hint
Copy the package files and install first. Copy the code last.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.
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.
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.
Bahasa Melayu
Di laptop anda, build semula ambil 10 saat. Di tali sawat, setiap build ambil 4 minit. Kenapa?
Walaupun ada cache, resipi ini bakar npm ci semula selepas setiap perubahan kod. Alihkan baris supaya layer (lapisan) itu kekal dalam cache bila src/cart.js berubah.
Cache layer boleh disimpan dalam cache GitHub Actions. cache-from baca layer lama. cache-to simpan yang baru. mode=max simpan peringkat tengah juga.
Tekan Run. Kemudian tekan Run sekali lagi, dan tengok stesen build.
Sekarang anda tambah satu pakej, jadi package.json berubah. Apa yang dibakar semula di tali sawat?
Tabiat cache yang bagus? Leret ke kanan untuk ya, ke kiri untuk “perlu dibaiki”.
Kini anda boleh simpan cache layer antara run.
You finished this before. Practice is always welcome!
Official docs: Docker docs: cache management with GitHub Actions · Docker docs: GitHub Actions cache backend
You've learned for 25 minutes. A short break helps your brain.
Sprint done! Great focus. Stretch for a minute?