首页 > 解决方案 > 具有多个路径的 GitLab CI 缓存似乎跳过了一个路径

问题描述

我配置了一个 gitlab CI,我在安装阶段有 2 个作业将依赖项拉入缓存位置。然后不同阶段的作业尝试访问这些位置,但似乎只有一个存在。

我根据 Gitlab 提供的 python 示例构建了 CI,可以 [找到这里]。1

我的 .gitlab-ci.yml 文件看起来像这样。

---

cache:
  paths:
  - foo-1
  - foo-2

stages:
- install
- test

install_foo-1_dependencies:
  stage: install
  script:
  - pull foo-1 dependencies

install_foo-2_dependencies:
  stage: install
  script:
  - pull foo-2 dependencies
  tags:
  - ansible-f5-runner

test_dependencies:
  stage: test
  script: 
  - ls foo-1
  - ls foo-2

install_foo-1_dependencies 和 install_foo-2_dependencies 的输出清楚地显示了正在创建的缓存。但是,当您查看 test_dependencies 的输出时,似乎只创建了 foo-1 缓存。

install_foo-1_dependencies 输出:

Fetching changes...
Removing foo-1/
Checking cache for default-5...
Successfully extracted cache
Creating cache default-5...
....
foo-1: found 1000 matching files                     
Created cache

install_foo-2_dependencies 输出:

Fetching changes...
Removing installed-roles/
Checking cache for default-5...
Successfully extracted cache
Creating cache default-5...  
....                 
foo-2: found 1000 matching files        
Created cache

test_dependencies 的输出

Fetching changes...
Removing foo-1/
Checking cache for default-5...
....
Successfully extracted cache
$ ls foo-1
files
$ ls foo-2
ls: cannot access foo-2: No such file or directory

标签: cachinggitlabgitlab-cigitlab-ci-runner

解决方案


您需要确保此管道的每个阶段都使用相同的运行器。从文档

提示:为您的管道使用相同的 Runner,是在一个阶段或管道中缓存文件的最简单有效的方法,并以有保证的方式将此缓存传递给后续阶段或管道。

从您的.gitlab-ci.yml文件中看不出您确保同一个跑步者参加每个阶段。再次从这些文档中,为确保使用一个跑步者,您应该使用以下一种或多种:

  • 标记您的跑步者并在共享其缓存的作业上使用该标记。
  • 使用仅对特定项目可用的粘性跑步者。
  • 使用适合您的工作流程的键(例如,每个分支上的不同缓存)。

推荐阅读