繁体   English   中英

Gitlab CI/CD缓存清理机制

[英]Gitlab CI/CD caching cleanup mechanism

我有一个简单.gitlab-ci.yml文件,其职责是在before_script中创建venv && install requirements.txt && 激活虚拟环境,但只有在venv目录不存在时才会这样做。 然后在下一个阶段,我想重新使用缓存的venv ,我可以成功但管道之间存在问题。

所以主要的想法是有一个独立于每个管道的缓存,例如,第一次我推送到 gitlab 并运行这个管道创建和使用缓存,然后我第二次推送到 gitlab 我不想要那个它将使用以前创建的缓存并重新开始(例如,因为我有新的依赖项),但目前在示例 1中我总是得到相同的缓存,并且它总是使用相同的venv ,这并不理想。 示例 2中,我创建了一个自定义cleanup阶段,在该阶段我删除了缓存,它可以正常工作,因为对于下一个管道,我创建了一个新的venv目录并安装了所有requirements ,但我得到了一个丑陋的WARNING: venv/: no matching files我不想在结果中看到的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在管道中是独立的。 但是现在我得到了丑陋的致命:第一阶段build中的FATAL: file does not exist错误

更新.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

任何想法为什么现在我得到这个以及如何解决它?

在 Linux shell/docker 执行器上使用key: "$CI_PIPELINE_ID"

如果您使用的是带有固定版本的需求文件,则使用$key: "$CI_COMMIT_REF_SLUG"会更有意义,因为您的需求只会在提交 ref 更改中发生变化。 这将允许具有相同提交 ref 的 MR 管道和分支管道共享缓存。

%VAR%语法适用于 Windows 批处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM