#!/bin/bash # Git-basiertes Deployment für Custom PHP Framework # Verwendet Gitea statt GitHub/externe Services # Konfiguration SERVER_USER="deploy" SERVER_IP="94.16.110.151" REMOTE_PATH="/var/www/michaelschiemer" GITEA_REPO_URL="git@gitea.example.com:user/michaelschiemer.git" # Anpassen an deine Gitea-URL SSH_OPTS="-o StrictHostKeyChecking=no" # Farben für Output GREEN="\e[32m" YELLOW="\e[33m" RED="\e[31m" BLUE="\e[34m" RESET="\e[0m" # Logging log() { echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${RESET} $1" } success() { echo -e "${GREEN}✅ $1${RESET}" } warning() { echo -e "${YELLOW}⚠️ $1${RESET}" } error() { echo -e "${RED}❌ $1${RESET}" exit 1 } # Validierung der lokalen Umgebung validate_local() { log "Validiere lokale Umgebung..." # Git Status prüfen if [[ -n $(git status --porcelain) ]]; then error "Working directory nicht clean. Bitte committen oder stashen." fi # Auf main branch? CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) if [[ "$CURRENT_BRANCH" != "main" ]]; then warning "Aktueller Branch: $CURRENT_BRANCH (nicht main)" read -p "Fortfahren? (y/n): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then error "Deployment abgebrochen" fi fi success "Lokale Validierung erfolgreich" } # Pre-Deployment Tests run_local_tests() { log "Führe lokale Tests aus..." # Framework-Tests if ! ./vendor/bin/pest --bail; then error "Pest Tests fehlgeschlagen" fi # Code Style if ! composer cs --dry-run; then error "Code Style Violations gefunden. Führe 'composer cs-fix' aus." fi # Static Analysis (optional, kann lange dauern) read -p "PHPStan ausführen? (empfohlen) (y/n): " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then if ! make phpstan; then warning "PHPStan Warnungen gefunden, aber Deployment wird fortgesetzt" fi fi success "Lokale Tests erfolgreich" } # Frontend Assets bauen build_assets() { log "Baue Frontend-Assets..." if ! npm ci; then error "NPM Install fehlgeschlagen" fi if ! npm run build; then error "Asset Build fehlgeschlagen" fi success "Assets erfolgreich gebaut" } # Git Tag für Deployment erstellen create_deployment_tag() { log "Erstelle Deployment-Tag..." TIMESTAMP=$(date +"%Y%m%d-%H%M%S") COMMIT_HASH=$(git rev-parse --short HEAD) DEPLOY_TAG="deploy-${TIMESTAMP}-${COMMIT_HASH}" git tag -a "$DEPLOY_TAG" -m "Deployment $(date): $(git log -1 --pretty=%B | head -n1)" # Push zu Gitea log "Push zu Gitea..." git push origin main git push origin "$DEPLOY_TAG" echo "$DEPLOY_TAG" > .deploy-tag success "Deployment-Tag erstellt: $DEPLOY_TAG" } # Server-Deployment ausführen deploy_to_server() { local DEPLOY_TAG="$1" log "Deploye zu Server mit Tag: $DEPLOY_TAG" # Deployment-Script auf Server übertragen und ausführen scp $SSH_OPTS ./scripts/server-deploy.sh "$SERVER_USER@$SERVER_IP:/tmp/server-deploy.sh" ssh $SSH_OPTS "$SERVER_USER@$SERVER_IP" "chmod +x /tmp/server-deploy.sh && /tmp/server-deploy.sh '$DEPLOY_TAG' '$REMOTE_PATH'" if [[ $? -eq 0 ]]; then success "Server-Deployment erfolgreich" else error "Server-Deployment fehlgeschlagen" fi } # Post-Deployment Validierung validate_deployment() { log "Validiere Deployment..." # Framework Health Check if ssh $SSH_OPTS "$SERVER_USER@$SERVER_IP" "cd $REMOTE_PATH && php console.php health:check"; then success "Framework Health Check erfolgreich" else error "Framework Health Check fehlgeschlagen" fi # MCP Server Test if ssh $SSH_OPTS "$SERVER_USER@$SERVER_IP" "cd $REMOTE_PATH && timeout 5 php console.php mcp:server --test 2>/dev/null"; then success "MCP Server funktionsfähig" else warning "MCP Server Test fehlgeschlagen (nicht kritisch)" fi # HTTP Test sleep 5 # Warte bis Services hochgefahren sind if curl -f -s -H "User-Agent: Mozilla/5.0 (Deployment Test)" "https://$SERVER_IP" > /dev/null; then success "HTTP-Zugriff erfolgreich" else warning "HTTP-Test fehlgeschlagen - prüfe SSL/nginx Konfiguration" fi } # Rollback-Funktion rollback() { local PREVIOUS_TAG="${1:-$(git describe --tags --abbrev=0 HEAD^)}" warning "Führe Rollback zu $PREVIOUS_TAG durch..." ssh $SSH_OPTS "$SERVER_USER@$SERVER_IP" "cd $REMOTE_PATH && git checkout '$PREVIOUS_TAG' && docker compose restart" if [[ $? -eq 0 ]]; then success "Rollback zu $PREVIOUS_TAG erfolgreich" else error "Rollback fehlgeschlagen - manuelle Intervention erforderlich" fi } # Hauptprogramm main() { echo -e "${BLUE}" echo "🚀 Git-basiertes Framework Deployment" echo "📦 Custom PHP Framework → Production" echo "🔗 Gitea-Repository → $SERVER_IP" echo -e "${RESET}" # Rollback-Option if [[ "$1" == "--rollback" ]]; then rollback "$2" exit 0 fi # Deployment-Pipeline validate_local run_local_tests build_assets create_deployment_tag DEPLOY_TAG=$(cat .deploy-tag) deploy_to_server "$DEPLOY_TAG" validate_deployment echo -e "${GREEN}" echo "🎉 Deployment erfolgreich abgeschlossen!" echo "📋 Tag: $DEPLOY_TAG" echo "🔗 URL: https://$SERVER_IP" echo "📊 Logs: ssh $SERVER_USER@$SERVER_IP 'cd $REMOTE_PATH && docker compose logs'" echo "🔄 Rollback: ./deploy-git.sh --rollback" echo -e "${RESET}" } # Script ausführen main "$@"