我正在为一个项目使用 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/