Skip to content

Commit

Permalink
Merge pull request #6 from max-pfeiffer/feature/base_image_update_and…
Browse files Browse the repository at this point in the history
…_multi_arch

Feature/base image update and multi arch
  • Loading branch information
max-pfeiffer authored Apr 19, 2024
2 parents 9d3713c + 8ea0b66 commit ae8cb0b
Show file tree
Hide file tree
Showing 29 changed files with 2,731 additions and 1,964 deletions.
2 changes: 2 additions & 0 deletions .env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PYTHON_VERSION=3.12.2
OS_VARIANT=slim-bookworm
129 changes: 106 additions & 23 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ jobs:
runs-on: ubuntu-20.04
steps:
- name: Checkout Repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: 3.9
python-version: 3.11
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.4.1
version: 1.8.2
virtualenvs-in-project: true
- name: Load cached venv
id: cached-poetry-dependencies
Expand All @@ -27,28 +27,31 @@ jobs:
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: |
poetry install --no-interaction --no-root
- name: Run pylint and black
- name: Run pre-commit hooks
run: |
source .venv/bin/activate
pylint build tests
black --check .
poetry run pre-commit run -a
run-tests:
run-build-image-tests:
needs: code-quality
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
python_version: ["3.10.14", "3.11.8", "3.12.2"]
os_variant: ["bookworm", "slim-bookworm"]
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
uses: actions/checkout@v4
- name: Expose GitHub Runtime
uses: crazy-max/ghaction-github-runtime@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: 3.9
python-version: 3.11
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.4.1
version: 1.8.2
virtualenvs-in-project: true
- name: Load cached venv
id: cached-poetry-dependencies
Expand All @@ -60,35 +63,112 @@ jobs:
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: |
poetry install --no-interaction --no-root
- name: Run all tests with pytest
- name: Run tests for image builds with pytest
env:
PYTHON_VERSION: ${{ matrix.python_version }}
OS_VARIANT: ${{ matrix.os_variant }}
POETRY_VERSION: ${{ matrix.poetry_version }}
run: |
source .venv/bin/activate
pytest --cov
poetry run pytest tests/build_image --cov --cov-report=xml:build_image_coverage_report.xml
- name: Upload coverage report to artifactory
uses: actions/upload-artifact@v3
with:
name: build-image-coverage-report-${{ matrix.python_version }}-${{ matrix.os_variant }}-${{ matrix.poetry_version }}
path: build_image_coverage_report.xml
if-no-files-found: error
retention-days: 1

run-publish-image-tests:
needs: code-quality
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
python_version: [ "3.10.14", "3.11.8", "3.12.2" ]
os_variant: [ "bookworm", "slim-bookworm" ]
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Expose GitHub Runtime
uses: crazy-max/ghaction-github-runtime@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: 3.11
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.8.2
virtualenvs-in-project: true
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: |
poetry install --no-interaction --no-root
- name: Run tests for image publishing with pytest
env:
PYTHON_VERSION: ${{ matrix.python_version }}
OS_VARIANT: ${{ matrix.os_variant }}
POETRY_VERSION: ${{ matrix.poetry_version }}
run: |
poetry run pytest tests/publish_image --cov --cov-report=xml:publish_image_coverage_report.xml
- name: Upload coverage report to artifactory
uses: actions/upload-artifact@v3
with:
name: publish-image-coverage-report-${{ matrix.python_version }}-${{ matrix.os_variant }}-${{ matrix.poetry_version }}
path: publish_image_coverage_report.xml
if-no-files-found: error
retention-days: 1

upload-test-coverage-reports:
needs:
- run-build-image-tests
- run-publish-image-tests
runs-on: ubuntu-20.04
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Download coverage reports from artifactory
uses: actions/download-artifact@v3
- name: Compile the relevant reports
run: |
find . -name "*.xml" -exec cp {} . \;
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
files: ./build_image_coverage_report.xml,./publish_image_coverage_report.xml
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}

publish-all-images:
needs: run-tests
needs: upload-test-coverage-reports
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
python_version: ["3.10.14", "3.11.8", "3.12.2"]
os_variant: ["bookworm", "slim-bookworm"]
steps:
- name: Checkout Repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Expose GitHub Runtime
uses: crazy-max/ghaction-github-runtime@v3
- name: Get Git Commit Tag Name
uses: olegtarasov/[email protected]
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: 3.9
python-version: 3.11
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.4.1
version: 1.8.2
virtualenvs-in-project: true
- name: Load cached venv
id: cached-poetry-dependencies
Expand All @@ -104,6 +184,9 @@ jobs:
env:
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }}
DOCKER_HUB_PASSWORD: ${{ secrets.DOCKER_HUB_PASSWORD }}
PYTHON_VERSION: ${{ matrix.python_version }}
OS_VARIANT: ${{ matrix.os_variant }}
POETRY_VERSION: ${{ matrix.poetry_version }}
run: |
source .venv/bin/activate
python -m build.publish
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.env
.venv
.env
.idea
.vscode
.pytest_cache
Expand Down
12 changes: 1 addition & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ repos:
- id: detect-private-key
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 23.3.0
rev: 24.4.0
hooks:
- id: black
- repo: local
hooks:
- id: pylint
name: pylint
entry: .venv/bin/pylint
language: system
types: [python]
args:
- build
- tests
28 changes: 17 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ Any feedback is highly appreciated and will be considered.
**GitHub Repository:** [https://github.com/max-pfeiffer/uvicorn-gunicorn-poetry](https://github.com/max-pfeiffer/uvicorn-gunicorn-poetry)

## Docker Image Features
1. Supported architectures:
1. Python v3.9, Debian or Debian-slim
2. Python v3.10, Debian or Debian-slim
3. Python v3.11, Debian or Debian-slim
2. Poetry is available as Python package dependency management tool
3. A virtual environment for the application and application server
4. The application is run with [Gunicorn](https://gunicorn.org/) and Uvicorn workers
1. Poetry v1.8.2 is available as Python package dependency management tool
2. A virtual environment for the application and application server
3. The application is run with [Gunicorn](https://gunicorn.org/) and Uvicorn workers
4. Python versions:
1. 3.10
2. 3.11
3. 3.12
5. Operating system variants:
1. [Debian Bookworm v12.1](https://www.debian.org/releases/bookworm/)
2. [Debian Bookworm slim v12.1](https://www.debian.org/releases/bookworm/)
6. Supported CPU architectures:
1. linux/amd64
2. linux/arm64/v8

## Usage
It just provides a platform that you can use to build upon your own multistage builds. So it consequently does not contain an
Expand Down Expand Up @@ -59,12 +65,12 @@ The application and test structure needs to be like that:
```
Please be aware that you need to provide a pyproject.toml file to specify your Python package dependencies for Poetry and configure
dependencies like Pytest. Poetry dependencies must at least contain the following to work:
* python = "^3.9"
* gunicorn = "20.1.0"
* uvicorn = "0.18.3"
* python = "^3.10"
* gunicorn = "22.0.0"
* uvicorn = "0.29.0"

If your application uses FastAPI framework this needs to be added as well:
* fastapi = "0.85.0"
* fastapi = "0.110.2"

**IMPORTANT:** make sure you have a [.dockerignore file](https://github.com/max-pfeiffer/uvicorn-gunicorn-poetry/blob/master/examples/fast_api_multistage_build/.dockerignore)
in your application root which excludes your local virtual environment in .venv! Otherwise, you will have an issue activating that virtual
Expand Down
1 change: 0 additions & 1 deletion build/.gitignore

This file was deleted.

42 changes: 3 additions & 39 deletions build/constants.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,7 @@
UVICORN_GUNICORN_POETRY_IMAGE_NAME = "pfeiffermax/uvicorn-gunicorn-poetry"
FAST_API_SINGLESTAGE_IMAGE_NAME: str = "fast-api-singlestage-build"
FAST_API_MULTISTAGE_IMAGE_NAME = "fast-api-multistage-build"
TARGET_ARCHITECTURES: list[str] = [
"python3.9.16-bullseye",
"python3.9.16-slim-bullseye",
"python3.10.11-bullseye",
"python3.10.11-slim-bullseye",
"python3.11.3-bullseye",
"python3.11.3-slim-bullseye",
]
BASE_IMAGES: dict = {
TARGET_ARCHITECTURES[
0
]: "pfeiffermax/python-poetry:1.3.0-poetry1.5.0-python3.9.16-bullseye@sha256:b5859d3d2308999853db2b14647b0ff155b86bf6b9f5799366c01c465ebb532a",
TARGET_ARCHITECTURES[
1
]: "pfeiffermax/python-poetry:1.3.0-poetry1.5.0-python3.9.16-slim-bullseye@sha256:9f35b4d878d5a3d421a21e388653e55cce0c09b3736c9b835c0fbbafeb1d1757",
TARGET_ARCHITECTURES[
2
]: "pfeiffermax/python-poetry:1.3.0-poetry1.5.0-python3.10.11-bullseye@sha256:d789d7e7a386e70da132ee0015dd7ece073a486b5db39a1aad22c1a59270b461",
TARGET_ARCHITECTURES[
3
]: "pfeiffermax/python-poetry:1.3.0-poetry1.5.0-python3.10.11-slim-bullseye@sha256:4a4b89205389d95f77868e574c796c8335447ad18204f1d041987c710c2ec423",
TARGET_ARCHITECTURES[
4
]: "pfeiffermax/python-poetry:1.3.0-poetry1.5.0-python3.11.3-bullseye@sha256:42909a19b117146153344ddea0c5332d807d26bb7a63ab5c5fd08a0879a3623c",
TARGET_ARCHITECTURES[
5
]: "pfeiffermax/python-poetry:1.3.0-poetry1.5.0-python3.11.3-slim-bullseye@sha256:47905681d13ca8c1c044dbdadfbe882f1633f524755406cb50a7bc2a2b80a422",
}
PYTHON_VERSIONS: dict = {
TARGET_ARCHITECTURES[0]: "3.9.16",
TARGET_ARCHITECTURES[1]: "3.9.16",
TARGET_ARCHITECTURES[2]: "3.10.11",
TARGET_ARCHITECTURES[3]: "3.10.11",
TARGET_ARCHITECTURES[4]: "3.11.3",
TARGET_ARCHITECTURES[5]: "3.11.3",
}
"""Constants for image build."""

# As we are running the server with an unprivileged user, we need to use
# a high port.
APPLICATION_SERVER_PORT: str = "8000"

PLATFORMS: list[str] = ["linux/amd64", "linux/arm64/v8"]
Loading

0 comments on commit ae8cb0b

Please sign in to comment.