Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to uv from poetry #603

Merged
merged 1 commit into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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