#!/usr/bin/env bash set -euo pipefail # Script d'automatisation des push SSH (template Linux) # Utilise automatiquement la clé SSH pour pousser sur le remote courant via SSH. GITEA_HOST="${GITEA_HOST:-git.4nkweb.com}" echo "🔑 Configuration SSH pour push (template)..." # Configuration SSH automatique echo "⚙️ Configuration Git pour utiliser SSH..." git config --global url."git@${GITEA_HOST}:".insteadOf "https://${GITEA_HOST}/" # Vérifier la configuration SSH echo "🔍 Vérification de la configuration SSH..." if ! ssh -T git@"${GITEA_HOST}" 2>&1 | grep -qi "authenticated\|welcome"; then echo "❌ Échec de l'authentification SSH" echo "💡 Vérifiez que votre clé SSH est configurée :" echo " 1. ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_4nk" echo " 2. Ajouter la clé publique à votre compte Gitea" echo " 3. ssh-add ~/.ssh/id_ed25519_4nk" exit 1 fi echo "✅ Authentification SSH réussie" # Fonction pour push automatique get_current_branch() { # Détecte la branche courante, compatible anciennes versions de git local br br="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)" if [ -z "$br" ] || [ "$br" = "HEAD" ]; then br="$(git symbolic-ref --short -q HEAD 2>/dev/null || true)" fi if [ -z "$br" ]; then # dernier recours: parser la sortie de "git branch" br="$(git branch 2>/dev/null | sed -n 's/^* //p' | head -n1)" fi echo "$br" } auto_push() { local branch branch=${1:-$(get_current_branch)} local commit_message=${2:-"Auto-commit $(date '+%Y-%m-%d %H:%M:%S')"} echo "🚀 Push automatique sur la branche: $branch" # Ajouter tous les changements git add . # Ne pas commiter si rien à commite if [[ -z "$(git diff --cached --name-only)" ]]; then echo "ℹ️ Aucun changement indexé. Skip commit/push." return 0 fi # Commiter avec le message fourni git commit -m "$commit_message" || true # Push avec SSH automatique echo "📤 Push vers origin/$branch..." git push origin "$branch" echo "✅ Push réussi !" } # Fonction pour push avec message personnalisé push_with_message() { local message="$1" local branch=${2:-$(get_current_branch)} echo "💬 Push avec message: $message" auto_push "$branch" "$message" } # Fonction pour push rapide (sans message) quick_push() { local branch=${1:-$(get_current_branch)} auto_push "$branch" } # Fonction pour push sur une branche spécifique push_branch() { local branch="$1" local message=${2:-"Update $branch $(date '+%Y-%m-%d %H:%M:%S')"} echo "🌿 Push sur la branche: $branch" auto_push "$branch" "$message" } # Fonction pour push et merge vers main push_and_merge() { local source_branch=${1:-$(get_current_branch)} local target_branch=${2:-main} echo "🔄 Push et merge $source_branch -> $target_branch" # Push de la branche source auto_push "$source_branch" # Indication pour PR manuelle echo "🔗 Ouvrez une Pull Request sur votre forge pour $source_branch -> $target_branch" } # Fonction pour status et push conditionnel status_and_push() { echo "📊 Statut du repository:" git status --short || true if [[ -n $(git status --porcelain) ]]; then echo "📝 Changements détectés, push automatique..." auto_push else echo "✅ Aucun changement à pousser" fi } # Menu interactif si aucun argument fourni if [[ $# -eq 0 ]]; then echo "🤖 Script de push SSH automatique (template)" echo "" echo "Options disponibles:" echo " auto-ssh-push.sh quick - Push rapide" echo " auto-ssh-push.sh message \"Mon message\" - Push avec message" echo " auto-ssh-push.sh branch nom-branche - Push sur branche spécifique" echo " auto-ssh-push.sh merge [source] [target] - Push et préparation merge" echo " auto-ssh-push.sh status - Status et push conditionnel" echo "" exit 0 fi # Traitement des arguments case "$1" in "quick") quick_push ;; "message") if [[ -z "${2:-}" ]]; then echo "❌ Message requis pour l'option 'message'" exit 1 fi push_with_message "$2" "${3:-}" ;; "branch") if [[ -z "${2:-}" ]]; then echo "❌ Nom de branche requis pour l'option 'branch'" exit 1 fi push_branch "$2" "${3:-}" ;; "merge") push_and_merge "${2:-}" "${3:-}" ;; "status") status_and_push ;; *) echo "❌ Option inconnue: $1" echo "💡 Utilisez './scripts/auto-ssh-push.sh' pour voir les options" exit 1 ;; esac echo "🎯 Push SSH automatique terminé !"