如何避免在 .gitlab-ci.yml 的每个阶段之前安装 requirements.txt?

How to avoid installing requirements.txt before every stage in .gitlab-ci.yml?

我有一个 .gitlab-ci.yml 看起来像这样:

image: "python:3.7"

before_script:
  - pip install -r requirements.txt

stages:
  - stageA
  - stageB

stage_a:
  stage: stageA
  script:
  - run_some_python_scripts

stage_b:
  stage: stageB
  script:
  - run_more_python_scripts

使用此设置,requirements.txt 在每个阶段之前安装。 我只需要安装一次,这样 stageAstageB 都可以使用。

我怎样才能做到这一点?

我发现如果 requirements.txt 文件变化不大,我发现一个非常有效的选项是将其烘焙到您自己的 Docker 图像中。

我个人不太喜欢的另一个选项是使用 virtualenv,然后在 virtualenv 上使用 GitLab 的 cache,但是这可能有点慢如果有很多 pip 个包。