Skip to content

Commit

Permalink
Huge tooling update (#195)
Browse files Browse the repository at this point in the history
* Poetry, ruff & mypy
* Dropped test suite, hoping mypy will do its job
* Migrated to async (>20) PTB
* New Github-Actions based CI
  • Loading branch information
f213 authored Jan 13, 2024
1 parent 0c9adaf commit 5e1ee4d
Show file tree
Hide file tree
Showing 42 changed files with 1,967 additions and 1,160 deletions.
101 changes: 0 additions & 101 deletions .circleci/config.yml

This file was deleted.

2 changes: 0 additions & 2 deletions .circleci/known_hosts

This file was deleted.

2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
.git
11 changes: 3 additions & 8 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@
[*.py]
indent_size = 4

[*.json]
indent_style = space
indent_size = 2

[*.coffee]
indent_style = space
indent_size = 2

[*.yaml]
indent_size = 2

[{Makefile,**.mk}]
indent_style = tab
116 changes: 116 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
name: CI


on:
push:
branches:
- master
pull_request:

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install poetry
uses: snok/install-poetry@v1
with:
version: 1.6.1

- name: Install python
id: setup-python
uses: actions/setup-python@v4
with:
cache: 'poetry'
python-version-file: 'pyproject.toml'

- name: make sure poetry lockfile is up to date
run: poetry check --lock && echo Lockfile is ok, $(poetry --version)
shell: bash

- name: install deps
if: steps.setup-python.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root

- name: Run the linters
run: make lint

build-images:
needs: lint
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install python
id: setup-python
uses: actions/setup-python@v4
with:
python-version-file: 'pyproject.toml'

- name: Set up qemu
uses: docker/setup-qemu-action@v2

- name: Set up buildx
uses: docker/setup-buildx-action@v2

- name: Generate image identifier
id: image
uses: ASzc/change-string-case-action@v5
with:
string: ${{ github.repository_owner }}

- name: Log in to the container registry
uses: docker/login-action@v2
if: ${{ github.ref == 'refs/heads/master' }}
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build bot image
uses: docker/build-push-action@v3
with:
context: .
target: bot
push: ${{ github.ref == 'refs/heads/master' }}
tags: |
ghcr.io/${{ steps.image.outputs.lowercase }}/selfmailbot-bot:latest
ghcr.io/${{ steps.image.outputs.lowercase }}/selfmailbot-bot:${{ github.sha }}
build-args: |
PYTHON_VERSION=${{ steps.setup-python.outputs.python-version }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Build web image
uses: docker/build-push-action@v3
with:
context: .
target: web
push: ${{ github.ref == 'refs/heads/master' }}
tags: |
ghcr.io/${{ steps.image.outputs.lowercase }}/selfmailbot-web:latest
ghcr.io/${{ steps.image.outputs.lowercase }}/selfmailbot-web:${{ github.sha }}
build-args: |
PYTHON_VERSION=${{ steps.setup-python.outputs.python-version }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Build background processing worker image
uses: docker/build-push-action@v3
with:
context: .
target: web
push: ${{ github.ref == 'refs/heads/master' }}
tags: |
ghcr.io/${{ steps.image.outputs.lowercase }}/selfmailbot-worker:latest
ghcr.io/${{ steps.image.outputs.lowercase }}/selfmailbot-worker:${{ github.sha }}
build-args: |
PYTHON_VERSION=${{ steps.setup-python.outputs.python-version }}
cache-from: type=gha
cache-to: type=gha,mode=max
65 changes: 65 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
ARG PYTHON_VERSION

#
# Compile custom uwsgi for web image
#
FROM python:${PYTHON_VERSION}-slim-bookworm as uwsgi-compile
ENV _UWSGI_VERSION 2.0.23
RUN apt-get update && apt-get --no-install-recommends install -y build-essential wget && rm -rf /var/lib/apt/lists/*
RUN wget -O uwsgi-${_UWSGI_VERSION}.tar.gz https://github.com/unbit/uwsgi/archive/${_UWSGI_VERSION}.tar.gz \
&& tar zxvf uwsgi-*.tar.gz \
&& UWSGI_BIN_NAME=/uwsgi make -C uwsgi-${_UWSGI_VERSION} \
&& rm -Rf uwsgi-*



#
# Build poetry and export compiled dependecines as plain requirements.txt
#
FROM python:${PYTHON_VERSION}-slim-bookworm as deps-compile

WORKDIR /
COPY poetry.lock pyproject.toml /
# Version is taken from poetry.lock, assuming it is generated with up-to-date version of poetry
RUN pip install --no-cache-dir poetry==$(cat poetry.lock |head -n1|awk -v FS='(Poetry |and)' '{print $2}')
RUN poetry export --format=requirements.txt > requirements.txt


FROM python:${PYTHON_VERSION}-slim-bookworm as base
LABEL maintainer="[email protected]"
RUN apt-get update \
&& apt-get -y install wget \
&& rm -rf /var/lib/apt/lists/*

RUN pip install --no-cache-dir --upgrade pip
COPY --from=deps-compile /requirements.txt /
RUN pip install --no-cache-dir -r requirements.txt
WORKDIR /
COPY src /src

USER nobody

#
# Bot image
#
FROM base as bot
ENV BOT_ENV production
HEALTHCHECK CMD wget -q -O /dev/null http://localhost:8000/healthcheck || exit 1
CMD python -m src.bot


#
# Background processing image
#
FROM base as worker
HEALTHCHECK CMD celery -A src.celery inspect ping -d celery@$HOSTNAME
CMD celery -A src.celery worker -c ${CONCURENCY:-4} -n "${celery}@%h" --max-tasks-per-child ${MAX_REQUESTS_PER_CHILD:-50} --time-limit ${TIME_LIMIT:-900}


#
# Web image
#
FROM base as web
COPY --from=uwsgi-compile /uwsgi /usr/local/bin/
CMD uwsgi --master --http :8000 --module src.web:app

15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fmt:
poetry run ruff format src
poetry run ruff check src --fix
poetry run toml-sort pyproject.toml

lint:
poetry run ruff check src
poetry run mypy src
poetry run toml-sort pyproject.toml --check

dev:
poetry run watchmedo auto-restart --directory src --patterns '*.py' --recursive -- python -- -m src.bot

worker:
poetry run watchmedo auto-restart --directory src --patterns '*.py' --recursive -- celery -- -A src.celery worker --purge
Loading

0 comments on commit 5e1ee4d

Please sign in to comment.