TP12.2 - Pipeline CI/CD et Déploiement
Objectifs du TP
Dans ce TP, vous allez:
- ✅ Créer un workflow GitHub Actions complet
- ✅ Automatiser tests, build, et publication Docker
- ✅ Déployer sur Railway (ou alternative)
- ✅ Configurer le déploiement automatique
Durée estimée: 1h30
Prérequis: TP12.1 terminé, compte GitHub, compte Railway
Contexte
Vous automatisez le déploiement de l'application Todo du TP12.1. Chaque push sur main déclenche:
- Tests automatiques
- Build de l'image Docker
- Publication sur Docker Hub
- Déploiement sur Railway
Partie 1: Préparation
Structure des Fichiers
.github/
└── workflows/
├── ci.yml # Tests sur chaque PR
├── cd.yml # Build + Deploy sur main
└── docker-publish.yml # Publication Docker
Créer les Secrets GitHub
Aller dans: Repository → Settings → Secrets and variables → Actions
# Docker Hub
DOCKERHUB_USERNAME=votre-username
DOCKERHUB_TOKEN=votre-token
# Railway
RAILWAY_TOKEN=votre-token
# (Optionnel) Heroku
HEROKU_API_KEY=votre-api-key
HEROKU_APP_NAME=votre-app
HEROKU_EMAIL=votre-email
Partie 2: Workflow CI (Tests)
.github/workflows/ci.yml
name: CI - Tests
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
test:
name: Run Tests
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15-alpine
env:
POSTGRES_DB: testdb
POSTGRES_USER: test
POSTGRES_PASSWORD: test
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
cache: 'maven'
- name: Run Unit Tests
run: mvn test -Dspring.profiles.active=test
- name: Run Integration Tests
run: mvn verify -Dspring.profiles.active=test
env:
SPRING_DATASOURCE_URL: jdbc:postgresql://localhost:5432/testdb
SPRING_DATASOURCE_USERNAME: test
SPRING_DATASOURCE_PASSWORD: test
- name: Upload Test Results
uses: actions/upload-artifact@v4
if: always()
with:
name: test-results
path: target/surefire-reports/
- name: Generate Coverage Report
run: mvn jacoco:report
- name: Upload Coverage
uses: codecov/codecov-action@v3
with:
files: target/site/jacoco/jacoco.xml
fail_ci_if_error: false
Partie 3: Workflow CD (Build & Deploy)
.github/workflows/cd.yml
name: CD - Build and Deploy
on:
push:
branches: [main]
release:
types: [published]
env:
REGISTRY: docker.io
IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/todo-app
jobs:
# ===== JOB 1: Test =====
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
cache: 'maven'
- name: Run Tests
run: mvn test
# ===== JOB 2: Build Docker =====
build:
name: Build and Push Docker
runs-on: ubuntu-latest
needs: test
outputs:
image-tag: ${{ steps.meta.outputs.tags }}
version: ${{ steps.meta.outputs.version }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=sha,prefix=
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Print image info
run: |
echo "Image pushed: ${{ env.IMAGE_NAME }}"
echo "Tags: ${{ steps.meta.outputs.tags }}"
# ===== JOB 3: Deploy to Railway =====
deploy:
name: Deploy to Railway
runs-on: ubuntu-latest
needs: build
if: github.ref == 'refs/heads/main'
environment: production
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install Railway CLI
run: npm i -g @railway/cli
- name: Deploy to Railway
run: railway up --detach
env:
RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}
- name: Print deployment info
run: |
echo "✅ Deployed to Railway!"
echo "Image: ${{ needs.build.outputs.image-tag }}"
Partie 4: Configuration Railway
Créer un Projet Railway
- Aller sur railway.app
- "New Project" → "Empty Project"
- "Add Service" → "GitHub Repo" → Sélectionner votre repo
- "Add Database" → "PostgreSQL"
Obtenir le Token Railway
- Account Settings → Tokens
- "Create Token"
- Copier le token dans GitHub Secrets
Variables d'Environnement Railway
Dans le dashboard Railway, configurer:
SPRING_PROFILES_ACTIVE=prod
DATABASE_URL=${{Postgres.DATABASE_URL}}
# Railway injecte automatiquement DATABASE_URL depuis PostgreSQL
railway.json (optionnel)
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "DOCKERFILE",
"dockerfilePath": "Dockerfile"
},
"deploy": {
"numReplicas": 1,
"healthcheckPath": "/actuator/health",
"healthcheckTimeout": 30,
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 3
}
}
Partie 5: Alternative Heroku
.github/workflows/deploy-heroku.yml
name: Deploy to Heroku
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Deploy to Heroku
uses: akhileshns/heroku-deploy@v3.12.14
with:
heroku_api_key: ${{ secrets.HEROKU_API_KEY }}
heroku_app_name: ${{ secrets.HEROKU_APP_NAME }}
heroku_email: ${{ secrets.HEROKU_EMAIL }}
usedocker: true
Fichiers Heroku Nécessaires
heroku.yml:
build:
docker:
web: Dockerfile
run:
web: java -Dserver.port=$PORT -jar app.jar
Partie 6: Tester le Pipeline
1. Push Initial
git add .
git commit -m "Add CI/CD pipeline"
git push origin main
2. Vérifier GitHub Actions
- Aller dans Actions tab
- Vérifier que le workflow s'exécute
- Chaque job doit passer au vert ✅
3. Vérifier Docker Hub
- Aller sur hub.docker.com
- Vérifier que l'image est publiée
- Vérifier les tags
4. Vérifier le Déploiement
# Railway
curl https://your-app.railway.app/actuator/health
# Ou Heroku
curl https://your-app.herokuapp.com/actuator/health
Partie 7: Workflow avec Notifications
Slack/Discord Notifications
notify:
name: Notify
runs-on: ubuntu-latest
needs: [deploy]
if: always()
steps:
- name: Send Slack notification
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
fields: repo,message,commit,author,action,eventName,ref,workflow
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
if: always()
Partie 8: Badge de Statut
Ajouter au README.md
# Todo App


## Description
Application de gestion de tâches avec Spring Boot.
## Liens
- Production: https://your-app.railway.app
- API Docs: https://your-app.railway.app/swagger-ui.html
Vérification Finale
Checklist
- Push déclenche le workflow CI
- Tests passent dans GitHub Actions
- Image Docker publiée sur Docker Hub
- Application déployée sur Railway
-
/actuator/healthretourne UP - L'API fonctionne en production
URLs à Vérifier
# Health check
curl https://your-app.railway.app/actuator/health
# API
curl https://your-app.railway.app/api/todos
# Créer un todo en prod
curl -X POST https://your-app.railway.app/api/todos \
-H "Content-Type: application/json" \
-d '{"title": "Test en production!"}'
🎯 Module Terminé!
Félicitations! Vous avez complété le Module 12 - Déploiement et Production.
Récapitulatif:
- Chapitre 1: Préparation pour production
- Chapitre 2: Docker et conteneurisation
- Chapitre 3: CI/CD avec GitHub Actions
- Chapitre 4: Déploiement cloud
- TP12.1: Dockerisation complète
- TP12.2: Pipeline CI/CD
Compétences acquises:
- ✅ Profils Spring Boot (dev, prod, docker)
- ✅ Dockerfile multi-stage optimisé
- ✅ Docker Compose avec PostgreSQL
- ✅ Workflows GitHub Actions
- ✅ Déploiement automatique sur Railway
- ✅ Monitoring et health checks