# Guide de Configuration GitLab Backup - ParcApp ## Vue d'ensemble Ce guide permet d'ajouter GitLab comme remote "backup" pour sauvegarder vos repos GitHub. **Repos concernés :** - Backend : `parcap-php` (GitHub) → `parcap-php` (GitLab) - Frontend : `parcapp-react` (GitHub) → `parcapp-react` (GitLab) --- ## 1) Pré-vérification ### Vérifier que vous êtes dans le bon dossier ```bash # Windows PowerShell cd D:\dev\parcapp\api git rev-parse --show-toplevel # Linux cd /path/to/parcapp/api git rev-parse --show-toplevel ``` **Résultat attendu :** Chemin vers le dossier racine du repo Git ### Vérifier l'état actuel ```bash # État du working tree git status # Branches locales git branch # Tags git tag -l # Remotes actuels git remote -v ``` **Résultat attendu :** - `origin` pointe vers `https://github.com/acoulGit/parcap-php.git` - Branches : `main`, `develop` - Tag : `v0.3.0` --- ## 2) Créer les repos sur GitLab ### Étapes sur GitLab.com 1. **Se connecter** à https://gitlab.com 2. **Créer un nouveau projet** : - Cliquer sur "New project" → "Create blank project" - **Project name** : `parcap-php` (backend) ou `parcapp-react` (frontend) - **Project slug** : Laisser par défaut - **Visibility Level** : **Private** (recommandé) - **Initialize repository with a README** : ❌ **DÉCOCHER** (important !) - Cliquer sur "Create project" 3. **Noter l'URL SSH** : - Sur la page du projet, copier l'URL SSH - Format : `git@gitlab.com:/parcap-php.git` - Exemple : `git@gitlab.com:acoul/parcap-php.git` **⚠️ IMPORTANT :** Ne pas initialiser avec README pour éviter les conflits lors du premier push. --- ## 3) Configuration SSH ### Option A : SSH (Recommandé) #### Étape 1 : Vérifier si une clé SSH existe **Windows :** ```powershell # Vérifier si des clés existent Test-Path $env:USERPROFILE\.ssh\id_ed25519.pub Test-Path $env:USERPROFILE\.ssh\id_rsa.pub ``` **Linux :** ```bash ls -la ~/.ssh/id_ed25519.pub ls -la ~/.ssh/id_rsa.pub ``` #### Étape 2 : Générer une clé SSH (si nécessaire) **Windows (Git Bash ou PowerShell) :** ```bash ssh-keygen -t ed25519 -C "votre-email@example.com" ``` **Linux :** ```bash ssh-keygen -t ed25519 -C "votre-email@example.com" ``` - Appuyer sur Entrée pour accepter l'emplacement par défaut - Optionnel : Entrer un passphrase (recommandé pour la sécurité) #### Étape 3 : Démarrer ssh-agent et ajouter la clé **Windows (Git Bash) :** ```bash eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519 ``` **Windows (PowerShell) :** ```powershell # Démarrer ssh-agent Start-Service ssh-agent # Ajouter la clé ssh-add $env:USERPROFILE\.ssh\id_ed25519 ``` **Linux :** ```bash eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519 ``` #### Étape 4 : Copier la clé publique **Windows (PowerShell) :** ```powershell Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | Set-Clipboard ``` **Windows (Git Bash) :** ```bash cat ~/.ssh/id_ed25519.pub # Copier manuellement le contenu ``` **Linux :** ```bash cat ~/.ssh/id_ed25519.pub # Copier manuellement le contenu # OU avec xclip : cat ~/.ssh/id_ed25519.pub | xclip -selection clipboard ``` #### Étape 5 : Ajouter la clé dans GitLab 1. Aller sur GitLab.com 2. **User Settings** (icône profil en haut à droite) → **SSH Keys** 3. Coller la clé publique dans le champ "Key" 4. Donner un titre (ex: "Windows Dev Machine" ou "Linux Server") 5. Cliquer sur "Add key" #### Étape 6 : Tester la connexion SSH ```bash ssh -T git@gitlab.com ``` **Résultat attendu :** ``` Welcome to GitLab, @username! ``` Si vous voyez ce message, SSH est configuré correctement. --- ### Option B : HTTPS + Personal Access Token (Alternative) Si SSH ne fonctionne pas, utiliser HTTPS avec un token. #### Étape 1 : Créer un Personal Access Token 1. GitLab.com → **User Settings** → **Access Tokens** 2. **Token name** : `parcapp-backup` (ou autre nom) 3. **Expiration date** : Choisir une date (ou laisser vide pour pas d'expiration) 4. **Scopes** : Cocher au minimum `read_repository` et `write_repository` 5. Cliquer sur "Create personal access token" 6. **⚠️ IMPORTANT :** Copier le token immédiatement (il ne sera plus affiché) #### Étape 2 : Utiliser le token L'URL du remote sera : ``` https://oauth2:@gitlab.com//parcap-php.git ``` Ou utiliser Git Credential Manager (recommandé) : ```bash git config --global credential.helper manager-core # Au premier push, Git demandera username (votre GitLab username) et password (le token) ``` --- ## 4) Ajouter le remote "backup" et pousser ### BACKEND (parcap-php) ```bash # Aller dans le dossier backend cd D:\dev\parcapp\api # Windows # OU cd /path/to/parcapp/api # Linux # Vérifier les remotes actuels git remote -v # Ajouter GitLab comme remote "backup" # REMPLACER par votre username GitLab git remote add backup git@gitlab.com:/parcap-php.git # Vérifier que le remote est ajouté git remote -v # Pousser toutes les branches git push backup --all # Pousser tous les tags git push backup --tags ``` **Résultat attendu :** ``` Enumerating objects: X, done. Counting objects: 100% (X/X), done. ... To gitlab.com:/parcap-php.git * [new branch] main -> main * [new branch] develop -> develop * [new tag] v0.3.0 -> v0.3.0 ``` ### FRONTEND (parcapp-react) - Optionnel ```bash # Aller dans le dossier frontend cd D:\dev\parcapp\frontend # Windows # OU cd /path/to/parcapp/frontend # Linux # Vérifier les remotes actuels git remote -v # Ajouter GitLab comme remote "backup" git remote add backup git@gitlab.com:/parcapp-react.git # Vérifier que le remote est ajouté git remote -v # Pousser toutes les branches git push backup --all # Pousser tous les tags (s'il y en a) git push backup --tags ``` --- ## 5) Validation finale ### Vérification sur GitLab.com 1. Aller sur votre projet GitLab 2. Vérifier : - [ ] Branche `main` existe - [ ] Branche `develop` existe - [ ] Tag `v0.3.0` existe (onglet "Tags" ou "Releases") ### Vérification locale ```bash # Lister les branches distantes sur backup git ls-remote --heads backup # Lister les tags distants sur backup git ls-remote --tags backup ``` **Résultat attendu :** ``` # Branches refs/heads/main refs/heads/develop # Tags refs/tags/v0.3.0 ``` ### Vérification complète ```bash # Voir tous les remotes git remote -v # Voir les branches locales et distantes git branch -a # Voir les tags git tag -l ``` --- ## 6) Push automatique vers GitLab (Optionnel) ### Option A : Push manuel régulier Créer un script pour push automatique : **Windows (PowerShell) - `push-backup.ps1` :** ```powershell # push-backup.ps1 cd D:\dev\parcapp\api git push backup --all git push backup --tags cd D:\dev\parcapp\frontend git push backup --all git push backup --tags Write-Host "✅ Backup vers GitLab terminé" ``` **Linux (Bash) - `push-backup.sh` :** ```bash #!/bin/bash # push-backup.sh cd /path/to/parcapp/api git push backup --all git push backup --tags cd /path/to/parcapp/frontend git push backup --all git push backup --tags echo "✅ Backup vers GitLab terminé" ``` **Utilisation :** ```bash # Windows .\push-backup.ps1 # Linux chmod +x push-backup.sh ./push-backup.sh ``` ### Option B : GitHub Actions (Mirroring automatique) Créer `.github/workflows/mirror-to-gitlab.yml` dans le repo GitHub : ```yaml name: Mirror to GitLab on: push: branches: [ main, develop ] tags: [ 'v*' ] workflow_dispatch: jobs: mirror: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 with: fetch-depth: 0 - name: Setup Git run: | git config --global user.name "GitHub Actions" git config --global user.email "actions@github.com" - name: Push to GitLab env: GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }} run: | git remote add gitlab https://oauth2:${GITLAB_TOKEN}@gitlab.com//parcap-php.git || true git push gitlab --all --force git push gitlab --tags --force ``` **Configuration :** 1. Créer un Personal Access Token sur GitLab (scope `write_repository`) 2. Ajouter le token dans GitHub : Settings → Secrets → Actions → New secret - Name : `GITLAB_TOKEN` - Value : Le token GitLab --- ## 7) Dépannage ### Erreur : "Permission denied (publickey)" **Cause :** Clé SSH non configurée ou non ajoutée à GitLab **Solution :** 1. Vérifier que la clé est ajoutée à GitLab (User Settings → SSH Keys) 2. Tester : `ssh -T git@gitlab.com` 3. Si ça échoue, utiliser HTTPS + token (Option B ci-dessus) ### Erreur : "remote backup already exists" **Cause :** Le remote "backup" existe déjà **Solution :** ```bash # Voir les remotes git remote -v # Supprimer le remote backup existant git remote remove backup # Réajouter git remote add backup git@gitlab.com:/parcap-php.git ``` ### Erreur : "repository not found" **Cause :** Le repo GitLab n'existe pas ou l'URL est incorrecte **Solution :** 1. Vérifier que le repo existe sur GitLab 2. Vérifier l'URL : `git remote -v` 3. Corriger si nécessaire : `git remote set-url backup git@gitlab.com:/.git` ### Erreur : "Host key verification failed" **Cause :** GitLab n'est pas dans les known_hosts **Solution :** ```bash # Ajouter GitLab aux known_hosts ssh-keyscan gitlab.com >> ~/.ssh/known_hosts # Linux ssh-keyscan gitlab.com >> $env:USERPROFILE\.ssh\known_hosts # Windows ``` ### Erreur : "Updates were rejected" **Cause :** Conflit avec le repo GitLab (si initialisé avec README) **Solution :** ```bash # Forcer le push (ATTENTION : écrase le contenu GitLab) git push backup --all --force git push backup --tags --force ``` --- ## 8) Commandes de référence rapide ### Ajouter remote backup ```bash git remote add backup git@gitlab.com:/.git ``` ### Pousser branches et tags ```bash git push backup --all git push backup --tags ``` ### Vérifier les remotes ```bash git remote -v ``` ### Vérifier branches/tags distants ```bash git ls-remote --heads backup git ls-remote --tags backup ``` ### Mettre à jour le backup (après nouveaux commits) ```bash git push backup --all git push backup --tags ``` --- ## ✅ Checklist finale - [ ] Repos GitLab créés (vides, sans README) - [ ] SSH configuré et testé (`ssh -T git@gitlab.com`) - [ ] Remote "backup" ajouté (`git remote -v`) - [ ] Branches poussées (`git push backup --all`) - [ ] Tags poussés (`git push backup --tags`) - [ ] Vérification sur GitLab.com (branches + tags visibles) - [ ] Vérification locale (`git ls-remote backup`) --- ## 📝 Notes importantes 1. **Le remote "origin" (GitHub) reste inchangé** - Tous vos workflows existants continuent de fonctionner 2. **Le remote "backup" est en lecture/écriture** - Vous pouvez push/pull depuis GitLab si besoin 3. **Synchronisation manuelle** - Après chaque push vers GitHub, faire `git push backup --all --tags` pour synchroniser 4. **Pas de secrets** - Vérifier que `.env` et autres secrets ne sont pas trackés (déjà fait via `.gitignore`) --- **Support :** En cas de problème, vérifier les logs Git avec `git push backup --all -v` (mode verbose)