Automatisez vos déploiements avec GitLab CI/CD
De 0 à production en 30 minutes
📊 Niveau requis : Intermédiaire
⏱️ Temps : 30 minutes de configuration
# 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
# 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
Pour gérer les deux dans un seul repository 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
Dans votre projet GitLab :
Settings → CI/CD → Variables → Expand
| 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 | ✅ | ✅ |
# 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
Gain : 50-70% de temps
Build de 5min → 1min30
Gain : 40% de temps
Pipeline 40% plus rapide
Démarrage instantané
Téléchargement 10x plus rapide
Configuration trop complexe ? Vous bloquez quelque part ?
Je peux vous aider à mettre en place votre pipeline CI/CD sur mesure.