Déploiement : CI/CD GitLab + image frontend de prod + Caddy

Remplace la chaîne GitHub Actions (jamais réellement branchée : aucun
remote git n'existait) par une chaîne GitLab CI/CD, et comble un manque
qu'elle avait déjà : le frontend n'était jusqu'ici jamais construit ni
déployé en production.

- `.gitlab-ci.yml` : tests (backend + frontend) à chaque push ; sur `main`,
  build des images backend et frontend (registre de conteneurs GitLab) puis
  déploiement SSH sur le VPS (pull, migration Alembic, up -d).
- `frontend/Dockerfile.prod` + `frontend/nginx.conf` : build Vite statique
  servi par nginx (repli SPA sur index.html). `VITE_API_MODE`/
  `VITE_EDITION_ID` sont des variables de build, passées en `--build-arg`
  depuis la variable CI/CD `VITE_EDITION_ID`.
- `deploy/docker-compose.prod.yml` : ajout du service `frontend` ; `backend`
  et `frontend` ne publient plus que sur `127.0.0.1` (Caddy, déjà installé
  sur le serveur, devient l'unique porte d'entrée publique).
- `deploy/Caddyfile.benevoles` : bloc de vhost à ajouter au Caddy existant
  (`/api/*` vers le backend avec préfixe retiré, le reste vers le frontend).
- `deploy/README.md` : runbook complet (remote GitLab, variables CI/CD,
  préparation du VPS, création de l'édition de production avant le premier
  build frontend, vhost Caddy).
- CLAUDE.md et README.md mis à jour en conséquence.

Vérifié : `docker compose -f deploy/docker-compose.prod.yml config`,
build réel de l'image frontend de prod, / et une route SPA (/grille)
répondent 200 via nginx.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017uUsRg7XzopkCPWimwg9ZM
This commit is contained in:
AdamThomas
2026-09-14 19:04:28 +02:00
co-authored by Claude Sonnet 5
parent 367dfe42eb
commit d5f67b2701
9 changed files with 333 additions and 135 deletions
+116
View File
@@ -0,0 +1,116 @@
# CI/CD : tests à chaque push, puis sur `main` seulement, build des images
# (registre de conteneurs GitLab) et déploiement SSH sur le VPS.
#
# Variables CI/CD à définir (Paramètres → CI/CD → Variables du projet) :
# SSH_HOST hôte du VPS
# SSH_USER utilisateur SSH
# SSH_KEY clé privée SSH (format OpenSSH), variable « protégée »
# + « masquée », type fichier ou variable multi-lignes
# DEPLOY_PATH dossier du projet sur le VPS (ex. /opt/benevoles)
# VITE_EDITION_ID UUID de l'édition de production, à renseigner APRÈS la
# toute première mise en service (voir deploy/README.md) —
# le frontend fige cette valeur au moment du build.
#
# `CI_REGISTRY`, `CI_REGISTRY_USER`, `CI_REGISTRY_PASSWORD` et
# `CI_REGISTRY_IMAGE` sont fournis automatiquement par GitLab.
stages:
- test
- build
- deploy
variables:
BACKEND_IMAGE: $CI_REGISTRY_IMAGE/backend
FRONTEND_IMAGE: $CI_REGISTRY_IMAGE/frontend
backend-test:
stage: test
image: python:3.12-slim
services:
- name: postgres:16-alpine
alias: postgres
variables:
POSTGRES_USER: benevoles
POSTGRES_PASSWORD: benevoles
POSTGRES_DB: benevoles_test
TEST_DATABASE_URL: postgresql+psycopg://benevoles:benevoles@postgres:5432/benevoles_test
DATABASE_URL: postgresql+psycopg://benevoles:benevoles@postgres:5432/benevoles_test
before_script:
- pip install --no-cache-dir uv
- cd backend
- uv sync
script:
- uv run ruff check .
- uv run alembic upgrade head
- uv run alembic check
- uv run pytest -q
frontend-test:
stage: test
image: node:20-alpine
before_script:
- cd frontend
- npm ci
script:
- npm run typecheck
- npm run lint
- npm run build
- npm run smoke
build-backend:
stage: build
image: docker:27
services:
- docker:27-dind
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
- docker build -t "$BACKEND_IMAGE:$CI_COMMIT_SHORT_SHA" -t "$BACKEND_IMAGE:latest" backend
- docker push "$BACKEND_IMAGE:$CI_COMMIT_SHORT_SHA"
- docker push "$BACKEND_IMAGE:latest"
build-frontend:
stage: build
image: docker:27
services:
- docker:27-dind
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
- >
docker build
--build-arg VITE_API_MODE=http
--build-arg VITE_EDITION_ID="$VITE_EDITION_ID"
-f frontend/Dockerfile.prod
-t "$FRONTEND_IMAGE:$CI_COMMIT_SHORT_SHA" -t "$FRONTEND_IMAGE:latest"
frontend
- docker push "$FRONTEND_IMAGE:$CI_COMMIT_SHORT_SHA"
- docker push "$FRONTEND_IMAGE:latest"
deploy:
stage: deploy
image: alpine:3.20
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
before_script:
- apk add --no-cache openssh-client rsync
- eval "$(ssh-agent -s)"
- echo "$SSH_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh && chmod 700 ~/.ssh
- ssh-keyscan -H "$SSH_HOST" >> ~/.ssh/known_hosts
script:
- rsync -az deploy/docker-compose.prod.yml "$SSH_USER@$SSH_HOST:$DEPLOY_PATH/docker-compose.prod.yml"
- |
ssh "$SSH_USER@$SSH_HOST" sh -s <<EOF
set -e
cd "$DEPLOY_PATH"
echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
export BACKEND_IMAGE="$BACKEND_IMAGE:$CI_COMMIT_SHORT_SHA"
export FRONTEND_IMAGE="$FRONTEND_IMAGE:$CI_COMMIT_SHORT_SHA"
docker compose -f docker-compose.prod.yml pull
docker compose -f docker-compose.prod.yml run --rm backend uv run alembic upgrade head
docker compose -f docker-compose.prod.yml up -d
docker image prune -f
EOF