Sikau

GitHub Actions · Day 10 · about 35 min

Caching and artifacts

actions/cache for speed, artifacts to pass files between jobs.

Don't do the same work twice

Downloading dependencies on every run is slow. A cache keeps files between runs, keyed by something like a hash of the lockfile. An artifact keeps a job's output — a build, a test report — so a later job (or a human) can download it.

cache, upload, download

name: Build
on: push
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/cache@v4
        with:
          path: ~/.npm
          key: npm-${{ hashFiles('package-lock.json') }}
      - run: npm ci
      - run: npm run build
      - uses: actions/upload-artifact@v4
        with:
          name: dist
          path: dist
  smoke-test:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: dist
          path: dist
      - run: node dist/server.js

The rest of this lesson

  • How cache keys workexplain
  • Build once, deploy that buildexplain
  • Cache or artifact?predict
  • Where should it go?match
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 12 lessons of GitHub Actions