Aller au contenu principal

Bonnes pratiques


1 - Architecture de référence

1.1 Pipeline multi-environnements

1.2 Infrastructure as Code

# Séparer l'infra et l'application
project/
├── infrastructure/
│ ├── vpc/
│ │ └── template.yaml
│ ├── ecs-cluster/
│ │ └── template.yaml
│ └── pipeline/
│ └── template.yaml
├── application/
│ ├── src/
│ ├── Dockerfile
│ ├── buildspec.yml
│ └── appspec.yml
└── environments/
├── dev.yaml
├── staging.yaml
└── production.yaml

2 - Stratégies de branching

2.1 GitFlow simplifié

2.2 Trunk-based development

# Pipeline pour trunk-based
Stages:
- Name: Source
Actions:
- Name: GitHub
Configuration:
BranchName: main

- Name: Build
Actions:
- Name: Build
# Build à chaque commit sur main

- Name: DeployDev
Actions:
- Name: Deploy
# Déploiement automatique en dev

- Name: DeployStaging
Actions:
- Name: Deploy
# Déploiement automatique en staging

- Name: DeployProduction
Actions:
- Name: Approval
# Approbation manuelle
- Name: Deploy
# Déploiement Blue/Green

3 - Tests dans le pipeline

3.1 Pyramide des tests

3.2 Configuration buildspec

# buildspec.yml avec tests
version: 0.2

phases:
install:
commands:
- npm ci

pre_build:
commands:
# Linting
- npm run lint
# Analyse statique
- npm run security-audit

build:
commands:
# Build
- npm run build
# Tests unitaires
- npm run test:unit -- --coverage

post_build:
commands:
# Tests d'intégration
- npm run test:integration

reports:
unit-tests:
files: ['coverage/junit.xml']
file-format: JUNITXML
coverage:
files: ['coverage/cobertura.xml']
file-format: COBERTURAXML

4 - Gestion des environnements

4.1 Variables par environnement

# environments/dev.yaml
Environment: development
ReplicaCount: 1
InstanceType: t3.small
EnableDebug: true
LogLevel: DEBUG

# environments/production.yaml
Environment: production
ReplicaCount: 3
InstanceType: t3.large
EnableDebug: false
LogLevel: INFO

4.2 Paramètres SSM par environnement

# Structure recommandée
/app/dev/config/api-url
/app/dev/secrets/db-password
/app/staging/config/api-url
/app/staging/secrets/db-password
/app/production/config/api-url
/app/production/secrets/db-password

5 - Rollback et recovery

5.1 Stratégies de rollback

StratégieTemps de rollbackComplexité
ECS RollingMinutesFaible
Blue/GreenSecondesMoyenne
CanaryMinutesÉlevée

5.2 Configuration ECS Blue/Green

DeploymentGroup:
Type: AWS::CodeDeploy::DeploymentGroup
Properties:
DeploymentStyle:
DeploymentOption: WITH_TRAFFIC_CONTROL
DeploymentType: BLUE_GREEN

BlueGreenDeploymentConfiguration:
TerminateBlueInstancesOnDeploymentSuccess:
Action: TERMINATE
TerminationWaitTimeInMinutes: 60
DeploymentReadyOption:
ActionOnTimeout: CONTINUE_DEPLOYMENT
WaitTimeInMinutes: 0

AutoRollbackConfiguration:
Enabled: true
Events:
- DEPLOYMENT_FAILURE
- DEPLOYMENT_STOP_ON_ALARM

6 - Monitoring du pipeline

6.1 Métriques essentielles

MétriqueDescriptionSeuil recommandé
Lead TimeTemps commit → production< 1 jour
Deploy FrequencyDéploiements/jour> 1/jour
MTTRTemps de recovery< 1 heure
Change Failure Rate% déploiements échoués< 15%

6.2 Dashboard CloudWatch

PipelineMetricsDashboard:
Type: AWS::CloudWatch::Dashboard
Properties:
DashboardBody: |
{
"widgets": [
{
"type": "metric",
"properties": {
"title": "Pipeline Success Rate",
"metrics": [
["AWS/CodePipeline", "SucceededPipeline", "PipelineName", "MyPipeline"],
[".", "FailedPipeline", ".", "."]
]
}
},
{
"type": "metric",
"properties": {
"title": "Build Duration",
"metrics": [
["AWS/CodeBuild", "Duration", "ProjectName", "MyProject"]
],
"stat": "Average"
}
}
]
}

7 - Optimisation des coûts

7.1 Bonnes pratiques

  • Utiliser le cache S3 pour CodeBuild
  • Choisir le bon compute type (small pour tests légers)
  • Activer les lifecycle policies ECR
  • Utiliser des instances spot pour les tests

7.2 Compute type adapté

# Tests rapides - Small
UnitTestsProject:
Type: AWS::CodeBuild::Project
Properties:
Environment:
ComputeType: BUILD_GENERAL1_SMALL

# Build Docker - Medium
BuildProject:
Type: AWS::CodeBuild::Project
Properties:
Environment:
ComputeType: BUILD_GENERAL1_MEDIUM

# Tests de charge - Large
LoadTestsProject:
Type: AWS::CodeBuild::Project
Properties:
Environment:
ComputeType: BUILD_GENERAL1_LARGE

8 - Checklist déploiement

Pre-déploiement

  • Tests unitaires passent
  • Tests d'intégration passent
  • Scan de sécurité OK
  • Review de code approuvée
  • Documentation à jour

Post-déploiement

  • Health checks OK
  • Métriques normales
  • Pas d'erreurs dans les logs
  • Smoke tests passent
  • Notification envoyée

Résumé

Dans ce chapitre, nous avons couvert :

  • L'architecture de référence multi-environnements
  • Les stratégies de branching
  • Les tests dans le pipeline
  • La gestion des environnements
  • Le rollback et recovery
  • Le monitoring du pipeline
  • L'optimisation des coûts

Prochaine étape

Dans le prochain chapitre, nous mettrons en pratique avec des Exercices et Projets.

→ Chapitre suivant : Exercices et Projets


← Retour à la table des matières