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" # Create a temporary directory for files to sync mkdir -p /tmp/klz-deploy cp "$COMPOSE_FILE" /tmp/klz-deploy/ # Check if varnish directory exists and copy it if [ -d "varnish" ]; then echo "Syncing varnish directory..." cp -r varnish /tmp/klz-deploy/ fi # Use tar to bundle files and send them via SSH in a single connection tar czf - -C /tmp/klz-deploy . | \ 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 }} NEXT_PUBLIC_UMAMI_WEBSITE_ID: ${{ secrets.NEXT_PUBLIC_UMAMI_WEBSITE_ID }} NEXT_PUBLIC_UMAMI_SCRIPT_URL: ${{ secrets.NEXT_PUBLIC_UMAMI_SCRIPT_URL }} SENTRY_DSN: ${{ secrets.SENTRY_DSN }} REDIS_URL: ${{ secrets.REDIS_URL }} REDIS_KEY_PREFIX: ${{ secrets.REDIS_KEY_PREFIX }} run: | echo "Starting deployment on server..." # Create deployment script on server cat > /tmp/deploy.sh << 'DEPLOY_EOF' #!/bin/bash set -e echo '=== Starting deployment ===' cd /home/deploy/sites/klz-cables.com echo '=== Creating .env ===' cat > .env << EOF NEXT_PUBLIC_UMAMI_WEBSITE_ID=${NEXT_PUBLIC_UMAMI_WEBSITE_ID} NEXT_PUBLIC_UMAMI_SCRIPT_URL=${NEXT_PUBLIC_UMAMI_SCRIPT_URL} SENTRY_DSN=${SENTRY_DSN} REDIS_URL=${REDIS_URL} REDIS_KEY_PREFIX=${REDIS_KEY_PREFIX} EOF echo '=== Logging into Docker registry ===' echo '${REGISTRY_PASS}' | docker login registry.infra.mintel.me -u '${REGISTRY_USER}' --password-stdin if [ $? -eq 0 ]; then echo '✓ Registry login successful' else echo '✗ Registry login failed' exit 1 fi echo '=== Checking if infra network exists ===' if ! docker network inspect infra >/dev/null 2>&1; then echo 'Creating infra network...' docker network create infra || true 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 (30 seconds) ===' sleep 30 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 15 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 ===' DEPLOY_EOF # Make script executable and copy to server chmod +x /tmp/deploy.sh # Copy deployment script to server with keep-alive and timeout scp -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no -o IPQoS=0x00 -o ServerAliveInterval=30 -o ServerAliveCountMax=3 -o ConnectTimeout=30 /tmp/deploy.sh deploy@alpha.mintel.me:/tmp/deploy.sh # Execute deployment script on server with keep-alive and timeout # Retry up to 3 times if connection is lost for i in {1..3}; do echo "Attempt $i of 3..." if ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no -o IPQoS=0x00 -o ServerAliveInterval=30 -o ServerAliveCountMax=3 -o ConnectTimeout=30 -o ServerAliveTimeout=300 deploy@alpha.mintel.me " set -e bash /tmp/deploy.sh "; then echo "✓ Deployment successful on attempt $i" break else echo "✗ Deployment failed on attempt $i" if [ $i -eq 3 ]; then echo "All deployment attempts failed" exit 1 fi echo "Retrying in 10 seconds..." sleep 10 fi done echo "Deployment completed"