Skip to content

Commit

Permalink
Use uv
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaFicarelli committed Oct 10, 2024
1 parent bec330e commit d96ec6b
Show file tree
Hide file tree
Showing 8 changed files with 1,004 additions and 1,078 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v3
with:
version: "0.4.20"
enable-cache: true
- name: Setup python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install dependencies
run: pip install --no-cache-dir pdm
python-version-file: "pyproject.toml"
- name: Check requirements
run: make check-deps
- name: Install requirements
Expand All @@ -31,3 +34,4 @@ jobs:
run: |
make clean
docker system prune --force --volumes
uv cache prune --ci
67 changes: 53 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,32 +1,65 @@
ARG PYTHON_BASE=3.12-slim
# syntax=docker/dockerfile:1.9
ARG UV_VERSION=0.4.20
ARG PYTHON_VERSION=3.12
ARG PYTHON_BASE=${PYTHON_VERSION}-slim

# uv stage
FROM ghcr.io/astral-sh/uv:${UV_VERSION} as uv

# build stage
FROM python:$PYTHON_BASE AS builder
SHELL ["/bin/bash", "-e", "-o", "pipefail", "-c"]
ARG ENVIRONMENT
ARG PDM_VERSION=2.17.3
ENV \
PDM_CHECK_UPDATE=false \
PDM_NO_EDITABLE=true \
PDM_NO_SELF=true
SHELL ["bash", "-e", "-x", "-o", "pipefail", "-c"]

RUN <<EOT
apt-get update -qy
apt-get install -qyy \
-o APT::Install-Recommends=false \
-o APT::Install-Suggests=false \
build-essential \
ca-certificates
EOT

COPY --from=uv /uv /usr/local/bin/uv

ENV UV_LINK_MODE=copy \
UV_COMPILE_BYTECODE=1 \
UV_PYTHON_DOWNLOADS=never \
UV_PYTHON=python${PYTHON_VERSION}

WORKDIR /code
RUN pip install --no-cache-dir pdm==${PDM_VERSION}
COPY pyproject.toml pdm.lock ./
RUN \
if ! [[ "${ENVIRONMENT}" =~ ^(dev|prod)$ ]]; then echo "Invalid ENVIRONMENT"; exit 1; fi && \
pdm venv create --with-pip && pdm install --check --${ENVIRONMENT}
ARG ENVIRONMENT
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 <<EOT
if [ "${ENVIRONMENT}" = "prod" ]; then
uv sync --locked --no-install-project --no-dev
elif [ "${ENVIRONMENT}" = "dev" ]; then
uv sync --locked --no-install-project
else
echo "Invalid ENVIRONMENT"; exit 1
fi
EOT

# run stage
FROM python:$PYTHON_BASE
RUN useradd -ms /bin/sh -u 1001 app
SHELL ["bash", "-e", "-x", "-o", "pipefail", "-c"]

RUN <<EOT
groupadd -r app
useradd -r -d /code -g app -N app
EOT

USER app
WORKDIR /code
ENV PATH="/code/.venv/bin:$PATH"
ENV PYTHONPATH="/code:$PYTHONPATH"
COPY --chown=app:app --from=builder /code/.venv/ .venv/
COPY --chown=app:app alembic.ini docker-cmd.sh pyproject.toml ./
COPY --chown=app:app alembic/ alembic/
COPY --chown=app:app app/ app/

RUN python -m compileall . # compile app files

ARG ENVIRONMENT
ARG APP_NAME
ARG APP_VERSION
Expand All @@ -36,5 +69,11 @@ ENV APP_NAME=${APP_NAME}
ENV APP_VERSION=${APP_VERSION}
ENV COMMIT_SHA=${COMMIT_SHA}

RUN <<EOT
python -V
python -m site
python -c 'import app'
EOT

STOPSIGNAL SIGINT
CMD ["./docker-cmd.sh"]
26 changes: 13 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ help: ## Show this help
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-23s\033[0m %s\n", $$1, $$2}'

install: ## Install dependencies into .venv
pdm install --no-self
uv sync --no-install-project

compile-deps: ## Create or update the lock file, without upgrading the version of the dependencies
pdm lock --update-reuse
uv lock

upgrade-deps: ## Create or update the lock file, using the latest version of the dependencies
pdm lock
uv lock --upgrade

check-deps: ## Check that the dependencies in the existing lock file are valid
pdm lock --check
uv lock --locked

format: # Run formatters
pdm run python -m ruff format
pdm run python -m ruff check --fix
uv run -m ruff format
uv run -m ruff check --fix

lint: ## Run linters
pdm run python -m ruff format --check
pdm run python -m ruff check
pdm run python -m mypy app
uv run -m ruff format --check
uv run -m ruff check
uv run -m mypy app

build: ## Build the Docker image
docker compose build app
Expand Down Expand Up @@ -58,15 +58,15 @@ test-local: export AWS_DEFAULT_REGION=us-east-1
test-local: export AWS_ENDPOINT_URL=http://127.0.0.1:9324
test-local: ## Run tests locally
docker compose up --wait db-test
pdm run python -m alembic upgrade head
pdm run python -m pytest
uv run -m alembic upgrade head
uv run -m pytest

migration: export DB_HOST=127.0.0.1
migration: export DB_PORT=5433
migration: ## Create the alembic migration
docker compose up --wait db
pdm run python -m alembic upgrade head
pdm run python -m alembic revision --autogenerate
uv run -m alembic upgrade head
uv run -m alembic revision --autogenerate

show-config: export COMPOSE_PROFILES=run,test
show-config: ## Show the docker-compose configuration in the current environment
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This service is composed by:
Requirements:

- [Docker compose](https://docs.docker.com/compose/) >= 2.24.4
- [PDM](https://pdm-project.org/)
- [uv](https://docs.astral.sh/uv/)

Valid `make` targets:

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ services:
path: ./app
target: /code/app
- action: rebuild
path: pdm.lock
path: uv.lock
depends_on:
db:
condition: service_healthy
Expand Down
Loading

0 comments on commit d96ec6b

Please sign in to comment.