From 119bbcc7a866e77b444be610c47bf707c53afce1 Mon Sep 17 00:00:00 2001 From: zwitschi Date: Thu, 23 Oct 2025 17:06:14 +0200 Subject: [PATCH] feat: Add Docker workflows for building, testing, and deploying the application; include Dockerfile for image creation --- .dockerignore | 21 +++++++++++++++++++ .gitea/workflows/build-and-push.yml | 28 ++++++++++++++++++++++++++ .gitea/workflows/deploy.yml | 21 +++++++++++++++++++ .gitea/workflows/test.yml | 24 ++++++++++++++++++++++ Dockerfile | 31 +++++++++++++++++++++++++++++ 5 files changed, 125 insertions(+) create mode 100644 .dockerignore create mode 100644 .gitea/workflows/build-and-push.yml create mode 100644 .gitea/workflows/deploy.yml create mode 100644 .gitea/workflows/test.yml create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..0e5a54c --- /dev/null +++ b/.dockerignore @@ -0,0 +1,21 @@ +.venv +__pycache__ +*.pyc +*.pyo +*.pyd +.Python +env/ +venv/ +.idea +.vscode +.git +.gitignore +.DS_Store +dist +build +*.egg-info +*.sqlite3 +.env +.env.* +.Dockerfile +.dockerignore diff --git a/.gitea/workflows/build-and-push.yml b/.gitea/workflows/build-and-push.yml new file mode 100644 index 0000000..d55de11 --- /dev/null +++ b/.gitea/workflows/build-and-push.yml @@ -0,0 +1,28 @@ +name: Build and Push Docker Image +on: + push: + branches: + - main + +jobs: + build-and-push: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Login to Gitea Registry + uses: docker/login-action@v1 + with: + registry: ${{ secrets.GITEA_REGISTRY }} + username: ${{ secrets.GITEA_USERNAME }} + password: ${{ secrets.GITEA_PASSWORD }} + + - name: Build and push Docker image + uses: docker/build-push-action@v2 + with: + context: . + push: true + tags: ${{ secrets.GITEA_REGISTRY }}/${{ secrets.GITEA_USERNAME }}/calminer:latest + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml new file mode 100644 index 0000000..ed49ab4 --- /dev/null +++ b/.gitea/workflows/deploy.yml @@ -0,0 +1,21 @@ +name: Deploy to Server +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - name: SSH and deploy + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.SSH_HOST }} + username: ${{ secrets.SSH_USERNAME }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + script: | + docker pull ${{ secrets.GITEA_REGISTRY }}/${{ secrets.GITEA_USERNAME }}/calminer:latest + docker stop calminer || true + docker rm calminer || true + docker run -d --name calminer -p 8000:8000 ${{ secrets.GITEA_REGISTRY }}/${{ secrets.GITEA_USERNAME }}/calminer:latest diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml new file mode 100644 index 0000000..5b80d1b --- /dev/null +++ b/.gitea/workflows/test.yml @@ -0,0 +1,24 @@ +name: Run Tests +on: [push] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: "3.10" + - name: Cache pip + uses: actions/cache@v3 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }} + restore-keys: | + ${{ runner.os }}-pip- + - name: Install dependencies + run: pip install -r requirements.txt + - name: Run tests + run: pytest diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..363d24b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,31 @@ +# Multi-stage Dockerfile to keep final image small +FROM python:3.10-slim AS builder + +# Install build-time packages and Python dependencies in one layer +WORKDIR /app +COPY requirements.txt /app/requirements.txt +RUN apt-get update \ + && apt-get install -y --no-install-recommends build-essential gcc libpq-dev \ + && python -m pip install --upgrade pip \ + && pip install --no-cache-dir --prefix=/install -r /app/requirements.txt \ + && apt-get purge -y --auto-remove build-essential gcc \ + && rm -rf /var/lib/apt/lists/* + +FROM python:3.10-slim +WORKDIR /app + +# Copy installed packages from builder +COPY --from=builder /install /usr/local + +# Production environment variables +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 + +# Copy application code +COPY . /app + +# Expose service port +EXPOSE 8000 + +# Run the FastAPI app with uvicorn +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]