xpeditis2.0/PORTAINER_ENV_FIX.md
David 6e3191b50e
All checks were successful
CI/CD Pipeline / Backend - Build, Test & Push (push) Successful in 5m45s
CI/CD Pipeline / Frontend - Build, Test & Push (push) Successful in 28m26s
CI/CD Pipeline / Integration Tests (push) Has been skipped
CI/CD Pipeline / Deployment Summary (push) Successful in 1s
CI/CD Pipeline / Deploy to Portainer (push) Successful in 14s
CI/CD Pipeline / Discord Notification (Failure) (push) Has been skipped
CI/CD Pipeline / Discord Notification (Success) (push) Successful in 1s
fix ci/cd and docker
2025-11-20 00:12:01 +01:00

6.2 KiB

🔧 Fix NODE_ENV pour Portainer

🚨 Problème Identifié

Backend Erreur

ERROR [ExceptionHandler] Config validation error: "NODE_ENV" must be one of [development, production, test]

Cause : La validation Joi dans apps/backend/src/app.module.ts (ligne 35) n'accepte QUE :

  • development
  • production
  • test

Mais le stack utilisait NODE_ENV=preprod

Frontend Redémarrage en Boucle

Le frontend redémarrait sans cesse (status "complete" répété) parce que le backend crashait, donc le health check échouait.


Solution Appliquée

Changement dans docker/portainer-stack.yml

Backend (ligne 83) :

environment:
  NODE_ENV: production  # ← Changé de "preprod" à "production"
  PORT: "4000"
  # ...

Frontend (ligne 157) :

environment:
  NODE_ENV: production  # ← Changé de "preprod" à "production"
  NEXT_PUBLIC_API_URL: https://api.preprod.xpeditis.com
  # ...

🎯 Pourquoi production et Pas preprod ?

Option 1 : Utiliser production ( SOLUTION ACTUELLE)

Avantages :

  • Fonctionne immédiatement sans changer le code
  • Active les optimisations de production (logs niveau info, pas de debug)
  • Comportement attendu pour un environnement de pre-production

Configuration :

NODE_ENV: production

Option 2 : Modifier la Validation Backend (Alternative)

Si vous voulez vraiment utiliser preprod, modifier apps/backend/src/app.module.ts :

// Ligne 35
NODE_ENV: Joi.string()
  .valid('development', 'production', 'test', 'preprod')  // ← Ajouter 'preprod'
  .default('development'),

Inconvénients :

  • Nécessite rebuild des images Docker
  • Re-trigger la CI/CD
  • Pas standard (Node.js attend development/production/test)

📊 Impact du NODE_ENV

Backend (NestJS)

NODE_ENV=production active :

  • Logs niveau info (pas debug)
  • Logging optimisé (JSON, pas pino-pretty)
  • Optimisations de performance
  • Caching agressif

NODE_ENV=development active :

  • Logs niveau debug (verbose)
  • Pino-pretty avec couleurs (plus lisible mais plus lent)
  • Pas de caching
  • Hot reload (non applicable en Docker)

Frontend (Next.js)

NODE_ENV=production active :

  • Build optimisé (minification, tree-shaking)
  • Images optimisées
  • Pas de React DevTools
  • Meilleure performance

🔍 Vérification Post-Fix

Backend : Logs Attendus

Portainer → Containers → xpeditis-backend → Logs :

🚀 Starting Xpeditis Backend...
⏳ Waiting for PostgreSQL to be ready...
✅ PostgreSQL is ready
🔄 Running database migrations...
✅ DataSource initialized
✅ Successfully ran 10 migration(s)
✅ Database migrations completed
🚀 Starting NestJS application...
[Nest] 1  - LOG [NestFactory] Starting Nest application...
[Nest] 1  - LOG [InstanceLoader] AppModule dependencies initialized
[Nest] 1  - LOG [RoutesResolver] AppController {/api/v1}:
[Nest] 1  - LOG Application is running on: http://0.0.0.0:4000

Plus d'erreur de validation

Frontend : Logs Attendus

Portainer → Containers → xpeditis-frontend → Logs :

▲ Next.js 14.0.4
- Local:        http://localhost:3000
- Network:      http://0.0.0.0:3000

✓ Ready in 88ms

Container reste en état "running" (pas de redémarrage)


📋 Checklist de Déploiement (Mise à Jour)

1. Update le Stack Portainer

  1. Portainer → Stacks → xpeditis-preprod
  2. Editor → Copier le nouveau portainer-stack.yml (avec NODE_ENV=production)
  3. Cocher "Re-pull image and redeploy"
  4. Update the stack

2. Vérifier les Services

Portainer → Containers :

Container État Attendu Logs Clés
xpeditis-backend Running (Healthy) ✅ Database migrations completed
xpeditis-frontend Running (Healthy) ✓ Ready in XXms

3. Tester les Endpoints

# Backend health check
curl https://api.preprod.xpeditis.com/api/v1/health
# Réponse : {"status":"ok","info":{"database":{"status":"up"},"redis":{"status":"up"}}}

# Frontend
curl -I https://app.preprod.xpeditis.com
# Réponse : HTTP/2 200

🎯 Résumé du Fix

Avant Après Résultat
NODE_ENV: preprod NODE_ENV: production Backend démarre
Backend crash Backend running Migrations OK
Frontend loop Frontend stable Reste en "running"

🔧 Si Vous Voulez Vraiment Utiliser preprod

Étape 1 : Modifier le Backend

Fichier : apps/backend/src/app.module.ts

validationSchema: Joi.object({
  NODE_ENV: Joi.string()
    .valid('development', 'production', 'test', 'preprod')  // ← Ajouter
    .default('development'),
  // ...
}),

Étape 2 : Ajuster les Conditions

Fichier : apps/backend/src/app.module.ts (ligne 56-66)

LoggerModule.forRootAsync({
  useFactory: (configService: ConfigService) => {
    const env = configService.get('NODE_ENV');
    const isDev = env === 'development';
    const isProd = env === 'production' || env === 'preprod';  // ← Traiter preprod comme prod

    return {
      pinoHttp: {
        transport: isDev ? { /* ... */ } : undefined,
        level: isProd ? 'info' : 'debug',
      },
    };
  },
}),

Étape 3 : Rebuild et Push

# Commit changes
git add apps/backend/src/app.module.ts
git commit -m "feat: add preprod to NODE_ENV validation"

# Push to trigger CI/CD
git push origin preprod

# Attendre que CI/CD rebuild et push les images (~15 min)

Étape 4 : Update Stack Portainer

Changer NODE_ENV: productionNODE_ENV: preprod dans le stack.


Recommandation : Garder NODE_ENV=production car :

  • Standard Node.js/NestJS
  • Fonctionne immédiatement
  • Pre-production = production-like environment

Date : 2025-11-19 Fix Appliqué : NODE_ENV=production dans portainer-stack.yml Status : Prêt pour déploiement