Node.js 应用程序的 Github 操作缓存不起作用

问题描述

我正在 github 中配置 CI/CD,但遇到了依赖项缓存的问题。

附加了我的 Node.js 应用程序的 github 操作 lint 配置。

如您所见,我有一个名为 build 的附加步骤,用于使用 actions/cache@v2 缓存依赖项。然后在 eslintPrettier 步骤中,我使用 restore-keys 提取缓存数据。 脚本在 eslint 步骤失败并出现错误

sh: 1: eslint: not found

我在 package.json 中有 eslint 是我的 devDependencies 部分。

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

解决方法

问题是我没有在 eslintprettier 步骤中运行依赖项安装。创建node_modules仍然需要完成。