devinxiang/issue-blog

gitlab runner remove cache

Opened this issue · 0 comments

问题场景:在 ng 从 8 升级到 9 的时候,gitlab-runner 自动取到了之前的缓存,导致编译不成功;
诉求:想要去掉缓存重新编译
解决方法:

  1. 修改 cache:key 的值为一个常量,实现与老版本的区分和最大程度的缓存;
    1. 校验新版本是否生效:
      1. node_modules 正常取到缓存;
      2. 编译正常;
    2. 校验老版本是否正常使用老版本的缓存:
      1. 已正常取到老版本的缓存;
      2. 编译正常

目前的配置:

cache:
	untracked: true
	paths: 
		- node-modules

解决方案:

  1. 手动清除编译机器上的 cache 文件夹内容,不生效,不能解决问题;
    cd /home/gitlab-runner/cache
  2. Clearing the cache by changing cache:key ,实践可以解决问题
    All you have to do is set a new cache: key in your .gitlab-ci.yml. In the next run of the pipeline, the cache will be stored in a different location.
    
  3. Clearing the cache manually

暂时还没有升级,无法使用
Introduced in GitLab 10.4.
If you want to avoid editing .gitlab-ci.yml, you can easily clear the cache via GitLab’s UI:

  1. Navigate to your project’s CI/CD > Pipelines page.
  2. Click on the Clear Runner caches button to clean up the cache.
    https://docs.gitlab.com/ee/ci/caching/img/clear_runners_cache.png
  3. On the next push, your CI/CD job will use a new cache.
    Behind the scenes, this works by increasing a counter in the database, and the value of that counter is used to create the key for the cache by appending an integer to it: 12, etc. After a push, a new key is generated and the old cache is not valid anymore.

衍生问题

第一次把 key 改成了一个变量:${CI_COMMIT_REF_NAME},但是这样每次都要重新安装,那么看了完整文档决定给 key 一个常量,给到常量的时候报错:

解决办法:

  1. 先清除缓存,通过设置code: {} 清除缓存
  2. 重新设置 cache 的 key 的值,即可解除报错,重新开始编译;

cache:key

  • cache:key 的配置说明

    Sharing caches across the same branch

    Define a cache with the key: ${CI_COMMIT_REF_SLUG} so that jobs of each branch always use the same cache:
    cache: key: ${CI_COMMIT_REF_SLUG}
    While this feels like it might be safe from accidentally overwriting the cache, it means merge requests get slow first pipelines, which might be a bad developer experience. The next time a new commit is pushed to the branch, the cache will be re-used.
    To enable per-job and per-branch caching:
    cache: key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG"
    To enable per-branch and per-stage caching:
    cache: key: "$CI_JOB_STAGE-$CI_COMMIT_REF_SLUG"

    Sharing caches across different branches

    If the files you are caching need to be shared across all branches and all jobs, you can use the same key for all of them:
    cache: key: one-key-to-rule-them-all
    To share the same cache between branches, but separate them by job:
    cache: key: ${CI_JOB_NAME}

    Disabling cache on specific jobs

    If you have defined the cache globally, it means that each job will use the same definition. You can override this behavior per-job, and if you want to disable it completely, use an empty hash:
    job: cache: {}

总结

官方文档有想要的一切:

  1. cache 的配置: Cache dependencies in GitLab CI/CD
  2. .yml 的配置: GitLab CI/CD pipeline configuration reference