【问题标题】:Caching npm dependencies in circleci在 circleci 中缓存 npm 依赖项
【发布时间】:2021-03-26 10:11:31
【问题描述】:

我正在为位于我的 repo 的 backend/core 文件夹中的现有 Express 服务器项目设置 CI。从基本设置和 linting 开始。我能够让 npm install 和 linting 工作,但我想缓存依赖项,这样每次推送就不需要 4 分钟加载。

我使用了他们描述的缓存方案here,但它似乎仍然每次都运行完整安装。或者如果它使用缓存的依赖项,它每次都安装 grpc,这需要一段时间。有什么想法我能做什么?

我的 config.yml 供参考:

# Use the latest 2.1 version of CircleCI pipeline process engine. See: https://circleci.com/docs/2.0/configuration-reference

# default executors
executors:
  core-executor:
    docker:
      - image: 'cimg/base:stable'

commands:
  init_env:
    description: initialize environment
    steps:
      - checkout
      - node/install
      - restore_cache:
          keys:
            # when lock file changes, use increasingly general patterns to restore cache
            - node-v1-{{ .Branch }}-{{ checksum "backend/core/package-lock.json" }}
            - node-v1-{{ .Branch }}-
            - node-v1-

      - run: npm --prefix ./backend/core install

      - save_cache:
          paths:
            - ~/backend/core/usr/local/lib/node_modules  # location depends on npm version
          key: node-v1-{{ .Branch }}-{{ checksum "backend/core/package-lock.json" }}


jobs:
  install-node:
    executor: core-executor
    steps:
      - checkout
      - node/install
      - run: node --version
      - run: pwd
      - run: ls -A
      - run: npm --prefix ./backend/core install

  lint:
    executor: core-executor
    steps:
      - init_env
      - run: pwd
      - run: ls -A
      - run: ls backend
      - run: ls backend/core -A
      - run: npm --prefix ./backend/core run lint

orbs:
  node: circleci/node@4.1.0
version: 2.1
workflows:
  test_my_app:
    jobs:
      #- install-node
      - lint
          #requires:
            #- install-node

【问题讨论】:

    标签: node.js npm caching circleci circleci-2.0


    【解决方案1】:

    我认为最好的办法是使用更快的npm ci。最好的解释在这里:https://stackoverflow.com/a/53325242/4410223。即使它每次都会重新安装,它的一致性也比缓存好。虽然在使用它时,我不确定在管道中继续使用缓存的意义何在,但似乎仍然建议使用 npm ci 进行缓存。

    但是,最好的方法是只使用配置中已有的 node orb。 - node/install-packages 的一个步骤将为您完成所有工作。您将能够用您的restore_cachenpm installsave_cache 步骤替换它。您甚至可以在此处查看它执行的所有步骤:https://circleci.com/developer/orbs/orb/circleci/node#commands-install-packages。只需打开命令源,查看第 71 行的步骤即可。

    【讨论】:

      猜你喜欢
      • 2019-06-17
      • 2018-04-02
      • 1970-01-01
      • 2021-01-21
      • 2015-09-30
      • 2018-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多