GitLab CI缓存密钥 [英] GitLab CI caching key
问题描述
假设我在 .gitlab-ci.yml
文件中执行以下步骤:
Say I have the following step in my .gitlab-ci.yml
file:
setup_vue:
image: ....
stage: setup
script:
- cd vue/
- npm install --no-audit
cache:
key: node-cache
paths:
- vue/node-modules/
我明白了
Checking cache for node-cache-1...
No URL provided, cache will not be downloaded from shared cache server. Instead a local version of cache will be extracted.
Successfully extracted cache
在脚本运行后:
Creating cache node-cache-1...
Created cache
WARNING: vue/node-modules/: no matching files
No URL provided, cache will be not uploaded to shared cache server. Cache will be stored only locally.
Job succeeded
当我尝试在下一步中获取缓存时,如下所示:
When I try to get the cache on the next step like so:
test_vue:
image: ....
stage: test
cache:
key: node-cache
script:
- cd docker-hotreload-vue
- cqc src
- npm test
它不尝试检索任何缓存,而只是尝试运行脚本(显然失败).根据 GitLab文档,这是正确的方法来做到这一点.(我正在使用dockerRunner)
It doesnt try to retrieve any cache, and just tries to run the script (which fails obviously). According to the GitLab docs this is the correct way to do this. (I'm using a docker runner)
这是我得到的输出:
Fetching changes...
fatal: remote origin already exists.
Removing vue/node_modules/
HEAD is now at ....
Checking out ...
Skipping Git submodules setup
$ cd docker-hotreload-vue
$ cqc src
我正在使用标签来确保同一跑步者正在执行作业.
I am using tags to ensure the same runner is executing the jobs.
推荐答案
尝试将密钥更新为以下内容:
Try updating your key to the below:
cache:
key: ${CI_COMMIT_REF_SLUG}
这解决了我的问题.我经历了3个阶段-构建,测试和打包.如果没有将密钥设置为$ {CI_COMMIT_REF_SLUG},则缓存仅适用于测试阶段.更新密钥后,现在打包阶段还可以正确提取缓存.
This solved my problem. I had 3 stages - build, test, package. Without the key set to ${CI_COMMIT_REF_SLUG}, the cache only worked for test stage. After updating the key, now the package stage can also extract the cache properly.
这篇关于GitLab CI缓存密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!