name: Build & Deploy KLZ Cables on: push: branches: - main jobs: deploy: runs-on: docker steps: - name: Checkout repo uses: actions/checkout@v3 - name: Install tools run: | apt-get update apt-get install -y \ docker.io \ openssh-client \ rsync - name: Login to registry env: REGISTRY_USER: ${{ secrets.REGISTRY_USER }} REGISTRY_PASS: ${{ secrets.REGISTRY_PASS }} run: | echo "$REGISTRY_PASS" | DOCKER_API_VERSION=1.44 docker login registry.infra.mintel.me -u "$REGISTRY_USER" --password-stdin - name: Build image run: | DOCKER_API_VERSION=1.44 docker build \ --pull \ --build-arg NEXT_PUBLIC_UMAMI_WEBSITE_ID=${{ secrets.NEXT_PUBLIC_UMAMI_WEBSITE_ID }} \ --build-arg NEXT_PUBLIC_UMAMI_SCRIPT_URL=${{ secrets.NEXT_PUBLIC_UMAMI_SCRIPT_URL }} \ --build-arg NEXT_PUBLIC_SENTRY_DSN=${{ secrets.SENTRY_DSN }} \ -t registry.infra.mintel.me/mintel/klz-cables.com:latest . - name: Push image run: DOCKER_API_VERSION=1.44 docker push registry.infra.mintel.me/mintel/klz-cables.com:latest - name: Setup SSH run: | mkdir -p ~/.ssh printf "%s\n" "${{ secrets.ALPHA_SSH_KEY }}" > ~/.ssh/id_ed25519 chmod 600 ~/.ssh/id_ed25519 ssh-keyscan -H alpha.mintel.me >> ~/.ssh/known_hosts - name: Sync docker-compose file to server run: | echo "Checking for docker-compose file..." COMPOSE_FILE="" if [ -f "docker-compose.yml" ]; then COMPOSE_FILE="docker-compose.yml" elif [ -f "docker-compose.yaml" ]; then COMPOSE_FILE="docker-compose.yaml" else echo "ERROR: Keine docker-compose.yml oder .yaml gefunden!" exit 1 fi echo "Found and syncing: $COMPOSE_FILE" # Use tar to bundle files and send them via SSH in a single connection tar czf - "$COMPOSE_FILE" varnish 2>/dev/null || tar czf - "$COMPOSE_FILE" | \ ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no -o IPQoS=0x00 deploy@alpha.mintel.me \ "mkdir -p /home/deploy/sites/klz-cables.com/ && tar xzf - -C /home/deploy/sites/klz-cables.com/ && echo 'Files synced successfully' && ls -la /home/deploy/sites/klz-cables.com/" echo "File sync completed" - name: Deploy on server env: REGISTRY_USER: ${{ secrets.REGISTRY_USER }} REGISTRY_PASS: ${{ secrets.REGISTRY_PASS }} run: | echo "Starting deployment on server..." # Execute deployment commands directly with proper error handling ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no -o IPQoS=0x00 deploy@alpha.mintel.me " set -e echo '=== Starting deployment ===' cd /home/deploy/sites/klz-cables.com echo '=== Creating .env ===' echo 'SENTRY_DSN=${{ secrets.SENTRY_DSN }}' > .env echo '=== Logging into Docker registry ===' echo '${{ secrets.REGISTRY_PASS }}' | docker login registry.infra.mintel.me -u '${{ secrets.REGISTRY_USER }}' --password-stdin if [ $? -eq 0 ]; then echo '✓ Registry login successful' else echo '✗ Registry login failed' exit 1 fi echo '=== Checking current containers ===' docker compose ps echo '=== Checking current image ===' docker images registry.infra.mintel.me/mintel/klz-cables.com:latest echo '=== Removing local image to force fresh pull ===' docker rmi registry.infra.mintel.me/mintel/klz-cables.com:latest || true echo '=== Pulling latest image ===' docker compose pull echo '✓ Image pull command completed' echo '=== Verifying new image after pull ===' docker images registry.infra.mintel.me/mintel/klz-cables.com:latest echo '=== Checking if image was actually updated ===' if docker inspect registry.infra.mintel.me/mintel/klz-cables.com:latest >/dev/null 2>&1; then echo '✓ Image exists locally' else echo '✗ Image pull failed - image not found locally' exit 1 fi echo '=== Stopping all containers ===' docker compose down echo '✓ Containers stopped' echo '=== Starting all containers ===' docker compose up -d echo '✓ Containers started' echo '=== Waiting for containers to be ready (20 seconds) ===' sleep 20 echo '=== Verifying deployment status ===' docker compose ps echo '=== Checking app container logs ===' docker compose logs --tail=30 app echo '=== Checking varnish container logs ===' docker compose logs --tail=20 varnish echo '=== Verifying application health ===' # Wait a bit more for the app to be fully ready sleep 10 if curl -f -s http://localhost:80/health > /dev/null 2>&1; then echo '✓ Application health check passed' else echo '✗ Application health check failed - checking app logs' docker compose logs --tail=50 app exit 1 fi echo '=== Cleaning up old images ===' docker image prune -f --filter 'until=24h' echo '=== Deployment completed successfully ===' " echo "Deployment completed"