From 141320f20a979cc72fdbb88ccd69a0c330992b01 Mon Sep 17 00:00:00 2001 From: ddorn Date: Sun, 22 Sep 2024 14:46:10 +0200 Subject: [PATCH] feat(deployment): Add Docker and Fly.io integration * Add a .dockerignore file to exclude unnecessary files and directories (like .git, __pycache__, .envrc, .venv, and fly.toml) from Docker builds, optimizing build context size and performance. * Introduce a Dockerfile setting up a multi-stage build for the `pucoti` application using Python 3.12. This enhances container performance by separating dependencies installation and the runtime environment. * Set up a GitHub Actions workflow `fly-deploy.yml` for continuous deployment to Fly.io. It triggers on pushes to the main branch and deploys the app using `flyctl`. * Create a fly.toml file for configuring Fly.io deployments, including app settings such as region, HTTP service preferences, vm size, and processes. --- .dockerignore | 5 +++++ .github/workflows/fly-deploy.yml | 18 ++++++++++++++++++ Dockerfile | 15 +++++++++++++++ fly.toml | 21 +++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 .dockerignore create mode 100644 .github/workflows/fly-deploy.yml create mode 100644 Dockerfile create mode 100644 fly.toml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..4a1c39c --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +fly.toml +.git/ +__pycache__/ +.envrc +.venv/ diff --git a/.github/workflows/fly-deploy.yml b/.github/workflows/fly-deploy.yml new file mode 100644 index 0000000..b0c246e --- /dev/null +++ b/.github/workflows/fly-deploy.yml @@ -0,0 +1,18 @@ +# See https://fly.io/docs/app-guides/continuous-deployment-with-github-actions/ + +name: Fly Deploy +on: + push: + branches: + - main +jobs: + deploy: + name: Deploy app + runs-on: ubuntu-latest + concurrency: deploy-group # optional: ensure only one action runs at a time + steps: + - uses: actions/checkout@v4 + - uses: superfly/flyctl-actions/setup-flyctl@master + - run: flyctl deploy --remote-only + env: + FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1819a35 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM python:3.12 AS builder + +ENV PYTHONUNBUFFERED=1 \ + PYTHONDONTWRITEBYTECODE=1 +WORKDIR /app + +RUN pip install poetry +RUN poetry config virtualenvs.in-project true +COPY pyproject.toml poetry.lock ./ +RUN poetry install +FROM python:3.12-slim +WORKDIR /app +COPY --from=builder /app/.venv .venv/ +COPY . . +CMD ["/app/.venv/bin/uvicorn", "pucoti.server:app", "--host", "0.0.0.0", "--port", "8000"] diff --git a/fly.toml b/fly.toml new file mode 100644 index 0000000..e51dd26 --- /dev/null +++ b/fly.toml @@ -0,0 +1,21 @@ +# fly.toml app configuration file generated for pucoti on 2024-09-21T19:52:26+02:00 +# +# See https://fly.io/docs/reference/configuration/ for information about how to use this file. +# + +app = 'pucoti' +primary_region = 'cdg' + +[build] + +[http_service] + internal_port = 8000 + force_https = true + auto_stop_machines = 'stop' + auto_start_machines = true + min_machines_running = 0 + max_machines_running = 1 + processes = ['app'] + +[[vm]] + size = 'shared-cpu-1x'