GitLab CI caching key

大兔子大兔子 提交于 2021-01-01 06:40:20

问题


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/

I see;

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

And after the script runs:

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

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)

Here's the output I get:

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.


回答1:


Try updating your key to the below:

cache:
  key: ${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.



来源:https://stackoverflow.com/questions/55512534/gitlab-ci-caching-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!