[Python] GitLab CI pipeline for a uv Python project does not cache environment and dependencies

Discussão em 'Python' iniciado por Stack, Junho 3, 2025.

  1. Stack

    Stack Membro Participativo

    I have a GitLab runner on a Linux machine that uses the shell executor. This runner is for testing a Python project that uses uv for virtual environment and dependency management. I have the Python project setup to use the runner with the .gitlab-ci.yml file shown below. I followed the caching instructions in the uv docs for GitLab CI but nothing seems to get cached. Every time the pipeline runs, the cache and virtual environment are removed and then created again (see log below). The project has a lot of dependencies that can take several minutes to download. How can I reuse the uv cache and virtual environment with GitLab CI so the dependencies are not downloaded every time a job runs?

    # GitLab CI for custom runner instance with shell executor
    # Requires uv installed for gitlab-runner user

    stages:
    - check
    - test

    variables:
    UV_CACHE_DIR: .uv-cache

    cache:
    - key:
    files:
    - uv.lock
    paths:
    - $UV_CACHE_DIR

    checks:
    stage: check
    script:
    - uv sync
    - uv run ruff check .
    - uv run ruff format --check .
    - uv cache prune --ci

    tests:
    stage: test
    script:
    - uv sync
    - uv run pytest
    - uv cache prune --ci


    Fetching changes with git depth set to 20...
    Reinitialized existing Git repository in /home/cloud/builds/t2_jESXhP/0/ai4ops/risk-analysis/.git/
    Checking out 94d41e64 as detached HEAD (ref is fix-checks)...
    Removing .uv-cache/
    Removing .venv/
    Skipping Git submodules setup

    Restoring cache
    00:00
    Checking cache for 0_uv-8be8d005e56e2d62177fb81b4081e3dc73657342-non_protected...
    Runtime platform arch=amd64 os=linux pid=184162 revision=4d7093e1 version=18.0.2
    No URL provided, cache will not be downloaded from shared cache server. Instead a local version of cache will be extracted.
    WARNING: Cache file does not exist
    Failed to extract cache

    Executing "step_script" stage of the job script
    00:42
    $ uv sync
    Using CPython 3.12.10
    Creating virtual environment at: .venv
    Resolved 215 packages in 1ms
    Downloading pygments (1.2MiB)
    Downloading setuptools (1.1MiB)
    Downloading numpy (15.8MiB)
    Downloading tokenizers (2.9MiB)
    Downloading kubernetes (1.9MiB)
    Downloading pymupdf (19.1MiB)
    Downloading llama-index-core (7.3MiB)
    Downloading sympy (5.9MiB)
    Downloading nvidia-cusparselt-cu12 (143.1MiB)
    Downloading tiktoken (1.1MiB)
    Downloading sqlalchemy (3.2MiB)
    Downloading streamlit (9.4MiB)
    Downloading nltk (1.4MiB)
    Downloading hf-xet (4.9MiB)
    Downloading grpcio (5.6MiB)
    Downloading nvidia-cublas-cu12 (346.6MiB)

    Continue reading...

Compartilhe esta Página