Skip to content

Commit

Permalink
build(dockerfile): add pypy variant
Browse files Browse the repository at this point in the history
ci: add `python 3.10` to tests matrix
ci: remove `graalpy-23.1` from tests matrix
ci(deps): downgrade `codecov/codecov-action` github actions to `v3.1.5`
fix: support `HEAD` method for `/health` route
  • Loading branch information
DeadNews committed Feb 6, 2024
1 parent 7305d03 commit 30294c7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.11", "3.12"]
python-version: ["3.10", "3.11", "3.12"]
include:
- os: ubuntu-latest
python-version: pypy3.10
- os: ubuntu-latest
python-version: graalpy-23.1
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
Expand All @@ -62,8 +60,8 @@ jobs:
run: poetry run poe test

- name: Upload coverage to Codecov
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.11'
uses: codecov/codecov-action@ab904c41d6ece82784817410c45d8b8c02684457 # v3.1.6
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.12'
uses: codecov/codecov-action@4fe8c5f003fae66aa5ebb77cfd3e7bfbbda0b6b0 # v3.1.5

publish-pypi:
name: Release to PyPI
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ ENV UVICORN_PORT=8000 \
PATH="/app/.venv/bin/:$PATH"

COPY --from=py-builder /app/.venv /app/.venv
COPY --from=py-builder /app/dist /app/
COPY --from=py-builder /app/dist/*.whl /app/

RUN pip install /app/*.whl

Expand Down
55 changes: 55 additions & 0 deletions Dockerfile.pypy
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
FROM pypy:3.10-slim@sha256:e074587f4ae78a735969c2fdedeacf2905c6116e8daefddabe18726ffc273327 as base
LABEL maintainer "DeadNews <[email protected]>"

ENV PIP_DEFAULT_TIMEOUT=100 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
# Avoid to write .pyc files.
PYTHONDONTWRITEBYTECODE=1 \
# Allow messages to immediately appear.
PYTHONUNBUFFERED=1 \
# Tracebacks on segfaults.
PYTHONFAULTHANDLER=1

WORKDIR /app

FROM base as py-builder

# renovate: datasource=pypi dep_name=poetry
ENV POETRY_VERSION="1.7.1"
ENV POETRY_VIRTUALENVS_IN_PROJECT=1 \
# Disable the dynamic versioning.
POETRY_DYNAMIC_VERSIONING_COMMANDS="" \
# Maunt as dedicated RUN cache.
POETRY_CACHE_DIR="/cache/poetry" \
PIP_CACHE_DIR="/cache/pip"

RUN --mount=type=cache,target=${PIP_CACHE_DIR} \
pip install "poetry==${POETRY_VERSION}"

# hadolint ignore=DL3008
RUN --mount=type=cache,target="/var/cache/" \
--mount=type=cache,target="/var/lib/apt/lists/" \
apt-get update && apt-get install --no-install-recommends -y gcc

COPY pyproject.toml poetry.lock README.md src ./
RUN --mount=type=cache,target=${POETRY_CACHE_DIR} \
poetry install --only=main --no-root && \
poetry build

FROM base as runtime

ENV UVICORN_PORT=8000 \
UVICORN_HOST=0.0.0.0 \
PATH="/app/.venv/bin/:$PATH"

COPY --from=py-builder /app/.venv /app/.venv
COPY --from=py-builder /app/dist/*.whl /app/

RUN pip install /app/*.whl

USER nobody:nogroup
EXPOSE ${UVICORN_PORT}
HEALTHCHECK --interval=60s --timeout=3s \
CMD wget --no-verbose --tries=1 --spider http://127.0.0.1:${UVICORN_PORT}/health || exit 1

CMD [ "python", "-m", "uvicorn", "deadnews_template_python.app:app" ]
2 changes: 1 addition & 1 deletion src/deadnews_template_python/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def read_root() -> dict[str, str]:
return HELLO_WORLD


@app.get("/health")
@app.api_route("/health", methods=["GET", "HEAD"])
def read_health() -> dict[str, str]:
"""Handles the "/health" route and returns a JSON response."""
return HEALTH
Expand Down

0 comments on commit 30294c7

Please sign in to comment.