Gitlab CI 每次作业使用后再次保存缓存
Gitlab CI saves the cache again after each job using it
基于 Gitlab CI 关于缓存归档和提取工作原理的文档 (https://docs.gitlab.com/ee/ci/caching/#how-archiving-and-extracting-works):
stages:
- build
- test
before_script:
- echo "Hello"
job A:
stage: build
script:
- mkdir vendor/
- echo "build" > vendor/hello.txt
cache:
key: build-cache
paths:
- vendor/
after_script:
- echo "World"
job B:
stage: test
script:
- cat vendor/hello.txt
cache:
key: build-cache
job A
这里应该创建缓存,job B
应该使用它。
虽然尝试此配置时发生了什么,但 job B
一旦脚本成功执行,而不是结束,再次保存缓存目录(完全没用,没有任何改变,这不是它的目的无论如何):
Running with gitlab-runner 13.3.1 (738bbe5a)
on docker-auto-scale fa6cab46
Preparing the "docker+machine" executor
00:40
Using Docker executor with image node:latest ...
Pulling docker image node:latest ...
Using docker image sha256:c2b4a3b480bf1adee17db717baa58f4b13c59cc712cd4199706406aa09ea0566 for node:latest ...
Preparing environment
00:04
Running on runner-fa6cab46-project-[REDACTED]-concurrent-0 via runner-fa6cab46-srm-1599721095-1e9a7c0b...
Getting source from Git repository
00:03
$ eval "$CI_PRE_CLONE_SCRIPT"
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/[REDACTED]/.git/
Created fresh repository.
Checking out b24f54fb as v1.3.0...
Skipping Git submodules setup
Restoring cache
00:21
Checking cache for v1-3-0_modules_app-26...
Downloading cache.zip from https://storage.googleapis.com/gitlab-com-runners-cache/project/[REDACTED]/v1-3-0_modules_app-26
Successfully extracted cache
Executing "step_script" stage of the job script
[...]
Saving cache
00:43
Creating cache v1-3-0_modules_app-26...
untracked: found 66568 files
Uploading cache.zip to https://storage.googleapis.com/gitlab-com-runners-cache/project/[REDACTED]/v1-3-0_modules_app-26
Created cache
[...]
除了文档没有提到这个“功能”之外,如何在后续作业中使用缓存而不让每个作业都浪费时间再次缓存它?
这是 gitlab 的正常行为,但你可以通过定义一个简单的策略来实现你想要的
在此处查看文档https://docs.gitlab.com/ee/ci/caching/#inherit-global-config-but-override-specific-settings-per-job
类似
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
- public/
- vendor/
policy: pull-push
job:
cache:
# inherit all global cache settings
<<: *global_cache
# override the policy
policy: pull
在此示例中,作业(称为作业)只会拉取缓存
基于 Gitlab CI 关于缓存归档和提取工作原理的文档 (https://docs.gitlab.com/ee/ci/caching/#how-archiving-and-extracting-works):
stages:
- build
- test
before_script:
- echo "Hello"
job A:
stage: build
script:
- mkdir vendor/
- echo "build" > vendor/hello.txt
cache:
key: build-cache
paths:
- vendor/
after_script:
- echo "World"
job B:
stage: test
script:
- cat vendor/hello.txt
cache:
key: build-cache
job A
这里应该创建缓存,job B
应该使用它。
虽然尝试此配置时发生了什么,但 job B
一旦脚本成功执行,而不是结束,再次保存缓存目录(完全没用,没有任何改变,这不是它的目的无论如何):
Running with gitlab-runner 13.3.1 (738bbe5a)
on docker-auto-scale fa6cab46
Preparing the "docker+machine" executor
00:40
Using Docker executor with image node:latest ...
Pulling docker image node:latest ...
Using docker image sha256:c2b4a3b480bf1adee17db717baa58f4b13c59cc712cd4199706406aa09ea0566 for node:latest ...
Preparing environment
00:04
Running on runner-fa6cab46-project-[REDACTED]-concurrent-0 via runner-fa6cab46-srm-1599721095-1e9a7c0b...
Getting source from Git repository
00:03
$ eval "$CI_PRE_CLONE_SCRIPT"
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/[REDACTED]/.git/
Created fresh repository.
Checking out b24f54fb as v1.3.0...
Skipping Git submodules setup
Restoring cache
00:21
Checking cache for v1-3-0_modules_app-26...
Downloading cache.zip from https://storage.googleapis.com/gitlab-com-runners-cache/project/[REDACTED]/v1-3-0_modules_app-26
Successfully extracted cache
Executing "step_script" stage of the job script
[...]
Saving cache
00:43
Creating cache v1-3-0_modules_app-26...
untracked: found 66568 files
Uploading cache.zip to https://storage.googleapis.com/gitlab-com-runners-cache/project/[REDACTED]/v1-3-0_modules_app-26
Created cache
[...]
除了文档没有提到这个“功能”之外,如何在后续作业中使用缓存而不让每个作业都浪费时间再次缓存它?
这是 gitlab 的正常行为,但你可以通过定义一个简单的策略来实现你想要的
在此处查看文档https://docs.gitlab.com/ee/ci/caching/#inherit-global-config-but-override-specific-settings-per-job
类似
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
- public/
- vendor/
policy: pull-push
job:
cache:
# inherit all global cache settings
<<: *global_cache
# override the policy
policy: pull
在此示例中,作业(称为作业)只会拉取缓存