From d15b3c51ac44de57ec151fd09f779af667db5bc5 Mon Sep 17 00:00:00 2001 From: Goose Date: Sun, 27 Sep 2026 01:16:13 +0000 Subject: [PATCH] ci(deploy): add production settings, Dockerfile, k8s manifests, and Forgejo deployment workflow --- .forgejo/workflows/deploy.yaml | 30 +++++++++ Dockerfile | 47 ++++++++++++++ entrypoint.sh | 10 +++ k8s.yaml | 108 +++++++++++++++++++++++++++++++++ openforce/asgi.py | 5 ++ openforce/settings/prod.py | 43 +++++++++++++ openforce/wsgi.py | 2 +- requirements.txt | 1 + 8 files changed, 245 insertions(+), 1 deletion(-) create mode 100644 .forgejo/workflows/deploy.yaml create mode 100644 Dockerfile create mode 100755 entrypoint.sh create mode 100644 k8s.yaml create mode 100644 openforce/asgi.py create mode 100644 openforce/settings/prod.py diff --git a/.forgejo/workflows/deploy.yaml b/.forgejo/workflows/deploy.yaml new file mode 100644 index 0000000..e5fcb55 --- /dev/null +++ b/.forgejo/workflows/deploy.yaml @@ -0,0 +1,30 @@ +name: Build, Test, and Deploy +on: + push: + branches: [main] + +jobs: + build-and-deploy: + runs-on: self-hosted + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Build and Package Docker Image + run: | + docker build \ + -t git.ch4t.buzz/buzzparty/openforce:${{ github.sha }} \ + -t git.ch4t.buzz/buzzparty/openforce:latest \ + . + + - name: Push to Forgejo Registry + run: | + docker push git.ch4t.buzz/buzzparty/openforce:${{ github.sha }} + docker push git.ch4t.buzz/buzzparty/openforce:latest + + - name: Roll out to Kubernetes + run: | + kubectl set image deployment/openforce \ + openforce=git.ch4t.buzz/buzzparty/openforce:${{ github.sha }} \ + -n default + kubectl rollout status deployment/openforce -n default --timeout=180s diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..cdf37fd --- /dev/null +++ b/Dockerfile @@ -0,0 +1,47 @@ +# Stage 1: Build Vue 3 Frontend +FROM node:20-alpine AS frontend-builder +WORKDIR /app/frontend + +COPY frontend/package*.json ./ +RUN npm ci + +COPY frontend/ ./ +RUN npm run build + +# Stage 2: Python / Django Runtime +FROM python:3.11-slim + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + DJANGO_SETTINGS_MODULE=openforce.settings.prod \ + PORT=8000 + +WORKDIR /app + +# Install system dependencies +RUN apt-get update && apt-get install -y --no-install-recommends \ + build-essential \ + libpq-dev \ + curl \ + && rm -rf /var/lib/apt/lists/* + +# Install Python requirements +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +# Copy application source +COPY . . + +# Copy built frontend assets to frontend/dist +COPY --from=frontend-builder /app/frontend/dist /app/frontend/dist + +# Collect static files into STATIC_ROOT +RUN mkdir -p /app/staticfiles /app/data && \ + python manage.py collectstatic --noinput + +RUN chmod +x /app/entrypoint.sh + +EXPOSE 8000 + +# Start via entrypoint +ENTRYPOINT ["/app/entrypoint.sh"] diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..264ea35 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,10 @@ +#!/bin/sh +set -e + +# Run database migrations +echo "Running database migrations..." +python manage.py migrate --noinput + +# Start Gunicorn server +echo "Starting Gunicorn server..." +exec gunicorn --bind 0.0.0.0:8000 --workers 3 --timeout 120 openforce.wsgi:application diff --git a/k8s.yaml b/k8s.yaml new file mode 100644 index 0000000..aef38a9 --- /dev/null +++ b/k8s.yaml @@ -0,0 +1,108 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: openforce + namespace: default + labels: + app: openforce +spec: + replicas: 1 + selector: + matchLabels: + app: openforce + template: + metadata: + labels: + app: openforce + spec: + containers: + - name: openforce + image: git.ch4t.buzz/buzzparty/openforce:latest + imagePullPolicy: Always + ports: + - containerPort: 8000 + name: http + env: + - name: DJANGO_SETTINGS_MODULE + value: "openforce.settings.prod" + - name: DJANGO_SECRET_KEY + valueFrom: + secretKeyRef: + name: openforce-secret + key: DJANGO_SECRET_KEY + optional: true + - name: SQLITE_DB_PATH + value: "/app/data/db.sqlite3" + volumeMounts: + - name: openforce-data + mountPath: /app/data + resources: + requests: + memory: "256Mi" + cpu: "100m" + limits: + memory: "512Mi" + cpu: "500m" + readinessProbe: + httpGet: + path: / + port: 8000 + httpHeaders: + - name: Host + value: crm.ch4t.buzz + initialDelaySeconds: 10 + periodSeconds: 10 + livenessProbe: + httpGet: + path: / + port: 8000 + httpHeaders: + - name: Host + value: crm.ch4t.buzz + initialDelaySeconds: 15 + periodSeconds: 20 + volumes: + - name: openforce-data + emptyDir: {} +--- +apiVersion: v1 +kind: Service +metadata: + name: openforce + namespace: default + labels: + app: openforce +spec: + type: ClusterIP + ports: + - port: 80 + targetPort: 8000 + protocol: TCP + name: http + selector: + app: openforce +--- +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: openforce + namespace: default + annotations: + cert-manager.io/cluster-issuer: letsencrypt-prod +spec: + ingressClassName: traefik + rules: + - host: crm.ch4t.buzz + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: openforce + port: + number: 80 + tls: + - hosts: + - crm.ch4t.buzz + secretName: openforce-tls diff --git a/openforce/asgi.py b/openforce/asgi.py new file mode 100644 index 0000000..49d1405 --- /dev/null +++ b/openforce/asgi.py @@ -0,0 +1,5 @@ +import os +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', os.environ.get('DJANGO_SETTINGS_MODULE', 'openforce.settings.prod')) +application = get_asgi_application() diff --git a/openforce/settings/prod.py b/openforce/settings/prod.py new file mode 100644 index 0000000..2b33c6a --- /dev/null +++ b/openforce/settings/prod.py @@ -0,0 +1,43 @@ +import os +from .base import * + +DEBUG = False + +SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', 'insecure-prod-key-override-via-env') + +ALLOWED_HOSTS = [ + 'crm.ch4t.buzz', + 'localhost', + '127.0.0.1', +] + +# CSRF & Security Headers +CSRF_TRUSTED_ORIGINS = [ + 'https://crm.ch4t.buzz', +] + +SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') +SECURE_SSL_REDIRECT = os.environ.get('SECURE_SSL_REDIRECT', 'false').lower() in ('true', '1') +SESSION_COOKIE_SECURE = True +CSRF_COOKIE_SECURE = True +SECURE_BROWSER_XSS_FILTER = True +SECURE_CONTENT_TYPE_NOSNIFF = True +X_FRAME_OPTIONS = 'DENY' + +# Database configuration: support SQLite persistent volume or PostgreSQL via env +DATABASE_URL = os.environ.get('DATABASE_URL') +if DATABASE_URL: + import dj_database_url # optional if installed + DATABASES = { + 'default': dj_database_url.config(default=DATABASE_URL, conn_max_age=600) + } +else: + DB_PATH = os.environ.get('SQLITE_DB_PATH', str(BASE_DIR / 'data' / 'db.sqlite3')) + os.makedirs(os.path.dirname(DB_PATH), exist_ok=True) + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': DB_PATH, + 'ATOMIC_REQUESTS': True, + } + } diff --git a/openforce/wsgi.py b/openforce/wsgi.py index 2a3ddf1..8607d12 100644 --- a/openforce/wsgi.py +++ b/openforce/wsgi.py @@ -1,5 +1,5 @@ import os from django.core.wsgi import get_wsgi_application -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'openforce.settings.base') +os.environ.setdefault('DJANGO_SETTINGS_MODULE', os.environ.get('DJANGO_SETTINGS_MODULE', 'openforce.settings.prod')) application = get_wsgi_application() diff --git a/requirements.txt b/requirements.txt index 3343201..10e719b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,7 @@ psycopg2-binary>=2.9 jsonschema>=4.0 django-cors-headers>=4.0 whitenoise>=6.5 +gunicorn>=21.2.0 pytest>=7.0 pytest-django>=4.5 pytest-cov>=4.0