Interview Prep/GitHub Actions/Speed, Caching & Cost

Docker builds take 12 minutes even when only one line of code changed. Speed them up.

IntermediateScenarioSpeed, Caching & Cost
Answer

Situation read → No layer cache on ephemeral runners, likely poor Dockerfile ordering.

What I'd do →

  1. Fix Dockerfile order: copy lockfile, install, then copy source.
  2. Use BuildKit with the GHA cache:
    - uses: docker/setup-buildx-action@v3
    - uses: docker/build-push-action@v6
      with:
        cache-from: type=gha,scope=api
        cache-to: type=gha,mode=max,scope=api
    
  3. Use RUN --mount=type=cache,target=/root/.npm npm ci for package caches inside the build.
  4. For images over a few GB, use type=registry cache in ECR instead to avoid evicting the repo's other caches.

Why → Each hosted job starts with an empty Docker daemon; without an exported cache nothing is reused.

Interview tip → Mention mode=max for multi-stage builds.

All GitHub Actions questions Roadmap: GitHub Actions

More on Speed, Caching & Cost