GitHub Actions : でnpm installをcacheする方法

対象者

GitHub Actionsでワークフロー内でnpm installをする人でcache(キャッシュ)の設定をまだしていない人

サンプルコード

Github ActionsでESLintのチェックをするAction

name: ESLint

on: pull_request

jobs:
  eslint:
    runs-on: ubuntu-latest
    timeout-minutes: 30

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Use Node.js
        uses: actions/setup-node@v2
        with:
          node-version: 14.x
          registry-url: 'https://npm.pkg.github.com'
          cache: npm # ここでcacheの設定をすることができる

      - name: Install Dependencies
        run: npm install

      - name: Run lint
        run: npm run lint

解説

cache設定をした初回のActionでは、Cacheがないためそれまでのnpm installにかかる時間は変わらないが、2回目から20秒ほど早くなるはず。

※ 入れているライブラリー数によって振れ幅はある

FYI

github.blog