Guide CI/CD pour WordPress Headless | GitLab CI/CD

Guide Complet CI/CD pour WordPress Headless

Automatisez vos déploiements avec GitLab CI/CD
De 0 à production en 30 minutes

Pourquoi le CI/CD est Essentiel ?

❌ AVANT (Manuel)

  • 2-3 heures par déploiement
  • Risque d'erreurs humaines
  • Stress et peur de casser la prod
  • Tests manuels incomplets
  • Downtime de 10-30 minutes

✅ APRÈS (CI/CD)

  • 2-3 minutes par déploiement
  • Processus standardisé et fiable
  • Confiance totale, zéro stress
  • Tests automatisés à 100%
  • Zéro downtime
💡 Le saviez-vous ? Le CI/CD n'est pas un luxe, c'est une nécessité en 2025. Les équipes qui l'utilisent déploient 200x plus souvent avec 3x moins d'incidents.

Ce Dont Vous Avez Besoin

  • Un compte GitLab (gratuit)
  • Votre projet WordPress Headless dans Git
  • Un compte Vercel/Netlify (ou serveur SSH)
  • 30 minutes de temps
  • Connaissances de base en Git

📊 Niveau requis : Intermédiaire
⏱️ Temps : 30 minutes de configuration

Configuration GitLab CI/CD

Pipeline Frontend Next.js + Vercel

.gitlab-ci.yml
# Pipeline GitLab CI/CD pour Frontend Next.js

stages:
  - build
  - test
  - deploy

variables:
  NODE_VERSION: "18"
  WORDPRESS_API_URL: "https://cms.votresite.com/wp-json"

# Cache pour accélérer les builds
.node-cache:
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
      - .next/cache/

# Job 1 : Build de l'application
build-app:
  extends: .node-cache
  image: node:${NODE_VERSION}-alpine
  stage: build
  script:
    - echo "🔨 Installation des dépendances..."
    - npm ci
    - echo "🏗️ Build de l'application Next.js..."
    - npm run build
  artifacts:
    paths:
      - .next/
      - public/
    expire_in: 1 hour
  only:
    - main
    - develop

# Job 2 : Tests unitaires
test-unit:
  extends: .node-cache
  image: node:${NODE_VERSION}-alpine
  stage: test
  script:
    - npm ci
    - echo "🧪 Exécution des tests..."
    - npm run test
  coverage: '/All files[^|]*\|[^|]*\s+([\d\.]+)/'

# Job 3 : Linting
test-lint:
  extends: .node-cache
  image: node:${NODE_VERSION}-alpine
  stage: test
  script:
    - npm ci
    - echo "🔍 Vérification du code..."
    - npm run lint

# Job 4 : Déploiement Production
deploy-production:
  image: node:${NODE_VERSION}-alpine
  stage: deploy
  before_script:
    - npm install -g vercel
  script:
    - echo "🚀 Déploiement en production..."
    - vercel --prod --token=$VERCEL_TOKEN --yes
  environment:
    name: production
    url: https://www.votresite.com
  when: manual
  only:
    - main

# Job 5 : Déploiement Staging Auto
deploy-staging:
  image: node:${NODE_VERSION}-alpine
  stage: deploy
  before_script:
    - npm install -g vercel
  script:
    - echo "🧪 Déploiement en staging..."
    - vercel --token=$VERCEL_TOKEN --yes
  environment:
    name: staging
    url: https://staging.votresite.com
  only:
    - develop
✅ Ce pipeline fait quoi ?
1. Détecte automatiquement les push sur main ou develop
2. Installe les dépendances npm
3. Build votre app Next.js
4. Lance les tests automatisés
5. Vérifie la qualité du code
6. Déploie sur Vercel

Total : ~2-3 minutes de la détection au déploiement !

Pipeline Backend WordPress

.gitlab-ci.yml (Backend WordPress)
# Pipeline pour WordPress Backend

stages:
  - validate
  - deploy

variables:
  PHP_VERSION: "8.2"

# Validation PHP
phpcs:
  image: php:${PHP_VERSION}-alpine
  stage: validate
  script:
    - composer install
    - vendor/bin/phpcs --standard=WordPress wp-content/themes/custom/
  only:
    - merge_requests

# Tests PHPUnit
phpunit:
  image: php:${PHP_VERSION}-alpine
  stage: validate
  services:
    - mysql:8.0
  variables:
    MYSQL_DATABASE: wordpress_test
    MYSQL_ROOT_PASSWORD: test_password
  script:
    - apt-get update && apt-get install -y git unzip
    - docker-php-ext-install mysqli pdo pdo_mysql
    - composer install
    - vendor/bin/phpunit
  only:
    - merge_requests

# Déploiement Staging
deploy-staging:
  image: alpine:latest
  stage: deploy
  before_script:
    - apk add openssh-client rsync
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | ssh-add -
  script:
    - rsync -avz wp-content/themes/ deploy@staging:/var/www/wp-content/themes/
    - ssh deploy@staging "cd /var/www && wp cache flush"
  environment:
    name: staging
  only:
    - develop

# Déploiement Production
deploy-production:
  image: alpine:latest
  stage: deploy
  before_script:
    - apk add openssh-client rsync
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | ssh-add -
  script:
    - rsync -avz wp-content/themes/ deploy@prod:/var/www/wp-content/themes/
    - ssh deploy@prod "cd /var/www && wp cache flush"
  environment:
    name: production
  when: manual
  only:
    - main

Pipeline Full Stack (Frontend + Backend)

Pour gérer les deux dans un seul repository monorepo :

.gitlab-ci.yml (Monorepo)
stages:
  - build
  - test
  - deploy

# Frontend Build
build-frontend:
  image: node:18-alpine
  stage: build
  script:
    - cd frontend
    - npm ci
    - npm run build
  artifacts:
    paths:
      - frontend/.next/
  only:
    changes:
      - frontend/**/*

# Backend Validation
validate-backend:
  image: php:8.2-alpine
  stage: test
  script:
    - cd backend
    - composer install
    - vendor/bin/phpcs --standard=WordPress
  only:
    changes:
      - backend/**/*

# Deploy Frontend
deploy-frontend:
  image: node:18-alpine
  stage: deploy
  script:
    - cd frontend
    - npm install -g vercel
    - vercel --prod --token=$VERCEL_TOKEN --yes
  only:
    - main

# Deploy Backend
deploy-backend:
  image: alpine:latest
  stage: deploy
  before_script:
    - apk add openssh-client rsync
  script:
    - cd backend
    - rsync -avz wp-content/ deploy@server:/var/www/wp-content/
  only:
    - main

Configuration des Variables Secrètes

1

Accéder aux Variables GitLab

Dans votre projet GitLab :
Settings → CI/CD → Variables → Expand

2

Ajouter les Variables

Variable Valeur Protected Masked
VERCEL_TOKEN Votre token Vercel ✅ ✅
WORDPRESS_API_URL https://cms.votresite.com/wp-json ❌ ❌
SSH_PRIVATE_KEY Votre clé SSH privée ✅ ✅
3

Obtenir le Token Vercel

  1. Allez sur vercel.com/account/tokens
  2. Cliquez "Create Token"
  3. Nom : "GitLab CI"
  4. Scope : Full Account
  5. Copiez le token généré
  6. Collez-le dans GitLab Variables

Stratégie de Branches Git

main (production)
↑ merge après validation
develop (staging)
↑ merge des features
feature/ma-feature

Workflow Recommandé

Commandes Git
# 1. DÉVELOPPEMENT
git checkout -b feature/ma-feature
# ... développez votre fonctionnalité
git add .
git commit -m "Ajout de ma feature"
git push origin feature/ma-feature

# 2. STAGING (Tests)
git checkout develop
git merge feature/ma-feature
git push origin develop
# → Pipeline déploie automatiquement sur staging

# 3. PRODUCTION
git checkout main
git merge develop
git push origin main
# → Déploiement manuel via GitLab UI

Optimisations du Pipeline

💾 Cache

Gain : 50-70% de temps

  • Cache node_modules
  • Cache .next/cache
  • Cache composer vendor/

Build de 5min → 1min30

⚡ Jobs Parallèles

Gain : 40% de temps

  • Tests + Lint simultanés
  • Build indépendant
  • Déploiements multiples

Pipeline 40% plus rapide

🐳 Images Légères

Démarrage instantané

  • Alpine Linux (5 MB)
  • Layer caching Docker
  • Multi-stage builds

Téléchargement 10x plus rapide

Checklist de Vérification

⚙️ Configuration GitLab

  • Fichier .gitlab-ci.yml créé
  • Variables secrètes configurées
  • Runner GitLab actif
  • Branches protégées configurées

🧪 Tests

  • Tests unitaires fonctionnent
  • Linting configuré et valide
  • Pipeline s'exécute sans erreur
  • Coverage > 80%

🚀 Déploiement

  • Staging automatique OK
  • Production manuel OK
  • Rollback testé
  • Notifications configurées

Besoin d'Aide pour Votre Pipeline CI/CD ?

Configuration trop complexe ? Vous bloquez quelque part ?
Je peux vous aider à mettre en place votre pipeline CI/CD sur mesure.

Demander un Devis →