Skip to content

Commit

Permalink
switch to pdm, cache builds better, pin postgres version
Browse files Browse the repository at this point in the history
  • Loading branch information
AbstractUmbra committed Feb 1, 2025
1 parent e758fec commit b982eaa
Show file tree
Hide file tree
Showing 9 changed files with 962 additions and 1,633 deletions.
56 changes: 19 additions & 37 deletions .github/workflows/coverage_and_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- feature/revamp
pull_request:
branches:
- main
Expand All @@ -15,60 +16,41 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.11", "3.x"]
python-version: ["3.12", "3.x"]

name: "Type Coverage and Linting @ ${{ matrix.python-version }}"
steps:
- name: "Checkout Repository"
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: "Load cached poetry installation @ ${{ matrix.python-version }}"
id: cached-poetry
uses: actions/cache@v3
- name: Setup PDM @ ${{ matrix.python-version }}
uses: pdm-project/setup-pdm@v4
with:
path: ~/.local
key: poetry-0
python-version: ${{ matrix.python-version }}
cache: true

- name: "Setup Poetry @ ${{ matrix.python-version }}"
if: steps.cached-poetry.outputs.cache-hit != 'true'
uses: snok/install-poetry@v1
with:
version: latest
virtualenvs-create: true
virtualenvs-in-project: true
virtualenvs-path: .venv

- name: "Setup Python @ ${{ matrix.python-version }}"
id: setup-python
uses: actions/setup-python@v4
with:
python-version: "${{ matrix.python-version }}"
cache: "poetry"

- name: "Load cached venv @ ${{ matrix.python-version }}"
id: cached-pip-wheels
uses: actions/cache@v3
with:
path: .venv/
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}

- name: "Install Python deps @ ${{ matrix.python-version }}"
if: ${{ steps.cached-pip-wheels.outputs.cache-hit != 'true' }}
id: install-deps
- name: Install deps @ ${{ matrix.python-version }}
run: |
poetry install --no-interaction
pdm install --check --no-editable
- name: Activate venv @ ${{ matrix.python-version }}
run: |
echo "$(poetry env info --path)/bin" >> $GITHUB_PATH
echo "$(pdm info --where)/.venv/bin" >> $GITHUB_PATH
- name: "Run Pyright @ ${{ matrix.python-version }}"
uses: jakebailey/pyright-action@v1
uses: jakebailey/pyright-action@v2
with:
warnings: false
no-comments: ${{ matrix.python-version != '3.x' }}
annotate: ${{ matrix.python-version != '3.x' }}

- name: Lint
uses: astral-sh/ruff-action@v3
with:
args: check .

- name: Formatting
uses: astral-sh/ruff-action@v3
with:
args: check .
18 changes: 9 additions & 9 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
name: Deploy

on:
workflow_run:
workflows: ["Create and publish a Docker image"]
branches: [main]
types:
- completed

jobs:
deploy:
name: Deploy bot
Expand All @@ -16,12 +25,3 @@ jobs:
docker compose --profile snekbox pull
docker compose --profile snekbox up --build -d
username: ${{ secrets.SSH_USER }}

name: Deploy

on:
workflow_run:
workflows: ["Create and publish a Docker image"]
branches: [main]
types:
- completed
1 change: 1 addition & 0 deletions .github/workflows/signoff.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: validate-signoff

on:
pull_request:
types:
Expand Down
1 change: 1 addition & 0 deletions .pdm-python
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/home/penumbra/projects/other/pythonista/pythonista-bot/.venv/bin/python
72 changes: 27 additions & 45 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,54 +1,36 @@
FROM python:3.12-slim
ARG PYTHON_BASE=3.12-slim

FROM python:${PYTHON_BASE} AS builder

# disable update check since we're "static" in an image
ENV PDM_CHECK_UPDATE=false
# install PDM
RUN pip install -U pdm

WORKDIR /project

RUN apt-get update -y \
&& apt-get install --no-install-recommends --no-install-suggests -y git \
&& rm -rf /var/lib/apt/lists/*

RUN --mount=type=cache,target=/project/.venv/,sharing=locked \
--mount=type=bind,source=pyproject.toml,target=/project/pyproject.toml,readwrite \
--mount=type=bind,source=pdm.lock,target=/project/pdm.lock,readwrite \
pdm install --check --prod --no-editable && \
cp -R /project/.venv /project/.ready-venv

FROM python:${PYTHON_BASE}

LABEL org.opencontainers.image.source=https://github.com/PythonistaGuild/Pythonista-Bot
LABEL org.opencontainers.image.description="Pythonista Guild's Discord Bot"
LABEL org.opencontainers.image.licenses=MIT

ENV PYTHONUNBUFFERED=1 \
# prevents python creating .pyc files
PYTHONDONTWRITEBYTECODE=1 \
\
# pip
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
\
# poetry
# https://python-poetry.org/docs/configuration/#using-environment-variables
# make poetry install to this location
POETRY_HOME="/opt/poetry" \
# make poetry create the virtual environment in the project's root
# it gets named `.venv`
POETRY_VIRTUALENVS_IN_PROJECT=true \
# do not ask any interactive question
POETRY_NO_INTERACTION=1 \
\
# paths
# this is where our requirements + virtual environment will live
PYSETUP_PATH="/opt/pysetup" \
VENV_PATH="/opt/pysetup/.venv"

ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"

RUN apt-get update \
&& apt-get install --no-install-recommends -y \
git \
# deps for installing poetry
curl \
# deps for building python deps
build-essential \
libcurl4-gnutls-dev \
gnutls-dev \
libmagic-dev

RUN curl -sSL https://install.python-poetry.org | python -

# copy project requirement files here to ensure they will be cached.
USER 1000:1000

WORKDIR /app
COPY poetry.lock pyproject.toml ./

# install runtime deps - uses $POETRY_VIRTUALENVS_IN_PROJECT internally
RUN poetry install --only=main
COPY --from=builder --chown=1000:1000 /project/.ready-venv/ /app/.venv
ENV PATH="/app/.venv/bin:$PATH"

COPY . /app/
ENTRYPOINT ["poetry", "run", "python", "-O", "launcher.py" ]
ENTRYPOINT [ "python", "-O", "launcher.py" ]
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
- database

database:
image: postgres
image: postgres:16
container_name: pythonista-bot-db
env_file:
- .env
Expand Down
Loading

0 comments on commit b982eaa

Please sign in to comment.