Skip to content

Commit

Permalink
Migrate to uv from poetry
Browse files Browse the repository at this point in the history
  • Loading branch information
ulgens committed Jan 19, 2025
1 parent a36be0d commit 0b0c009
Show file tree
Hide file tree
Showing 5 changed files with 1,326 additions and 2,049 deletions.
45 changes: 14 additions & 31 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
# Things to check before start:
# * https://github.com/marketplace/actions/python-poetry-action
# * https://github.com/packetcoders/action-setup-cache-python-poetry/blob/main/action.yml
# * https://gist.github.com/soof-golan/6ebb97a792ccd87816c0bda1e6e8b8c2
# * https://github.com/nicoloboschi/poetry-dockerize-plugin
# * https://github.com/wemake-services/wemake-django-template/blob/master/%7B%7Bcookiecutter.project_name%7D%7D/docker/django/Dockerfile
# * https://github.com/orgs/python-poetry/discussions/1879
# Adapted from https://docs.astral.sh/uv/guides/integration/github/#using-uv-in-github-actions

name: "tests"

Expand All @@ -17,7 +11,7 @@ on:

env:
PYTHON_VERSION: 3.13.1
POETRY_VERSION: 2.0.1
UV_VERSION: 0.5.21

jobs:
run-django-tests:
Expand All @@ -41,30 +35,19 @@ jobs:
- uses: actions/checkout@v4.2.2
name: Checkout

- uses: actions/setup-python@v5.3.0
id: setup_python
name: Set up Python ${{ env.PYTHON_VERSION }}
- name: Install uv ${{ env.UV_VERSION }} & Python ${{ env.PYTHON_VERSION }}
uses: astral-sh/setup-uv@v5
with:
# The default value is "auto" and it resolves to "true" in Github-hosted runners, but explicit is better.
enable-cache: true
# uv can install Python, no need for actions/setup-python
# https://github.com/astral-sh/setup-uv?tab=readme-ov-file#do-i-still-need-actionssetup-python-alongside-setup-uv
# https://github.com/astral-sh/setup-uv?tab=readme-ov-file#python-version
python-version: ${{ env.PYTHON_VERSION }}
version: ${{ env.UV_VERSION }}

- uses: actions/cache@v4.1.2
name: Cache for Poetry setup
id: poetry_install_cache
with:
key: poetry_install-${{ runner.os }}-${{ env.PYTHON_VERSION }}-poetry-${{ env.POETRY_VERSION }}
path: ${{ env.pythonLocation }}
restore-keys: |
poetry_install-${{ runner.os }}-${{ env.PYTHON_VERSION }}-poetry-
- name: Setup Poetry
if: steps.poetry_install_cache.outputs.cache-hit != 'true'
run: pip install poetry==${{ env.POETRY_VERSION }}

# TODO:
# This step must be cached too; but with current small list of packages,
# installing them is faster than cache retrieval and update.
- name: Install packages
run: poetry install
run: uv sync --frozen --no-install-project

- name: Run tests
working-directory: ./src
Expand All @@ -76,6 +59,6 @@ jobs:
SECRET_KEY: ""
USE_SSL: "false"
run: |
poetry run python manage.py wait_for_db
poetry run python manage.py wait_for_cache
poetry run python manage.py test --parallel --shuffle
uv run python manage.py wait_for_db
uv run python manage.py wait_for_cache
uv run python manage.py test --parallel --shuffle
42 changes: 25 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Adapted from
# * https://github.com/astral-sh/uv-docker-example/blob/main/Dockerfile
# * https://github.com/wemake-services/wemake-django-template/blob/master/%7B%7Bcookiecutter.project_name%7D%7D/docker/django/Dockerfile

FROM python:3.13.1-slim-bookworm

RUN --mount=type=cache,target=/var/cache/apt --mount=type=cache,target=/var/lib/apt \
Expand All @@ -14,24 +18,28 @@ RUN --mount=type=cache,target=/var/cache/apt --mount=type=cache,target=/var/lib/
ENV PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
PYTHONHASHSEED=random \
PYTHONDONTWRITEBYTECODE=True \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100

# Poetry settings
ENV POETRY_VERSION="2.0.1" \
# # When true, `poetry run` is required to run the commands relating to the venv
POETRY_VIRTUALENVS_CREATE=0 \
POETRY_NO_INTERACTION=1 \
POETRY_NO_ANSI=1
PYTHONDONTWRITEBYTECODE=True

RUN --mount=type=cache,target=/root/.cache/pip \
pip install "poetry==$POETRY_VERSION"
# uv settings
# https://github.com/astral-sh/uv-docker-example/blob/main/pyproject.toml
# https://hynek.me/articles/docker-uv/

RUN mkdir /code
WORKDIR /code
# Enable bytecode compilation
ENV UV_COMPILE_BYTECODE=1
# Silence uv complaining about not being able to use hard links.
ENV UV_LINK_MODE=copy
# https://github.com/astral-sh/uv/pull/6834#issuecomment-2319253359
ENV UV_PROJECT_ENVIRONMENT="/usr/local/"
ENV UV_PYTHON_PREFERENCE=system

COPY poetry.lock pyproject.toml /code/
# https://docs.astral.sh/uv/guides/integration/docker/#installing-uv
# uv version can not be defined in an environment variable,
# because COPY --from doesn't support variable expansion
# https://github.com/moby/moby/issues/34482
COPY --from=ghcr.io/astral-sh/uv:0.5.21 /uv /uvx /bin/

# TODO: Share the venv folder with the host.
RUN poetry install
# https://github.com/astral-sh/uv-docker-example/blob/a14ebc89e3a5e5b33131284968d8969ae054ed0d/Dockerfile#L13
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --frozen --no-install-project
Loading

0 comments on commit 0b0c009

Please sign in to comment.