如何在GitLab CI中不为每个作业安装Python依赖。

huangapple go评论65阅读模式
英文:

How not to install python dependencies for each job in gitlab CI

问题

我可以使用 before_script 来在每个作业之前 pip install... 所有项目需要的内容,但这意味着依赖项在每个作业独立安装。这会增加额外的时间,特别是在连续作业的情况下。是否有任何常见的方法?

英文:

I can use before_script to pip install... all the project needs but it means dependencies are installed before each job independently. This adds additional time, especially in case of sequential jobs.

Is there any common approach?

答案1

得分: 2

GitLab CI支持在作业之间缓存依赖项:

image: python:latest

# 将pip的缓存目录更改为项目目录内,因为我们只能缓存本地项目。
variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

# Pip的缓存不会存储Python包
# https://pip.pypa.io/en/stable/topics/caching/
#
# 如果你想同时缓存安装的包,你必须在virtualenv中安装它们并将其缓存。
cache:
  paths:
    - .cache/pip
    - venv/

before_script:
  - python --version  # 用于调试
  - pip install virtualenv
  - virtualenv venv
  - source venv/bin/activate

test:
  script:
    - python setup.py test
    - pip install tox flake8  # 你也可以使用tox
    - tox -e py,flake8

注意 在Python中缓存依赖项可能有点奇怪,所以确保仔细查阅文档。

英文:

GitLab CI has support for cacheing dependencies across jobs:

image: python:latest

# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

# Pip's cache doesn't store the python packages
# https://pip.pypa.io/en/stable/topics/caching/
#
# If you want to also cache the installed packages, you have to install
# them in a virtualenv and cache it as well.
cache:
  paths:
    - .cache/pip
    - venv/

before_script:
  - python --version  # For debugging
  - pip install virtualenv
  - virtualenv venv
  - source venv/bin/activate

test:
  script:
    - python setup.py test
    - pip install tox flake8  # you can also use tox
    - tox -e py,flake8

note cacheing deps in python can be a little weird so def double check the docs.

Refs:

https://docs.gitlab.com/ee/ci/caching/#cache-python-dependencies

https://pip.pypa.io/en/stable/topics/caching/

huangapple
  • 本文由 发表于 2023年4月17日 01:04:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定