CI/CD-Pipelines

GitLab CI/CD

Für GitLab CI/CD-Pipelines gibt es verschiedene Docker-Images mit vorinstalliertem uv: Available images.

.gitlab-ci.yml
 1variables:
 2  UV_VERSION: 0.11
 3  PYTHON_VERSION: 3.14
 4  BASE_LAYER: trixie-slim
 5  # GitLab CI creates a separate mountpoint for the build directory,
 6  # so we need to copy instead of using hard links.
 7  UV_LINK_MODE: copy
 8
 9stages:
10  - build
11
12uv-install:
13  stage: build
14  image: ghcr.io/astral-sh/uv:$UV_VERSION-python$PYTHON_VERSION-$BASE_LAYER
15  variables:
16    UV_CACHE_DIR: .uv-cache
17  cache:
18    - key:
19        files:
20          - uv.lock
21      paths:
22        - $UV_CACHE_DIR
23  script:
24    # YOUR UV COMMANDS
25    - uv cache prune --ci
Zeile 25

Dies reduziert die Cache-Größe, s.a. Caching in continuous integration.

GitHub Actions

Die offizielle astral-sh/setup-uv-GitHub Action installiert uv, fügt es PATH hinzu und stellt einen Cache für die installierten Pakete bereit:

ci.yml
name: ci

jobs:
  test:
    name: python
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
      with:
        persist-credentials: false

    - name: Setup cached uv
      uses: hynek/setup-cached-uv@4300ec2180bc77d705e626a34e381b81a4772c51 # v2.5.0

Anschließend könnt ihr entweder eine einzelne Python-Version oder eine Matrix mit uv installieren:

ci.yml
- uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0
  with:
    python-version-file: .python-version

oder

ci.yml
name: ci

strategy:
  matrix:
    python-version:
      - "3.10"
      - "3.11"
      - "3.12"
      - "3.13"
      - "3.14"

steps:
  - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
  - name: Install uv and set the Python version
    uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0
    with:
      python-version: ${{ matrix.python-version }}

uv sync und uv run

Sobald uv und Python installiert sind, kann das Projekt mit uv sync installiert werden und Befehle können in der Umgebung mit uv run ausgeführt werden, z.B. für pytest:

ci.yml
- name: Install the project
  run: uv sync --group tests

- name: Run tests
  run: uv run pytest

Caching

Der Cache von uv verbessert die Laufzeiten:

ci.yml
- name: Enable caching
  uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0
  with:
    enable-cache: true

Macht den Cache ungültig, wenn sich uv.lock ändert:

ci.yml
- name: Define a cache dependency glob
  uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0

  with:
    enable-cache: true
    cache-dependency-glob: "uv.lock"