互联网上充满了关于不缓存Gitlab的提示,但就我而言,我认为Gitlab CI确实可以正确缓存。关键是,npm似乎仍然会重新安装所有内容。
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- vendor/
- bootstrap/
- node_modules/
build-dependencies:
image: ...
stage: build
script:
- cp .env.gitlab-testing .env
- composer install --no-progress --no-interaction
- php artisan key:generate
- npm install
- npm run prod
- npm run prod
artifacts:
paths:
- vendor/
- bootstrap/
- node_modules/
- .env
- public/mix-manifest.json
tags:
- docker
这是我的gitlab-ci.yml文件(..相关部分)。虽然使用了缓存的 Composer 依赖关系,但没有使用node_modules。我什至添加了所有内容以绝望地缓存和工件。
最佳答案
实际上它应该可以工作,您的缓存是全局设置的,您的键指向当前分支${CI_COMMIT_REF_SLUG}
...
这是我的构建,似乎在各个阶段之间缓存了node_modules。
image: node:latest
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
- .next/
stages:
- install
- test
- build
- deploy
install_dependencies:
stage: install
script:
- npm install
test:
stage: test
script:
- npm run test
build:
stage: build
script:
- npm run build
关于npm - Gitlab CI : npm doesn't like the cached node_modules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55359804/