From 34b71e0ba26277ec817c75f8591447a88a9f7cca Mon Sep 17 00:00:00 2001 From: Xiao <65860997+xiaopeng-ye@users.noreply.github.com> Date: Tue, 24 Dec 2024 22:51:44 +0100 Subject: [PATCH 1/2] Refactor Dockerfile for multi-stage build and add .dockerignore (#50) --- .dockerignore | 41 +++++++++++++++++++++++++++++++++++++++++ Dockerfile | 33 ++++++++++++++++++++++++++++----- 2 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..4720766 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,41 @@ +# Git +.git +.gitignore + +# Python +__pycache__ +*.pyc +*.pyo +*.pyd +.Python +env +pip-log.txt +pip-delete-this-directory.txt +.tox +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.log + +# Virtual environment +venv +.env +.venv +ENV + +# IDE +.idea +.vscode +*.swp +*.swo + +# Project specific +docs/ +tests/ +*.md +LICENSE +pytest.ini +setup.py diff --git a/Dockerfile b/Dockerfile index cfcee63..7d3a04f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,37 @@ -FROM python:3.12 +# Build stage +FROM python:3.12-slim AS builder + +WORKDIR /build + +# Copy requirements first to leverage Docker cache +COPY requirements.txt . + +# Install build dependencies and Python packages +RUN apt-get update \ + && apt-get install -y --no-install-recommends gcc python3-dev \ + && pip install --no-cache-dir --upgrade pip \ + && pip install --no-cache-dir --timeout 1000 -r requirements.txt \ + && rm -rf /var/lib/apt/lists/* + +# Runtime stage +FROM python:3.12-slim + +# Set Python environment variables +ENV PYTHONUNBUFFERED=1 +ENV PYTHONDONTWRITEBYTECODE=1 + +# Install git +RUN apt-get update \ + && apt-get install -y --no-install-recommends git \ + && rm -rf /var/lib/apt/lists/* WORKDIR /app # Create a non-root user RUN useradd -m -u 1000 appuser +COPY --from=builder /usr/local/lib/python3.12/site-packages/ /usr/local/lib/python3.12/site-packages/ COPY src/ ./ -COPY requirements.txt ./ - -RUN pip install -r requirements.txt # Change ownership of the application files RUN chown -R appuser:appuser /app @@ -18,4 +41,4 @@ USER appuser EXPOSE 8000 -CMD ["uvicorn", "main:app", "--reload", "--host", "0.0.0.0"] +CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] From fe02f66fb33bcb01e48eb2de96fad14659a84799 Mon Sep 17 00:00:00 2001 From: cyclotruc Date: Wed, 25 Dec 2024 01:11:56 +0000 Subject: [PATCH 2/2] Add automated Tests run on push --- .github/workflows/unitest.yml | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 .github/workflows/unitest.yml diff --git a/.github/workflows/unitest.yml b/.github/workflows/unitest.yml new file mode 100644 index 0000000..e1fb7eb --- /dev/null +++ b/.github/workflows/unitest.yml @@ -0,0 +1,33 @@ +name: Unit Tests + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11"] + + steps: + - uses: actions/checkout@v4 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pytest pytest-asyncio + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + pip install -e . + + - name: Run tests + run: | + pytest \ No newline at end of file