Skip to content

Commit ace1c31

Browse files
committed
move to pdm, change to docker secrets over env, change builds to use pdm and multistage
1 parent 19e8d89 commit ace1c31

11 files changed

+1121
-1640
lines changed

.dockerignore

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
__pycache__/
22
*.py[cod]
3-
_pyright/
3+
**/*.ipynb
4+
45
.git/
56
.github/
67
.venv/
78
.vscode/
89
.idea/
9-
logs/
10+
.ruff_cache/
1011
.gitignore
11-
.gitmodules
12-
**/*.ipynb
12+
.gitmodule
13+
14+
logs/
1315
docker-compose.yml
1416
Dockerfile
1517
LICENSE

.github/workflows/coverage_and_lint.yml

+10-36
Original file line numberDiff line numberDiff line change
@@ -24,51 +24,25 @@ jobs:
2424
with:
2525
fetch-depth: 0
2626

27-
- name: "Load cached poetry installation @ ${{ matrix.python-version }}"
28-
id: cached-poetry
29-
uses: actions/cache@v3
27+
- name: Setup PDM @ ${{ matrix.python-version }}
28+
uses: pdm-project/setup-pdm@v4
3029
with:
31-
path: ~/.local
32-
key: poetry-0
30+
python-version: ${{ matrix.python-version }}
31+
cache: true
3332

34-
- name: "Setup Poetry @ ${{ matrix.python-version }}"
35-
if: steps.cached-poetry.outputs.cache-hit != 'true'
36-
uses: snok/install-poetry@v1
37-
with:
38-
version: latest
39-
virtualenvs-create: true
40-
virtualenvs-in-project: true
41-
virtualenvs-path: .venv
42-
43-
- name: "Setup Python @ ${{ matrix.python-version }}"
44-
id: setup-python
45-
uses: actions/setup-python@v4
46-
with:
47-
python-version: "${{ matrix.python-version }}"
48-
cache: "poetry"
49-
50-
- name: "Load cached venv @ ${{ matrix.python-version }}"
51-
id: cached-pip-wheels
52-
uses: actions/cache@v3
53-
with:
54-
path: .venv/
55-
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
56-
57-
- name: "Install Python deps @ ${{ matrix.python-version }}"
58-
if: ${{ steps.cached-pip-wheels.outputs.cache-hit != 'true' }}
59-
id: install-deps
33+
- name: Install deps @ ${{ matrix.python-version }}
6034
run: |
61-
poetry install --no-interaction
35+
pdm install --check --no-editable
6236
6337
- name: Activate venv @ ${{ matrix.python-version }}
6438
run: |
65-
echo "$(poetry env info --path)/bin" >> $GITHUB_PATH
39+
echo "$(pdm info --where)/.venv/bin" >> $GITHUB_PATH
6640
6741
- name: "Run Pyright @ ${{ matrix.python-version }}"
68-
uses: jakebailey/pyright-action@v1
42+
uses: jakebailey/pyright-action@v2
6943
with:
7044
warnings: false
71-
no-comments: ${{ matrix.python-version != '3.x' }}
45+
annotate: ${{ matrix.python-version != '3.x' }}
7246

7347
- name: Lint
74-
uses: astral-sh/ruff-action@v3
48+
uses: astral-sh/ruff-action@v2

.gitignore

+5-3
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ venv.bak/
113113
# Visual Studio Code settings
114114
.vscode/
115115

116+
# Intellij settings
117+
.idea/
118+
116119
# Spyder project settings
117120
.spyderproject
118121
.spyproject
@@ -133,6 +136,5 @@ dmypy.json
133136

134137
# config
135138
config.toml
136-
137-
.vscode/
138-
.idea/
139+
postgres_password.txt
140+
.pdm-python

.template.env

-3
This file was deleted.

Dockerfile

+22-46
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,30 @@
1-
FROM python:3.12-slim
1+
ARG PYTHON_BASE=3.11-slim
2+
3+
FROM python:${PYTHON_BASE} AS builder
4+
5+
# disable update check since we're "static" in an image
6+
ENV PDM_CHECK_UPDATE=false
7+
# install PDM
8+
RUN pip install -U pdm
9+
10+
WORKDIR /project
11+
COPY . /project/
12+
RUN apt-get update -y \
13+
&& apt-get install --no-install-recommends --no-install-suggests -y git \
14+
&& rm -rf /var/lib/apt/lists/*
15+
16+
RUN pdm install --check --prod --no-editable
17+
18+
FROM python:${PYTHON_BASE}
219

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

7-
ENV PYTHONUNBUFFERED=1 \
8-
# prevents python creating .pyc files
9-
PYTHONDONTWRITEBYTECODE=1 \
10-
\
11-
# pip
12-
PIP_NO_CACHE_DIR=off \
13-
PIP_DISABLE_PIP_VERSION_CHECK=on \
14-
PIP_DEFAULT_TIMEOUT=100 \
15-
\
16-
# poetry
17-
# https://python-poetry.org/docs/configuration/#using-environment-variables
18-
# make poetry install to this location
19-
POETRY_HOME="/opt/poetry" \
20-
# make poetry create the virtual environment in the project's root
21-
# it gets named `.venv`
22-
POETRY_VIRTUALENVS_IN_PROJECT=true \
23-
# do not ask any interactive question
24-
POETRY_NO_INTERACTION=1 \
25-
\
26-
# paths
27-
# this is where our requirements + virtual environment will live
28-
PYSETUP_PATH="/opt/pysetup" \
29-
VENV_PATH="/opt/pysetup/.venv"
30-
31-
ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"
32-
33-
RUN apt-get update \
34-
&& apt-get install --no-install-recommends -y \
35-
git \
36-
# deps for installing poetry
37-
curl \
38-
# deps for building python deps
39-
build-essential \
40-
libcurl4-gnutls-dev \
41-
gnutls-dev \
42-
libmagic-dev
43-
44-
RUN curl -sSL https://install.python-poetry.org | python -
45-
46-
# copy project requirement files here to ensure they will be cached.
4724
WORKDIR /app
48-
COPY poetry.lock pyproject.toml ./
49-
50-
# install runtime deps - uses $POETRY_VIRTUALENVS_IN_PROJECT internally
51-
RUN poetry install --only=main
5225

26+
COPY --from=builder /project/.venv/ /app/.venv
27+
ENV PATH="/app/.venv/bin:$PATH"
5328
COPY . /app/
54-
ENTRYPOINT ["poetry", "run", "python", "-O", "launcher.py" ]
29+
30+
ENTRYPOINT [ "python", "-O", "bot.py" ]

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ python -O launcher.py
2828
We also provide both `Dockerfile` and `docker-compose.yml` files for running the bot in Docker.
2929
The default compose file assumes you want snekbox too, however you can comment out or remove that entire key if this is not the case.
3030

31-
Make a copy of `.template.env` and name it to `.env`, edit the values as you see fit (the defaults will work too, and be okay so long as you don't expose this database to the internet).
31+
Make a copy of `postgres_password.example.txt` and name it to `postgres_password.txt`, edit the text within the file as you see fit.
3232
Then a simple `docker compose up` will bring your bot up!
3333

3434
NOTE: If you want to enable the snekbox service for sandboxed open eval, do:
@@ -43,7 +43,7 @@ which will enable all services and snekbox.
4343

4444
This also means that it will use internal docker networking to resolve the database and snekbox names. By default these will be `database` and `snekbox`, but these will be the **service names** in `docker-compose.yml` if you change them. Please keep this in mind when editing your config file.
4545

46-
We also provide a file for ignoring git blame references. You can enable this with:-
46+
We also provide a file for ignoring git blame references (for mass edits like formatting tools and so on). You can enable this with:-
4747
```sh
4848
git config blame.ignoreRevsFile .git-blame-ignore-revs
4949
```

docker-compose.yml

+9-5
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ services:
1111
database:
1212
image: postgres
1313
container_name: pythonista-bot-db
14-
env_file:
15-
- .env
1614
environment:
17-
- POSTGRES_USER=${POSTGRES_USER}
18-
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
19-
- POSTGRES_DB=${POSTGRES_DB}
15+
- POSTGRES_USER=pythonistabot
16+
- POSTGRES_PASSWORD_FILE=/run/secrets/postgres-password
17+
- POSTGRES_DB=pythonistabot
2018
healthcheck:
2119
interval: 1s
2220
retries: 10
2321
test: ["CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}"]
2422
timeout: 5s
2523
restart: always
24+
secrets:
25+
- postgres-password
2626
volumes:
2727
- pgdata:/var/lib/postgresql/data
2828
- ./database/schema.sql:/docker-entrypoint-initdb.d/schema.sql:ro
@@ -38,5 +38,9 @@ services:
3838
- "snekbox"
3939
restart: always
4040

41+
secrets:
42+
postgres-password:
43+
file: ./postgres-password.txt
44+
4145
volumes:
4246
pgdata:

0 commit comments

Comments
 (0)