尝试使用之前两个 运行 作业中的文件时,如何在 gitlab 中使用 "cache"?
How to use the "cache" in gitlab when trying to use files from two previously run jobs?
我有以下 .gitlab-ci.yml
配置:
stages:
- stage1
- stage2
- stage3
job1:
stage: stage1
rules:
- if: $RUN != "job3"
when: always
- when: never
script:
- date > data/test1.txt
cache:
key: test-cache
paths:
- data/test1.txt
job2:
stage: stage2
rules:
- if: $RUN != "job3"
when: always
- when: never
script:
- date > test2.txt
cache:
key: test-cache
paths:
- test2.txt
job3:
stage: stage3
rules:
- if: $RUN == "job3"
script:
- cat data/test1.txt
- cat test2.txt
cache:
key: test-cache
paths:
- data/test1.txt
- test2.txt
计划执行以下操作:
- 至运行 未定义变量
RUN
的管道,因此只有两个作业 job1
和 job2
是 运行,但不是job3
。 job1
和 job2
这两个作业应该将文件 data/test1.txt
和 test2.txt
添加到缓存中,以便稍后 job3
可以获取它们。
- 到 运行 变量
RUN
设置为 run3
的管道,它仅 运行 作业 job3
。该作业应该从缓存中获取先前生成的文件,即 data/test1.txt
和 test2.txt
. 这两个文件
解释:
实际上,job1
和 job2
这两个作业是非常耗时的作业,也会消耗大量 API 调用。因此,我不想无缘无故地重新运行它们,因为它们无论如何都会产生相同的结果。我只想“存储”这两个作业 job1
和 job2
的结果,并从稍后的 运行ning 作业(可能几天后)中“获取”这些结果。此 job3
也将 运行 在同一回购协议和同一分支的同一管道中。作为工作 job1
和 job2
.
预期结果:
job3
可以使用 job1
和 job2
.
创建的缓存中的两个文件
观测结果:
$ cat data/test1.txt
cat: data/test1.txt: No such file or directory
我有以下问题:
- 我该如何解决这个问题?问题是什么?
- 这是在哪里记录的?
根据GitLab:
对依赖项使用缓存,例如从 Internet 下载的包。如果启用分布式缓存,缓存存储在安装 GitLab Runner 的位置并上传到 S3。
使用工件在阶段之间传递中间构建结果。工件由作业生成,存储在 GitLab 中,可以下载。
工件和缓存都定义了它们相对于项目目录的路径,并且不能link到它之外的文件。
解决方案
我猜下面会修复它,方法是将默认工件路径添加到 data
目录。这会将每个作业结果存储在 data
目录中,并使其在下一个作业中可用。
要使用来自多个作业的工件,您应该使用 dependencies。
default:
artifacts:
paths:
- ./data
stages:
- stage1
- stage2
- stage3
job1:
stage: stage1
rules:
- if: $RUN != "job3"
when: always
- when: never
script:
- date > data/test1.txt
job2:
stage: stage2
rules:
- if: $RUN != "job3"
when: always
- when: never
script:
- date > test2.txt
job3:
stage: stage3
dependencies:
- job1
- job2
rules:
- if: $RUN == "job3"
script:
- cat data/test1.txt
- cat test2.txt
工作解决方案是以下配置:
job1:
stage: stage1
rules:
- if: $RUN != "job3"
when: always
- when: never
script:
- date > data/test1.txt
cache:
key: test-cache
paths:
- data/test1.txt
- test2.txt
job2:
stage: stage2
rules:
- if: $RUN != "job3"
when: always
- when: never
script:
- date > test2.txt
cache:
key: test-cache
paths:
- data/test1.txt
- test2.txt
job3:
stage: stage3
rules:
- if: $RUN == "job3"
script:
- cat data/test1.txt
- cat test2.txt
cache:
key: test-cache
paths:
- data/test1.txt
- test2.txt
文档说(如果您能够在非常庞大的文档中找到特定的 statement):
If two jobs have the same cache key but a different path, the caches
can be overwritten.
所以解决方案是对三个作业中的每个使用相同的“东西”,所以gitlab一直试图gull/push这两个文件。
我有以下 .gitlab-ci.yml
配置:
stages:
- stage1
- stage2
- stage3
job1:
stage: stage1
rules:
- if: $RUN != "job3"
when: always
- when: never
script:
- date > data/test1.txt
cache:
key: test-cache
paths:
- data/test1.txt
job2:
stage: stage2
rules:
- if: $RUN != "job3"
when: always
- when: never
script:
- date > test2.txt
cache:
key: test-cache
paths:
- test2.txt
job3:
stage: stage3
rules:
- if: $RUN == "job3"
script:
- cat data/test1.txt
- cat test2.txt
cache:
key: test-cache
paths:
- data/test1.txt
- test2.txt
计划执行以下操作:
- 至运行 未定义变量
RUN
的管道,因此只有两个作业job1
和job2
是 运行,但不是job3
。job1
和job2
这两个作业应该将文件data/test1.txt
和test2.txt
添加到缓存中,以便稍后job3
可以获取它们。 - 到 运行 变量
RUN
设置为run3
的管道,它仅 运行 作业job3
。该作业应该从缓存中获取先前生成的文件,即data/test1.txt
和test2.txt
. 这两个文件
解释:
实际上,job1
和 job2
这两个作业是非常耗时的作业,也会消耗大量 API 调用。因此,我不想无缘无故地重新运行它们,因为它们无论如何都会产生相同的结果。我只想“存储”这两个作业 job1
和 job2
的结果,并从稍后的 运行ning 作业(可能几天后)中“获取”这些结果。此 job3
也将 运行 在同一回购协议和同一分支的同一管道中。作为工作 job1
和 job2
.
预期结果:
job3
可以使用 job1
和 job2
.
观测结果:
$ cat data/test1.txt
cat: data/test1.txt: No such file or directory
我有以下问题:
- 我该如何解决这个问题?问题是什么?
- 这是在哪里记录的?
根据GitLab:
对依赖项使用缓存,例如从 Internet 下载的包。如果启用分布式缓存,缓存存储在安装 GitLab Runner 的位置并上传到 S3。
使用工件在阶段之间传递中间构建结果。工件由作业生成,存储在 GitLab 中,可以下载。
工件和缓存都定义了它们相对于项目目录的路径,并且不能link到它之外的文件。
解决方案
我猜下面会修复它,方法是将默认工件路径添加到 data
目录。这会将每个作业结果存储在 data
目录中,并使其在下一个作业中可用。
要使用来自多个作业的工件,您应该使用 dependencies。
default:
artifacts:
paths:
- ./data
stages:
- stage1
- stage2
- stage3
job1:
stage: stage1
rules:
- if: $RUN != "job3"
when: always
- when: never
script:
- date > data/test1.txt
job2:
stage: stage2
rules:
- if: $RUN != "job3"
when: always
- when: never
script:
- date > test2.txt
job3:
stage: stage3
dependencies:
- job1
- job2
rules:
- if: $RUN == "job3"
script:
- cat data/test1.txt
- cat test2.txt
工作解决方案是以下配置:
job1:
stage: stage1
rules:
- if: $RUN != "job3"
when: always
- when: never
script:
- date > data/test1.txt
cache:
key: test-cache
paths:
- data/test1.txt
- test2.txt
job2:
stage: stage2
rules:
- if: $RUN != "job3"
when: always
- when: never
script:
- date > test2.txt
cache:
key: test-cache
paths:
- data/test1.txt
- test2.txt
job3:
stage: stage3
rules:
- if: $RUN == "job3"
script:
- cat data/test1.txt
- cat test2.txt
cache:
key: test-cache
paths:
- data/test1.txt
- test2.txt
文档说(如果您能够在非常庞大的文档中找到特定的 statement):
If two jobs have the same cache key but a different path, the caches can be overwritten.
所以解决方案是对三个作业中的每个使用相同的“东西”,所以gitlab一直试图gull/push这两个文件。