【问题标题】:Github actions caching for Node.js application is not workingNode.js 应用程序的 Github 操作缓存不起作用
【发布时间】:2021-08-17 15:08:18
【问题描述】:

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

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

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

sh: 1: eslint: not found

我有 eslint 是我在 package.json 中的 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

【问题讨论】:

  • 我不知道你为什么期望它起作用。您正在缓存和恢复 ~/.npmnot node_modules/(这很好,这是通常推荐的) - 您仍然需要 install,它更快,因为大多数的依赖项已经在本地缓存中。
  • @jonrsharpe 我使用命令 npm ci --ignore-scripts,它应该安装依赖项。还是我错过了什么?
  • 您在构建作业中执行此操作,但不是在您尝试运行 linter 的 eslint 作业中。而且不清楚为什么要忽略脚本,这可能会导致某些依赖项出现问题。
  • @jonrsharpe 我认为我们需要缓存以便能够在构建步骤中不为每个步骤运行安装,仅运行一次。在此之后,它将在 eslint 步骤中检索此缓存。为什么在这种情况下我需要再次运行安装?
  • 因为,同样,您要恢复的内容不是 node_modules/。阅读您实际编写的内容,并思考它在做什么。

标签: node.js continuous-integration github-actions github-actions-runners


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-23
    • 2014-05-18
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多