Git auf VPS: Deployment mit Git Push automatisieren
Git ist nicht nur für Code-Versionierung.
Git ist nicht nur für Code-Versionierung. Du kannst auch direkt auf deinen VPS pushen und automatisch deployen.
Warum Git für Deployment?
Vorteile:
- Versionskontrolle (jedes Deployment ist nachvollziehbar)
- Ein Command: git push production
- Rollbacks einfach: git reset --hard HEAD~1
- Kein FTP/SFTP nötig
Perfekt für kleinere Projekte.
Git auf VPS installieren
sudo apt update
sudo apt install git
# Version checken:
git --version
Bare Repository auf VPS erstellen
Ein Bare Repo hat keine Working Copy – perfekt als Remote.
# Auf dem VPS:
mkdir -p ~/git/mywebsite.git
cd ~/git/mywebsite.git
git init --bare
Post-Receive Hook für Auto-Deploy
Der Hook wird nach jedem Push ausgeführt:
# Auf dem VPS:
nano ~/git/mywebsite.git/hooks/post-receive
# Inhalt:
#!/bin/bash
GIT_WORK_TREE=/var/www/mywebsite git checkout -f
echo "Deployed to /var/www/mywebsite"
# Executable machen:
chmod +x ~/git/mywebsite.git/hooks/post-receive
Remote auf lokalem Rechner hinzufügen
# Auf deinem lokalen Rechner (im Projekt):
git remote add production user@vps-ip:git/mywebsite.git
# Pushen:
git push production main
# Output:
# Deployed to /var/www/mywebsite
Erweiterte Hooks: Build-Prozess
Hook kann auch npm/composer/etc. ausführen:
#!/bin/bash
GIT_WORK_TREE=/var/www/mywebsite git checkout -f
cd /var/www/mywebsite
# Dependencies installieren:
npm install
npm run build
# Oder Composer:
composer install --no-dev
echo "Deployed & built!"
Fazit
Git-Deployment ist simpel und mächtig. Ein git push, und deine Änderungen sind live.
Wenn du noch nach einem günstigen VPS für deinen Git-Server suchst, findest du regelmäßig aktualisierte Codes bei HostDealHero.