Gitlab CI/CD 缓存清理机制

如何解决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

以及如何解决不同 gitlab 管道之间的缓存问题?

示例 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?