如何解决Gitlab CI/CD 缓存清理机制
我有一个简单的 .gitlab-ci.yml
文件,其职责是在 venv
中创建 requirements.txt
&& install before_script
&& 激活虚拟环境,但这仅在 venv
{1}} 目录不存在。然后在接下来的每个阶段,我都想重新使用缓存的 venv
,我可以成功地完成但在管道之间存在问题。
所以主要想法是在每个管道上都有一个独立的缓存,例如,第一次我推送到 gitlab 并运行此管道创建和使用缓存,然后第二次我推送到 gitlab 我做不希望它使用以前创建的缓存并重新开始(因为例如我有新的依赖项),但目前在 示例 1 中我总是获得相同的缓存并且它总是使用相同的 { {1}},这并不理想。在示例 2 中,我创建了一个自定义 venv
阶段,我在其中删除了缓存,然后在下一个管道中正常工作,我创建了一个新的 cleanup
目录并安装所有 venv
,但我得到了一个丑陋的 requirements
,我不想在结果中看到它。
自定义清理缓存警告消息
WARNING: venv/: no matching files
所以我的问题是:如何为单个管道正确创建 ....
Restoring cache
00:02
Checking cache for %CI_PIPELINE_ID%-2...
Downloading cache.zip from https://storage.googleapis.com/gitlab-com-runners-cache/project/<>/%25CI_PIPELINE_ID%25-2
WARNING: venv/bin/python: chmod venv/bin/python: no such file or directory (suppressing repeats)
Successfully extracted cache
Executing "step_script" stage of the job script
00:01
Using docker image sha256:fc14d038d14407498be583a6aa2e27d6b251814e9b004b003ee17bfdc038d5a1 for python:3.9.2-alpine3.12 with digest python@sha256:f092b9adbc7cbc012e9b857899e043af5d4de9ffd01ec32cb12ba38c295752d4 ...
$ python -V
Python 3.9.2
$ ls -la
total 72
drwxrwxrwx 5 root root 4096 Mar 12 11:41 .
drwxrwxrwx 4 root root 4096 Mar 12 11:41 ..
drwxrwxrwx 6 root root 4096 Mar 12 11:41 .git
-rw-rw-rw- 1 root root 41 Mar 12 11:41 .gitignore
-rw-rw-rw- 1 root root 664 Mar 12 11:41 .gitlab-ci.yml
-rw-rw-rw- 1 root root 15 Mar 12 11:41 README.md
drwxrwxrwx 5 root root 4096 Mar 12 11:41 app
-rw-rw-rw- 1 root root 275 Mar 12 11:41 requirements.txt
drwxr-xr-x 5 root root 4096 Mar 12 11:32 venv
$ pwd
/builds/rsimkus/static-flask-website
$ [[ ! -d "venv" ]] && python3.9 -m venv venv && source venv/bin/activate && pip install -r requirements.txt || source venv/bin/activate
$ echo "removing venv dir for next pipeline"
removing venv dir for next pipeline
$ rm -rf venv
Saving cache for successful job
00:02
Creating cache %CI_PIPELINE_ID%-2...
WARNING: venv/: no matching files
Uploading cache.zip to https://storage.googleapis.com/gitlab-com-runners-cache/project/<>/%25CI_PIPELINE_ID%25-2
Created cache
Cleaning up file based variables
00:01
Job succeeded
并且不担心下一个管道会使用以前缓存的 cache
?
示例 1
venv
示例 2
image: python:3.9.2-alpine3.12
stages:
- build
- test
cache:
key: "%CI_PIPELINE_ID%"
paths:
- venv/
before_script:
- python -V
- ls -la
- pwd
- '[[ ! -d "venv" ]] && python3.9 -m venv venv && source venv/bin/activate && pip install -r requirements.txt || source venv/bin/activate'
build-app:
stage: build
script:
- echo "building static flask app"
test-code:
stage: test
script:
- echo "running unit tests with pytest module"
- pytest
更新 2021-03-15
我已经用正确的语法更新了 .yml 文件,从 image: python:3.9.2-alpine3.12
stages:
- build
- test
- cleanup
cache:
key: "%CI_PIPELINE_ID%"
paths:
- venv/
before_script:
- python -V
- ls -la
- pwd
- '[[ ! -d "venv" ]] && python3.9 -m venv venv && source venv/bin/activate && pip install -r requirements.txt || source venv/bin/activate'
build-app:
stage: build
script:
- echo "building static flask app"
test-code:
stage: test
script:
- echo "running unit tests with pytest module"
- pytest
cleanup-venv:
stage: cleanup
script:
- echo "removing venv dir for next pipeline"
- rm -rf venv
到 %CI_PIPELINE_ID%
,这起到了作用,$CI_PIPELINE_ID
跨管道独立。但是现在我在第一阶段 venv
FATAL: file does not exist
错误
更新了 .yml
build
构建阶段日志
image: python:3.9.2-alpine3.12
stages:
- build
- test
cache:
key: "$CI_PIPELINE_ID"
paths:
- venv/
before_script:
- python -V
- ls -la
- pwd
- '[[ ! -d "venv" ]] && python3.9 -m venv venv && source venv/bin/activate && pip install -r requirements.txt || source venv/bin/activate'
build-app:
stage: build
script:
- echo "building static flask app"
test-code:
stage: test
script:
- echo "running unit tests with pytest module"
- pytest
任何想法为什么我现在得到这个以及如何解决它?
解决方法
在 Linux shell/docker 执行器上使用 key: "$CI_PIPELINE_ID"
如果您使用的是带有固定版本的需求文件,使用 $key: "$CI_COMMIT_REF_SLUG"
会更有意义,因为您的需求只会在提交引用更改时发生变化。这将允许具有相同提交引用的 MR 管道和分支管道共享缓存。
%VAR%
语法适用于 Windows 批处理。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。