Gitlab CI/CD 缓存清理机制

Gitlab CI/CD caching cleanup mechanism

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

所以主要的想法是有一个独立于每个管道的缓存,例如,我第一次推送到 gitlab 并运行这个创建和使用缓存的管道,然后我第二次推送到 gitlab我不希望它使用以前创建的缓存并重新开始(因为例如我有新的依赖项),但目前在 Example 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 跨管道独立。但是现在我在第一阶段 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" 会更有意义,因为您的需求只会在提交引用更改时发生变化。这将允许具有相同提交引用的 MR 管道和分支管道共享缓存。

%VAR% 语法用于 Windows 批次。