Skip to content

Commit

Permalink
build: migrate to uv (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryankanno authored Oct 23, 2024
1 parent 5f25016 commit db76f15
Show file tree
Hide file tree
Showing 23 changed files with 1,836 additions and 2,301 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
.idea

# tools
.direnv
.ipynb_checkpoints
.husky
.coverage
Expand All @@ -27,13 +28,15 @@ pytest.ini
.konchrc
.mypy_cache
.pytest_cache
.ruff_cache
commitlint.config.ts
Dockerfile*
docker-compose*
.dockerignore
Makefile
Justfile
tox.ini
.tox

# python
*__pycache__
Expand Down
2 changes: 1 addition & 1 deletion .envrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
layout pyenv 3.11.0
layout_python_uv 3.11.10
17 changes: 14 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on:

env:
PRE_COMMIT_CACHE: ~/.cache/pre-commit
UV_VERSION: 0.4.25

jobs:
tox-precommit:
Expand All @@ -29,10 +30,15 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install uv
uses: astral-sh/setup-uv@v3
with:
version: ${{ env.UV_VERSION }}
enable-cache: true
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install poetry==1.5.1 tox==4.5.2 tox-gh
python -m pip install tox==4.18.1 tox-gh
- name: Cache pre-commit
uses: actions/cache@v4
with:
Expand All @@ -58,15 +64,20 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install uv
uses: astral-sh/setup-uv@v3
with:
version: ${{ env.UV_VERSION }}
enable-cache: true
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install poetry==1.5.1 tox==4.5.2 tox-gh
python -m pip install tox==4.18.1 tox-gh
- name: Cache tox env
uses: actions/cache@v4
with:
path: .tox
key: ${{ matrix.os }}-${{ matrix.python-version }}-tox-${{ hashFiles('pyproject.toml', '.pre-commit-config.yaml') }}
key: ${{ matrix.os }}-${{ matrix.python-version }}-tox-${{ hashFiles('pyproject.toml', 'uv.lock') }}
- name: Set tox env (for tox + codecov)
run: |
MATRIX_PYTHON_VERSION=${{matrix.python-version}}
Expand Down
10 changes: 8 additions & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:

# manual
workflow_dispatch:
UV_VERSION: 0.4.25

env:
PRE_COMMIT_CACHE: ~/.cache/pre-commit
Expand All @@ -31,15 +32,20 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install uv
uses: astral-sh/setup-uv@v3
with:
version: ${{ env.UV_VERSION }}
enable-cache: true
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install poetry==1.5.1 tox==4.11.3 tox-gh
python -m pip install tox==4.18.1 tox-gh
- name: Cache pre-commit
uses: actions/cache@v4
with:
path: ${{ env.PRE_COMMIT_CACHE }}
key: ${{ matrix.os }}-${{ matrix.python-version }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
key: ${{ matrix.os }}-${{ matrix.python-version }}-pre-commit-${{ hashFiles('uv.lock') }}
- name: Run tox
run: |
tox
Expand Down
69 changes: 36 additions & 33 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:

env:
PYTHON_VERSION: 3.11
POETRY_VERSION: 1.5.1
UV_VERSION: 0.4.25

jobs:
build:
Expand All @@ -30,8 +30,11 @@ jobs:
with:
python-version: ${{ env.PYTHON_VERSION }}

- name: Install Poetry ${{ env.POETRY_VERSION }}
run: python -m pip install poetry==${{ env.POETRY_VERSION }}
- name: Install uv
uses: astral-sh/setup-uv@v3
with:
version: ${{ env.UV_VERSION }}
enable-cache: true

- name: Set version with dunamai
uses: mtkennerly/dunamai-action@v1
Expand All @@ -40,15 +43,15 @@ jobs:
command: dunamai from git
args: --no-metadata --style semver

- run: echo ${{ steps.version.outputs.version }}
- run: poetry version ${{ steps.version.outputs.version }}
- name: set version from dunamai
- run: sed -i -e "s/0.0.0/${{ steps.version.outputs.version }}/" pyproject.toml

- name: poetry build
run: poetry build
- name: uv build
run: uv build

- uses: actions/upload-artifact@v4
with:
name: poetry_build
name: uv_build
path: dist/

publish_test_pypi:
Expand All @@ -64,24 +67,23 @@ jobs:
with:
python-version: ${{ env.PYTHON_VERSION }}

- name: Install uv
uses: astral-sh/setup-uv@v3
with:
version: ${{ env.UV_VERSION }}
enable-cache: true

- uses: actions/download-artifact@v4
with:
name: poetry_build
name: uv_build
path: dist/

- name: Install Poetry ${{ env.POETRY_VERSION }}
run: python -m pip install poetry==${{ env.POETRY_VERSION }}

- run: poetry version ${{ needs.build.outputs.version }}

- name: poetry configure TestPyPI Repo
run: poetry config repositories.test-pypi https://test.pypi.org/legacy/

- name: poetry configure TestPyPI Token
run: poetry config pypi-token.test-pypi ${{ secrets.TEST_PYPI_API_TOKEN }}

- name: poetry publish TestPyPi
run: poetry publish -r test-pypi
- name: uv publish TestPyPi
env:
UV_PUBLISH_URL: https://test.pypi.org/legacy/
UV_PUBLISH_TOKEN: ${{ secrets.TEST_PYPI_API_TOKEN }}
run: |
uv publish
publish_pypi:
if: github.repository_owner == 'ryankanno' && github.event_name == 'release' && github.event.action == 'published'
Expand All @@ -96,18 +98,19 @@ jobs:
with:
python-version: ${{ env.PYTHON_VERSION }}

- name: Install uv
uses: astral-sh/setup-uv@v3
with:
version: ${{ env.UV_VERSION }}
enable-cache: true

- uses: actions/download-artifact@v4
with:
name: poetry_build
name: uv_build
path: dist/

- name: Install Poetry ${{ env.POETRY_VERSION }}
run: python -m pip install poetry==${{ env.POETRY_VERSION }}

- run: poetry version ${{ needs.build.outputs.version }}

- name: poetry configure PyPI Token
run: poetry config pypi-token.pypi ${{ secrets.PYPI_API_TOKEN }}

- name: poetry publish PyPi
run: poetry publish
- name: uv publish PyPi
env:
UV_PUBLISH_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
run: |
uv publish
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
repos:
- repo: https://github.com/python-poetry/poetry
rev: 1.8.0
- repo: https://github.com/astral-sh/uv-pre-commit
rev: 0.4.25
hooks:
- id: poetry-check
- id: uv-lock
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
Expand All @@ -20,7 +20,7 @@ repos:
hooks:
- id: ruff
args: ['--config', 'pyproject.toml']
stages: [commit]
stages: [pre-commit]
- id: ruff
stages: [manual]
args: ['--config', 'pyproject.toml', '--fix', '--exit-non-zero-on-fix']
Expand Down
117 changes: 67 additions & 50 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,69 +1,86 @@
# syntax=docker/dockerfile:1.9
FROM python:3.11-slim-buster AS base

ENV PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
PYTHONHASHSEED=random

FROM base as builder
FROM base AS uv

ENV PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
PATH="~/.local/bin:/venv/bin:${PATH}" \
VIRTUAL_ENV="/venv" \
POETRY_VERSION=1.5.1 \
POETRY_VIRTUALENVS_CREATE=false

WORKDIR /app
ENV UV_LINK_MODE=copy \
UV_COMPILE_BYTECODE=1 \
UV_PYTHON_DOWNLOADS=never \
UV_PYTHON=python3.11 \
UV_PROJECT_ENVIRONMENT=/app

SHELL ["/bin/bash", "-exo", "pipefail", "-c"]

RUN apt-get update \
&& apt-get install --no-install-recommends -y curl \
RUN apt-get update -qy \
&& apt-get install -qyy \
-o APT::Install-Recommends=false \
-o APT::Install-Suggests=false \
ca-certificates \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

RUN curl -sSL https://install.python-poetry.org | python3 - --version ${POETRY_VERSION}
RUN python3 -m venv ${VIRTUAL_ENV}
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv

FROM uv AS deps-builder

COPY pyproject.toml /_project/
COPY uv.lock /_project/

RUN --mount=type=cache,target=/root/.cache <<EOT
uv venv
EOT

WORKDIR /_project

# install deps
RUN --mount=type=cache,target=/root/.cache <<EOT
uv sync --locked --no-dev --no-install-project
EOT

FROM uv AS project-builder

COPY --from=deps-builder /app /app

COPY . /src

WORKDIR /src

COPY pyproject.toml poetry.lock ./
RUN poetry install --no-dev --no-interaction --no-ansi --no-root
COPY . ./
RUN poetry install --no-interaction --no-ansi
# install project
RUN --mount=type=cache,target=/root/.cache <<EOT
uv sync --locked --no-dev --no-editable
EOT

FROM base AS final

ARG REPOSITORY=https://github.com/ryankanno/py-golf-games
ARG BUILD_DATETIME
ARG VERSION
ARG REVISION
ARG BRANCH

ENV REPOSITORY=${REPOSITORY}
ENV BUILD_DATETIME=${BUILD_DATETIME:-null}
ENV VERSION=${VERSION:-null}
ENV REVISION=${REVISION:-null}
ENV BRANCH=${BRANCH:-main}

ENV PATH="/venv/bin:${PATH}" \
VIRTUAL_ENV="/venv" \
PYTHONPATH="/app"

LABEL maintainers="Ryan Kanno <[email protected]>"

LABEL org.opencontainers.image.created="${BUILD_DATETIME}" \
org.opencontainers.image.title="py-golf-games" \
org.opencontainers.image.description="Library to help simulate golf games" \
org.opencontainers.image.authors="[email protected]" \
org.opencontainers.image.revision="${REVISION}" \
org.opencontainers.image.source="${REPOSITORY}" \
org.opencontainers.image.version="${VERSION}" \
org.opencontainers.image.licenses="MIT"

COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}
COPY --from=builder /app /app

COPY docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x '/docker-entrypoint.sh'
SHELL ["/bin/bash", "-exo", "pipefail", "-c"]

ENV PATH=/app/bin:$PATH

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

STOPSIGNAL SIGINT

RUN <<EOT
apt-get update -qy
apt-get install -qyy \
-o APT::Install-Recommends=false \
-o APT::Install-Suggests=false
apt-get clean
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
EOT

COPY docker-entrypoint.sh /
RUN chmod +x '/docker-entrypoint.sh'
ENTRYPOINT ["/bin/bash", "/docker-entrypoint.sh"]

COPY --from=project-builder --chown=app:app /app /app

USER app
WORKDIR /app
Loading

0 comments on commit db76f15

Please sign in to comment.