Skip to content

Commit

Permalink
Merge pull request #1172 from pycontw/adopt-poetry
Browse files Browse the repository at this point in the history
feat: adopt poetry
  • Loading branch information
mattwang44 authored Apr 13, 2024
2 parents 5bac15d + d540aea commit f825944
Show file tree
Hide file tree
Showing 14 changed files with 1,802 additions and 199 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ jobs:
with:
fetch-depth: 0

- name: Install poetry
run: pipx install poetry

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
cache: 'pip'
cache: 'poetry'

- name: Set up Python dependencies
run: |
pip install -U pip
pip install -r requirements/dev.txt
poetry install
- uses: actions/setup-node@v3
with:
Expand All @@ -51,16 +53,16 @@ jobs:

- name: Run Flake8 on src
run: |
flake8 src
poetry run flake8 src
- name: Run DB migration for test
run: |
python src/manage.py migrate --verbosity 1 --noinput
poetry run python src/manage.py migrate --verbosity 1 --noinput
- name: Run Test
run: |
cd src
pytest -n 2 --cov=. --cov-report=term-missing --cov-report=xml
poetry run pytest -n 2 --cov=. --cov-report=term-missing --cov-report=xml
- name: Run Codecov action
uses: codecov/codecov-action@v4
Expand Down
27 changes: 20 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ RUN yarn install --dev --frozen-lockfile \
# [Python Stage for Django web server]
FROM python:3.10.14-slim-bullseye as python_stage

ENV PYTHONUNBUFFERED 1
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_HOME="/opt/poetry" \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_NO_INTERACTION=1

ENV BASE_DIR /usr/local
ENV APP_DIR $BASE_DIR/app

Expand Down Expand Up @@ -47,17 +54,23 @@ RUN adduser --system --disabled-login docker \
&& mkdir -p "$BASE_DIR" "$APP_DIR" "$APP_DIR/src/assets" "$APP_DIR/src/media" \
&& chown -R docker:nogroup "$BASE_DIR" "$APP_DIR"

USER docker
# Install Poetry
RUN pip install --no-cache-dir pip==23.3.2 && \
pip install --no-cache-dir poetry==1.8.2

# Install Python dependencies (only main dependencies)
COPY --chown=docker:nogroup pyproject.toml poetry.lock ./
RUN poetry install --no-root && \
yes | poetry cache clear --all pypi

# Only copy and install requirements to improve caching between builds
# Install Python dependencies
COPY --chown=docker:nogroup ./requirements $APP_DIR/requirements
RUN pip3 install --user -r "$APP_DIR/requirements/production.txt" \
&& rm -rf $HOME/.cache/pip/*
# Add poetry bin directory to PATH
ENV PATH="${WORKDIR}/.venv/bin:$PATH"

# Finally, copy all the project files along with source files
COPY --chown=docker:nogroup ./ $APP_DIR

USER docker

WORKDIR $APP_DIR/src
VOLUME $APP_DIR/src/media
EXPOSE 8000
Expand Down
25 changes: 17 additions & 8 deletions dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ FROM python:3.10.14-slim-bullseye as python_stage

WORKDIR /app

ENV PYTHONUNBUFFERED 1
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_HOME="/opt/poetry" \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_NO_INTERACTION=1

# Infrastructure tools
# gettext is used for django to compile .po to .mo files.
Expand All @@ -30,14 +36,17 @@ RUN apt-get install -y \
libxml2-dev \
libxslt-dev

# Only copy and install requirements to improve caching between builds
# Install Poetry
RUN pip install --no-cache-dir pip==23.3.2 && \
pip install --no-cache-dir poetry==1.8.2

# Install Python dependencies
COPY ./requirements ./requirements
RUN pip3 install -r ./requirements/dev.txt
COPY pyproject.toml poetry.lock ./
RUN poetry install --no-root && \
yes | poetry cache clear --all pypi

# Add poetry bin directory to PATH
ENV PATH="${WORKDIR}/.venv/bin:$PATH"

COPY --from=node_stage /node_modules ./node_modules
COPY --from=node_stage /usr/local/bin/node /usr/local/bin/node

# for entry point
COPY ./docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
4 changes: 2 additions & 2 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ services:
- DJANGO_SUPERUSER_USERNAME=admin
- DJANGO_SUPERUSER_PASSWORD=1234
- [email protected]
entrypoint: /docker-entrypoint.sh
command: python /app/src/manage.py runserver 0.0.0.0:8000
working_dir: /app/src
command: tail -f /dev/null
8 changes: 0 additions & 8 deletions docker-entrypoint.sh

This file was deleted.

24 changes: 23 additions & 1 deletion document/deploy_docker_dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,30 @@
DATABASE_URL=postgres://postgres:secretpostgres@db:5432/pycontw2016
```
2. Simply run the following command to install all dependencies, activate a containerized Postgres server, and run the Django server (<code>ctrl+c</code> to stop).
2. Simply run the following command to install all dependencies, activate a containerized Postgres server, and enter into a poetry shell inside the application container (<code>ctrl+c</code> to quit).
```
./enter_dev_env.sh
```
3. In the shell, you can run any commands as if you are in a local development environment. Here are some common Django commands:
```sh
# make migrations
python manage.py makemigrations
# apply migrations
python manage.py migrate
# create a superuser
python manage.py createsuperuser
# pull out strings for translations
python manage.py makemessages -l en_US -l zh_Hant
# compile translations
python manage.py compilemessages
# run the dev server (you can access the site at http://localhost:8000/)
python manage.py runserver 0.0.0.0:8000
```
14 changes: 4 additions & 10 deletions document/deploy_local_env_dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,15 @@ Create and start the database for development:

docker-compose -f docker-compose-db.yml up

#### Python - Built-in `venv`
#### Python - Poetry

Create your virtual environment:

python3 -m venv venv
poetry env use 3.10

And enable it:

. venv/bin/activate

#### Python - [virtualenvwrapper](https://virtualenvwrapper.readthedocs.org)

You need to specify your python path when creating the virtual environment:

mkvirtualenv --python=$(which python3) pycontw2016
poetry shell

#### Node.js - [nvm](https://github.com/creationix/nvm)

Expand All @@ -45,7 +39,7 @@ Switch to version specified in `.nvmrc`:

Use pip to install Python depedencies:

pip install -r requirements.txt
poetry install

Use Yarn to install Node dependencies:

Expand Down
5 changes: 2 additions & 3 deletions enter_dev_env.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/bin/bash -xe
CONTAINER="${USER}_pycontw_vm"
COMPOSE_FILE="./docker-compose-dev.yml"
START_SHELL="sh"

# test if the container is running
HASH=`docker ps -q -f name=$CONTAINER`
Expand All @@ -16,14 +15,14 @@ fi
if [ -n "$HASH" ];then
echo "found existing running container $CONTAINER, proceeding to exec another shell"
docker-compose -f $COMPOSE_FILE restart
docker exec -it $HASH $START_SHELL
docker exec -w /app/src -it $HASH bash -c "SHELL=bash poetry shell"
elif [ -n "$HASH_STOPPED" ];then
echo "found existing stopped container $CONTAINER, starting"
docker-compose -f $COMPOSE_FILE restart
docker start --attach -i $HASH_STOPPED
else
echo "existing container not found, creating a new one, named $CONTAINER"
docker-compose -f $COMPOSE_FILE pull
docker-compose -f $COMPOSE_FILE run -p 8000:8000 --name=$CONTAINER pycontw
docker-compose -f $COMPOSE_FILE run -p 8000:8000 --name=$CONTAINER pycontw bash -c "SHELL=bash poetry shell"
fi
echo "see you, use 'docker rm $CONTAINER' to kill the dev container or 'docker-compose -f $COMPOSE_FILE down' to kill both the postgres and the dev container if you want a fresh env next time"
Loading

0 comments on commit f825944

Please sign in to comment.