我有一个简单的.gitlab-ci.yml文件,它的职责是创建venv &安装requirements.txt &激活before_script中的虚拟环境,但是只有当venv dir不存在时才会这样做。然后,在下一个阶段,我希望重用缓存的venv,这是我可以成功完成的,但是管道之间存在问题。
因此,主要的想法是有一个独立于每个管道的缓存,例如,第一次我推到gitlab,它运行这个创建和使用缓存的管道,然后第二次向gitlab推送,我不希望它使用以前创建的缓存并重新启动(例如,我有新的依赖项),但是在示例1中,我总是得到相同的缓存,并且它总是使用相同的venv,这并不理想。在示例2中,我已经创建了一个自定义的cleanup阶段,在这个阶段中,我删除了缓存,它可以正常工作,对于下一个管道,我创建了一个新的venv dir并安装了所有的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,而不必担心下一个管道将使用以前缓存的venv。
以及如何解决不同gitlab管道之间的缓存问题?
示例1
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示例2
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任何提示、技巧或评论都将受到赞赏。
更新2021-03-15
我已经用正确的语法更新了.yml文件,从%CI_PIPELINE_ID%到$CI_PIPELINE_ID都是如此,venv跨管道是独立的。但是现在我在第一阶段的FATAL: file does not exist中出现了丑陋的build错误。
更新的.yml
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建造阶段日志
Created fresh repository.
Checking out eef91fb7 as master...
Skipping Git submodules setup
Restoring cache
00:00
Checking cache for 270417733-2...
FATAL: file does not exist
Failed to extract cache
Executing "step_script" stage of the job script有什么想法吗,为什么我现在得到了这个,以及如何解决它?
发布于 2021-03-13 15:11:13
在Linux /docker执行器上使用key: "$CI_PIPELINE_ID"
如果您使用的是固定版本的需求文件,那么使用$key: "$CI_COMMIT_REF_SLUG"会更有意义,因为您的需求只会在提交引用更改中更改。这将允许具有相同提交引用的MR管道和分支管道共享缓存。
%VAR%语法适用于Windows。
https://stackoverflow.com/questions/66600234
复制相似问题