node.js - 更新 package.json 后,GitLab CI 中只有 'npm install'

标签 node.js npm gitlab npm-install gitlab-ci

我正在为一个项目使用 GitLab CI,该过程的第一步是 npm install。我缓存 node_modules 以便稍后更快地运行相同的作业,并将它们定义为构建工件以便在以后的阶段使用它们。但是,即使我缓存 node_modules 并且它是最新的,每次运行 install_packages 作业时调用 npm install 也需要很长时间,因为该命令遍历所有 package.json 并检查包的更新等(我假设)。

有没有办法根据某些条件install_packages 作业中运行 npm install?更具体地说(我认为最好的解决方案),自上次构建以来 package.json 是否已更改?

以下是我的 .gitlab-ci.yml 文件的相关部分:

image: node:6.9.1

stages:
  - install
  - prepare
  - deploy

install_packages:
  stage: install
  script:
    - npm prune
    - npm install
  cache:
    key: ${CI_BUILD_REF_NAME}
    paths:
      - node_modules/
  artifacts:
    paths:
      - node_modules/
  only:
    - master
    - develop

build_and_test:
  stage: prepare
  script:
    #do_stuff...

deploy_production:
  stage: deploy
  #do_stuff...

deploy_staging:
  stage: deploy
  #do_stuff...

最佳答案

只需使用 only:changes 标志 doc

工作将是:

install_packages:
  stage: install
  script:
    - npm prune
    - npm install
  cache:
    key: ${CI_COMMIT_REF_NAME}
    paths:
      - node_modules/
  artifacts:
    paths:
      - node_modules/
  only:
    refs:
      - master
      - develop
    changes:
      - package.json

还有一点是:你设置缓存的方式正确吗? 读这个: https://docs.gitlab.com/runner/configuration/autoscale.html#distributed-runners-caching https://docs.gitlab.com/ee/ci/caching/

关于node.js - 更新 package.json 后,GitLab CI 中只有 'npm install',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40615533/

相关文章:

javascript - 在 NodeJs 中呈现 jquery POST 请求的响应

node.js - 限制对 ExpressJS 中静态文件的访问

node.js - 缺少 node-v59-linux-x64/grpc_node.node

node.js - 沙发底座错误 : Data received on socket was not in the expected format - couchbase?

azure - npm 任务未在 Azure 管道中创建 node_modules 文件夹

node.js - 为 Node.js 安装 Web 服务器

docker - .m2的Docker缓存在本地不起作用

javascript - 元素在点 (1254, 21) 处不可单击。其他元素将收到点击

GitLab CI pipeline failing with 您必须将 Bundler 2 或更高版本与此锁定文件一起使用

gitlab转移到新服务器而不创建备份