#!/bin/bash set -e # Configuration SERVER="debian@92.243.27.35" APP_NAME="zapwall" DOMAIN="zapwall.fr" APP_DIR="/var/www/${DOMAIN}" GIT_REPO="https://git.4nkweb.com/4nk/story-research-zapwall.git" # Fonction pour exécuter une commande SSH ssh_exec() { ssh -o ConnectTimeout=10 ${SERVER} "$@" 2>&1 } # Vérifier qu'un message de commit est fourni if [ -z "$1" ]; then echo "Erreur: Un message de commit est requis" echo "" echo "Usage: ./deploy.sh \"Message de commit\"" echo "" echo "Exemple: ./deploy.sh \"Fix: Correction du bug de connexion\"" exit 1 fi COMMIT_MESSAGE="$1" echo "=== Déploiement de ${DOMAIN} ===" echo "" # Détecter la branche courante BRANCH=$(git branch --show-current 2>/dev/null || echo "main") echo "Branche courante: ${BRANCH}" echo "Message de commit: ${COMMIT_MESSAGE}" echo "" # Commit et push des modifications locales echo "1. Préparation du commit local..." HAS_CHANGES=false HAS_UNPUSHED_COMMITS=false # Vérifier s'il y a des modifications non commitées if ! git diff --quiet || ! git diff --cached --quiet; then HAS_CHANGES=true fi # Vérifier s'il y a des commits non poussés if git rev-parse --abbrev-ref ${BRANCH}@{upstream} >/dev/null 2>&1; then LOCAL=$(git rev-parse @) REMOTE=$(git rev-parse @{u}) if [ "$LOCAL" != "$REMOTE" ]; then HAS_UNPUSHED_COMMITS=true fi else # Pas de branche distante configurée, vérifier s'il y a des commits locaux if git rev-list --count origin/${BRANCH}..HEAD >/dev/null 2>&1; then UNPUSHED_COUNT=$(git rev-list --count origin/${BRANCH}..HEAD 2>/dev/null || echo "0") if [ "$UNPUSHED_COUNT" -gt 0 ]; then HAS_UNPUSHED_COMMITS=true fi fi fi if [ "$HAS_CHANGES" = true ]; then echo " ✓ Modifications détectées" echo "" echo "2. Ajout des modifications..." git add -A echo " ✓ Fichiers ajoutés" echo "" echo "3. Création du commit..." git commit -m "${COMMIT_MESSAGE}" echo " ✓ Commit créé" HAS_UNPUSHED_COMMITS=true fi if [ "$HAS_UNPUSHED_COMMITS" = true ]; then echo "" echo "4. Push vers le dépôt distant..." git push origin ${BRANCH} echo " ✓ Push effectué" else echo " ⚠ Aucune modification à commiter ni commit à pousser" fi # Vérifier si Git est initialisé sur le serveur echo "" echo "5. Vérification du dépôt Git sur le serveur..." if ssh_exec "cd ${APP_DIR} && git status >/dev/null 2>&1" >/dev/null 2>&1; then echo " ✓ Dépôt Git détecté" else echo " ⚠ Dépôt Git non initialisé, initialisation..." # Initialiser le dépôt Git en une seule commande pour réduire les appels SSH ssh_exec "cd ${APP_DIR} && (git init 2>/dev/null || true) && (git remote add origin ${GIT_REPO} 2>/dev/null || git remote set-url origin ${GIT_REPO}) && (git checkout -b ${BRANCH} 2>/dev/null || true)" echo " ✓ Dépôt Git initialisé" fi # Récupérer les dernières modifications echo "" echo "6. Récupération des dernières modifications..." ssh_exec "cd ${APP_DIR} && git fetch origin" # Sauvegarder les modifications locales sur le serveur echo "" echo "7. Sauvegarde des modifications locales sur le serveur..." STASH_OUTPUT=$(ssh_exec "cd ${APP_DIR} && git stash push -u -m 'Auto-stash before deploy - $(date +%Y-%m-%d_%H:%M:%S)' 2>&1" || echo "No changes to stash") if echo "$STASH_OUTPUT" | grep -q "No local changes"; then echo " ✓ Aucune modification locale à sauvegarder" else echo " ✓ Modifications locales sauvegardées" fi # Nettoyer les fichiers non suivis echo "" echo "8. Nettoyage des fichiers non suivis..." ssh_exec "cd ${APP_DIR} && git clean -fd || true" # Vérifier que la branche existe echo "" echo "9. Vérification de la branche ${BRANCH}..." if ssh_exec "cd ${APP_DIR} && git ls-remote --heads origin ${BRANCH} | grep -q ${BRANCH}"; then echo " ✓ Branche ${BRANCH} trouvée" else echo " ✗ Branche ${BRANCH} non trouvée sur le dépôt distant" echo "" echo " Branches disponibles:" AVAILABLE_BRANCHES=$(ssh_exec "cd ${APP_DIR} && git ls-remote --heads origin | sed 's/.*refs\\/heads\\///'") echo "$AVAILABLE_BRANCHES" | sed 's/^/ - /' echo "" echo " Erreur: La branche '${BRANCH}' n'existe pas sur le dépôt distant." echo " Vérifiez que vous avez bien poussé la branche avec 'git push origin ${BRANCH}'" exit 1 fi # Mise à jour depuis la branche echo "" echo "10. Mise à jour depuis la branche ${BRANCH}..." ssh_exec "cd ${APP_DIR} && git checkout ${BRANCH} 2>/dev/null || git checkout -b ${BRANCH} origin/${BRANCH}" ssh_exec "cd ${APP_DIR} && git pull origin ${BRANCH}" # Afficher le dernier commit echo "" echo "11. Dernier commit:" ssh_exec "cd ${APP_DIR} && git log -1 --oneline" echo "" echo "12. Installation des dépendances (reproductible via lockfile)..." ssh_exec "cd ${APP_DIR} && npm ci" echo "" echo "13. Construction de l'application..." ssh_exec "cd ${APP_DIR} && npm run build" # Redémarrer le service echo "" echo "14. Redémarrage du service ${APP_NAME}..." ssh_exec "sudo systemctl restart ${APP_NAME}" sleep 3 # Vérifier que le service fonctionne echo "" echo "15. Vérification du service..." if ssh_exec "sudo systemctl is-active ${APP_NAME} >/dev/null"; then echo " ✓ Service actif" echo "" echo " Statut du service:" ssh_exec "sudo systemctl status ${APP_NAME} --no-pager | head -10" else echo " ✗ Service inactif, vérification des logs..." ssh_exec "sudo journalctl -u ${APP_NAME} --no-pager -n 30" exit 1 fi # Vérifier que le port est en écoute echo "" echo "16. Vérification du port 3001..." if ssh_exec "sudo ss -tuln | grep -q ':3001 '"; then echo " ✓ Port 3001 en écoute" else echo " ⚠ Port 3001 non encore en écoute, attente..." sleep 5 if ssh_exec "sudo ss -tuln | grep -q ':3001 '"; then echo " ✓ Port 3001 maintenant en écoute" else echo " ✗ Port 3001 toujours non en écoute" exit 1 fi fi echo "" echo "=== Déploiement terminé avec succès ===" echo "" echo "Site disponible sur: https://${DOMAIN}/" echo "" echo "Commandes utiles:" echo " Voir les logs: ssh ${SERVER} 'sudo journalctl -u ${APP_NAME} -f'" echo " Voir les stashes: ssh ${SERVER} 'cd ${APP_DIR} && git stash list'" echo " Restaurer un stash: ssh ${SERVER} 'cd ${APP_DIR} && git stash pop'"