0

I'm working on configuration of CI/CD in github and faced a problem with caching of dependencies.

My github actions lint config for my Node.js app attached.

As you can see I have additional step called build which is used to cache dependencies using actions/cache@v2. Then on eslint and Prettier steps I extract cached data using restore-keys. The script fails on eslint step with error:

sh: 1: eslint: not found

I have eslint is my devDependencies section in package.json.

name: test

on: [push]

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    container:
      image: node:14.17.0-stretch-slim
    steps:
      - uses: actions/checkout@v2
      - name: Cache dependencies
        uses: actions/cache@v2
        with:
          path: ~/.npm
          key: ${{ runner.OS }}-cache-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.OS }}-cache-
      - name: Install dependencies
        run: npm ci --ignore-scripts
  eslint:
    needs: build
    name: ESLint
    runs-on: ubuntu-latest
    container:
      image: node:14.17.0-stretch-slim
    steps:
      - uses: actions/checkout@v2
      - name: Cache dependencies
        uses: actions/cache@v2
        with:
          path: ~/.npm
          key: ${{ runner.OS }}-cache-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.OS }}-cache-
      - name: Lint source code with ESLint
        run: npm run lint
  prettier:
    needs: build
    name: Prettier
    runs-on: ubuntu-latest
    container:
      image: node:14.17.0-stretch-slim
    steps:
      - uses: actions/checkout@v2
      - name: Cache dependencies
        uses: actions/cache@v2
        with:
          path: ~/.npm
          key: ${{ runner.OS }}-cache-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.OS }}-cache-
      - name: Lint source code with Prettier
        run: npm run check:format
Vitali Skripka
  • 562
  • 1
  • 7
  • 25
  • I'm not sure why you expected that to work. You're caching and restoring `~/.npm`, **not** `node_modules/` (which is fine, that's what's generally recommended) - you still need to _install_, it's just faster because most of the dependencies are already in the local cache. – jonrsharpe May 29 '21 at 18:28
  • @jonrsharpe I use command npm ci --ignore-scripts, it should install dependencies. Or I'm missing something with it? – Vitali Skripka May 29 '21 at 18:32
  • You do in the build job, but **not in the eslint job** where you're trying to run the linter. And it's not clear why you ignore scripts, that might cause problems with some of your dependencies. – jonrsharpe May 29 '21 at 18:33
  • @jonrsharpe I thought that we need caching for the ability to not run installation for each step, only once, on build step. And after this it will retrieve this cache on eslint step. Why I need to run installation again in this case? – Vitali Skripka May 29 '21 at 18:37
  • 3
    Because, again, what you're restoring is **not** `node_modules/`. Read what you've actually written, and think about what it's doing. – jonrsharpe May 29 '21 at 18:38
  • @jonrsharpe ok I got it, now it works. Thanks! – Vitali Skripka May 29 '21 at 18:48

1 Answers1

0

The problem was that I didn't run dependencies installation on eslint and prettier steps. It still needs to be done to create node_modules.

Vitali Skripka
  • 562
  • 1
  • 7
  • 25