Skip to main content
⚑ OpenDevDocs
GitHub Actions
devopsgithub-actionsGuideIntermediate

GitHub Actions Caching & Artifacts

Accelerate pipeline speeds with dependency and build caching, and pass compiled binaries and test reports between jobs with artifacts.

Verified: 2026-10-01
Tested on:github-actions current
Platforms:
all
#github-actions#caching#artifacts#optimization#performance

GitHub Actions Caching & Artifacts

Optimizing CI/CD execution speed and retaining build outputs requires understanding the difference between Caching and Artifacts.


1. Caching vs Artifacts

FeatureCaching (actions/cache)Artifacts (actions/upload-artifact)
PurposeReusing dependencies and build caches to speed up runsSaving test reports, coverage, and compiled binary releases
LifecyclePersisted across different workflow runs for up to 7 daysStored for 90 days (configurable) for download in GitHub UI
Job SharingUsed across different workflow runs on same branch/default branchPassed between different jobs within the same workflow run
Storage Limit10 GB per repositoryConsumes GitHub Actions storage quota

2. Dependency Caching

Official setup actions provide built-in package manager caching:

- name: Setup Node.js with PNPM cache
  uses: actions/setup-node@v4
  with:
    node-version: 22
    cache: 'pnpm'

2.2 Explicit Caching with actions/cache@v4

For custom directories like Next.js build cache:

- name: Cache Next.js build cache
  uses: actions/cache@v4
  with:
    path: |
      ~/.npm
      ${{ github.workspace }}/.next/cache
    # Generate a unique key based on lockfiles and source files
    key: ${{ runner.os }}-nextjs-${{ hashFiles('**/pnpm-lock.yaml') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
    # Fallback to restore cache from previous commit if exact key misses
    restore-keys: |
      ${{ runner.os }}-nextjs-${{ hashFiles('**/pnpm-lock.yaml') }}-

3. Passing Artifacts Between Jobs

Use upload-artifact and download-artifact to compile once and reuse across testing or deployment jobs:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: 'pnpm'
      - run: pnpm install --frozen-lockfile
      - run: pnpm build

      # Upload the production build output
      - name: Upload production artifact
        uses: actions/upload-artifact@v4
        with:
          name: web-dist
          path: .next/standalone
          retention-days: 7

  deploy:
    needs: build
    runs-on: ubuntu-latest
    steps:
      # Download artifact generated by build job
      - name: Download web build
        uses: actions/download-artifact@v4
        with:
          name: web-dist
          path: ./dist

      - name: Verify build files
        run: ls -la ./dist

On this page