如何缓存“诗歌安装”对于 Gitlab CI?

发布于 2025-01-09 19:16:08 字数 347 浏览 0 评论 0原文

有没有办法在 Gitlab CI (.gitlab-ci.yml) 中缓存 poetry install 命令?

例如,在 node yarn 中,有一种方法可以缓存 yarn install (https://classic.yarnpkg.com/lang/en/docs/install-ci/ Gitlab部分)这使得阶段变得更快。

Is there a way to cache poetry install command in Gitlab CI (.gitlab-ci.yml)?

For example, in node yarn there is a way to cache yarn install (https://classic.yarnpkg.com/lang/en/docs/install-ci/ Gitlab section) this makes stages a lot faster.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阿楠 2025-01-16 19:16:08

GitLab 只能缓存工作目录中的内容 和 Poetry 默认将包存储在其他地方

将在其中创建虚拟环境的目录。默认为 {cache-dir}/virtualenvs(Windows 上为 {cache-dir}\virtualenvs)。

在我的机器上,cache-dir/home/chris/.cache/pypoetry

您可以使用virtualenvs.in-project选项 改变这种行为:

如果设置为 true,则将在项目根目录中名为 .venv 的文件夹中创建 virtualenv。

所以,类似这样的东西应该在你的 gitlab-ci.yml 中工作:

before_script:
  - poetry config virtualenvs.in-project true

cache:
  paths:
    - .venv

GitLab can only cache things in the working directory and Poetry stores packages elsewhere by default:

Directory where virtual environments will be created. Defaults to {cache-dir}/virtualenvs ({cache-dir}\virtualenvs on Windows).

On my machine, cache-dir is /home/chris/.cache/pypoetry.

You can use the virtualenvs.in-project option to change this behaviour:

If set to true, the virtualenv wil be created and expected in a folder named .venv within the root directory of the project.

So, something like this should work in your gitlab-ci.yml:

before_script:
  - poetry config virtualenvs.in-project true

cache:
  paths:
    - .venv
一曲爱恨情仇 2025-01-16 19:16:08

您可以将 Poetry virtualenv 缓存目录 设置为 GitLab 项目空间中的某个目录,例如:

variables:
    POETRY_VIRTUALENVS_PATH: $CI_PROJECT_DIR/venv

然后像平常一样缓存它:

cache:
    paths:
        - venv

You can set the Poetry virtualenv cache directory to something within the GitLab project space, e.g.:

variables:
    POETRY_VIRTUALENVS_PATH: $CI_PROJECT_DIR/venv

and then cache it like normal:

cache:
    paths:
        - venv
生寂 2025-01-16 19:16:08

结合 @Chris 和 @Sean Connolly 的答案帮助我转移了 Python 虚拟环境以及诗歌二进制文件夹的依赖关系。

我使用 POETRY_VIRTUALENVS_PATH 来指定虚拟环境的实例化位置。 诗歌文档第 2 步安装中说明诗(高级)来自使用官方安装程序,可以定义自定义安装文件夹。然后我安装poetry并指定它应该安装在%CI_PROJECT_DIR/poetry中。现在我知道了 poetry 和 venv 文件夹的安装位置,因此我可以使用工件进入下一阶段。在此阶段,我导出诗歌二进制文件,以便可以在不直接引用文件夹的情况下使用它。

下面的 yaml 文件应该可以工作:

---
stages:
  - lint
  - test

pre-commit:
  stage: lint
  image: python:3.11-alpine
  variables:
    POETRY_VIRTUALENVS_PATH: $CI_PROJECT_DIR/.venv
  before_script:
    - apk add curl
    - apk add git
    - curl -sSL https://install.python-poetry.org | POETRY_HOME=$CI_PROJECT_DIR/poetry python3 -
    - export PATH="/builds/GROUP_NAME/REPOSITORY_NAME/poetry/bin:$PATH"
    - poetry config virtualenvs.in-project true
    - poetry install
  script:
    - poetry run pre-commit run --all-files
  artifacts:
    paths:
      - $CI_PROJECT_DIR/.venv
      - $CI_PROJECT_DIR/poetry


quality-checks:
  stage: test
  image: python:3.11-alpine
  dependencies:
    - pre-commit
  before_script:
    - apk add curl
    - apk add bash
    - apk add git
  script:
    - export PATH="/builds/GROUP_NAME/REPOSITORY_NAME/bin:$PATH"
    - source $CI_PROJECT_DIR/.venv/bin/activate
    - poetry run pytest

Combining the answers of @Chris and @Sean Connolly helped me to transfer the dependencies of my Python virtual environment and also the poetry binary folder.

I used the POETRY_VIRTUALENVS_PATH to specify where the virtual environment will be instantiated. The poetry documentation states in step 2 Install Poetry (advanced) from the With the official installer that it is possible to define a custom install folder. Then I install poetry and specify that it should be installed in %CI_PROJECT_DIR/poetry. Now I know where poetry and the venv folders are installed thus I can passed to the next stage using artifacts. In this stage I export the poetry binary so it can be used without referencing the folder directly.

The yaml file below should work:

---
stages:
  - lint
  - test

pre-commit:
  stage: lint
  image: python:3.11-alpine
  variables:
    POETRY_VIRTUALENVS_PATH: $CI_PROJECT_DIR/.venv
  before_script:
    - apk add curl
    - apk add git
    - curl -sSL https://install.python-poetry.org | POETRY_HOME=$CI_PROJECT_DIR/poetry python3 -
    - export PATH="/builds/GROUP_NAME/REPOSITORY_NAME/poetry/bin:$PATH"
    - poetry config virtualenvs.in-project true
    - poetry install
  script:
    - poetry run pre-commit run --all-files
  artifacts:
    paths:
      - $CI_PROJECT_DIR/.venv
      - $CI_PROJECT_DIR/poetry


quality-checks:
  stage: test
  image: python:3.11-alpine
  dependencies:
    - pre-commit
  before_script:
    - apk add curl
    - apk add bash
    - apk add git
  script:
    - export PATH="/builds/GROUP_NAME/REPOSITORY_NAME/bin:$PATH"
    - source $CI_PROJECT_DIR/.venv/bin/activate
    - poetry run pytest

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文