Environments GitLab
Les environments representent les cibles de deploiement (staging, production, etc.).
stages:
- build
- test
- deploy
deploy-staging:
stage: deploy
script:
- echo "Deploiement sur staging..."
- rsync -avz dist/ deploy@staging-server:/var/www/app/
environment:
name: staging
url: https://staging.mon-app.com
rules:
- if: $CI_COMMIT_BRANCH == "develop"
deploy-production:
stage: deploy
script:
- echo "Deploiement en production..."
- rsync -avz dist/ deploy@prod-server:/var/www/app/
environment:
name: production
url: https://mon-app.com
rules:
- if: $CI_COMMIT_BRANCH == "main"
when: manual
Deploiement manuel (approval)
Le mot-cle when: manual oblige un utilisateur a cliquer pour lancer le deploiement.
deploy-production:
stage: deploy
script:
- ./deploy.sh production
environment:
name: production
when: manual
allow_failure: false # Le pipeline attend l'approbation
Deploiement avec Docker
deploy-docker:
stage: deploy
image: docker:latest
services:
- docker:dind
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
- ssh deploy@server "docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA && docker-compose up -d"
Rollback
# GitLab garde l'historique des deploiements
# Operations > Environments > Revert (bouton dans l'interface)
# Rollback manuel
deploy-rollback:
stage: deploy
script:
- docker pull $CI_REGISTRY_IMAGE:$ROLLBACK_TAG
- docker-compose up -d
when: manual
Bonne pratique : Deployez toujours en staging d'abord et utilisez des approbations manuelles pour la production.