# CD Pipeline: Auto-deploy to Mac Mini on push to main # # Requires a self-hosted GitHub Actions runner on the Mac Mini. # Setup: see docs/MAC_MINI_RUNNER_SETUP.md # # Flow: # Push → main : Detects changed services → rebuilds & restarts only those containers # # The runner executes directly on the Mac Mini, so it has access to # Docker, docker-compose, and the local project directory. name: CD Mac Mini on: push: branches: - main workflow_dispatch: inputs: service: description: 'Service to deploy (or "all" for everything)' required: false default: 'all' type: choice options: - all - matrix-web - mana-core-auth - chat-backend - chat-web - todo-backend - todo-web - calendar-backend - calendar-web - clock-backend - clock-web - contacts-backend - contacts-web - matrix-mana-bot concurrency: group: cd-macmini cancel-in-progress: false # Don't cancel in-progress deploys env: PROJECT_DIR: /Users/mana/projects/manacore-monorepo COMPOSE_FILE: docker-compose.macmini.yml ENV_FILE: .env.macmini PATH: /usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin jobs: # =========================================== # Detect what changed # =========================================== detect-changes: name: Detect Changes runs-on: self-hosted if: github.event_name == 'push' outputs: matrix-web: ${{ steps.changes.outputs.matrix-web }} mana-core-auth: ${{ steps.changes.outputs.mana-core-auth }} chat-backend: ${{ steps.changes.outputs.chat-backend }} chat-web: ${{ steps.changes.outputs.chat-web }} todo-backend: ${{ steps.changes.outputs.todo-backend }} todo-web: ${{ steps.changes.outputs.todo-web }} calendar-backend: ${{ steps.changes.outputs.calendar-backend }} calendar-web: ${{ steps.changes.outputs.calendar-web }} clock-backend: ${{ steps.changes.outputs.clock-backend }} clock-web: ${{ steps.changes.outputs.clock-web }} contacts-backend: ${{ steps.changes.outputs.contacts-backend }} contacts-web: ${{ steps.changes.outputs.contacts-web }} matrix-mana-bot: ${{ steps.changes.outputs.matrix-mana-bot }} any-changes: ${{ steps.changes.outputs.any-changes }} steps: - name: Check for changes id: changes run: | cd "${{ env.PROJECT_DIR }}" # Get changed files between previous and current commit CHANGED=$(git diff --name-only HEAD~1 HEAD 2>/dev/null || echo "") # Shared packages trigger rebuilds for all services that use them SHARED_CHANGED="false" if echo "$CHANGED" | grep -qE "^packages/(shared-ui|shared-theme|shared-icons|shared-tailwind|shared-auth|shared-branding|shared-i18n|shared-utils|shared-types)/"; then SHARED_CHANGED="true" fi check_changes() { local name=$1 shift local result="false" for path in "$@"; do if echo "$CHANGED" | grep -q "^$path"; then result="true" break fi done # Shared package changes trigger rebuild if [ "$SHARED_CHANGED" == "true" ]; then result="true" fi echo "$name=$result" >> $GITHUB_OUTPUT echo " $name: $result" } echo "Changed files:" echo "$CHANGED" | head -20 echo "" echo "Shared packages changed: $SHARED_CHANGED" echo "" check_changes "matrix-web" "apps/matrix/apps/web/" "apps/matrix/packages/" check_changes "mana-core-auth" "services/mana-core-auth/" check_changes "chat-backend" "apps/chat/apps/backend/" "apps/chat/packages/" check_changes "chat-web" "apps/chat/apps/web/" "apps/chat/packages/" check_changes "todo-backend" "apps/todo/apps/backend/" "apps/todo/packages/" check_changes "todo-web" "apps/todo/apps/web/" "apps/todo/packages/" check_changes "calendar-backend" "apps/calendar/apps/backend/" "apps/calendar/packages/" check_changes "calendar-web" "apps/calendar/apps/web/" "apps/calendar/packages/" check_changes "clock-backend" "apps/clock/apps/backend/" "apps/clock/packages/" check_changes "clock-web" "apps/clock/apps/web/" "apps/clock/packages/" check_changes "contacts-backend" "apps/contacts/apps/backend/" "apps/contacts/packages/" check_changes "contacts-web" "apps/contacts/apps/web/" "apps/contacts/packages/" check_changes "matrix-mana-bot" "services/matrix-mana-bot/" "packages/matrix-bot-common/" # Check if anything needs deploying ANY="false" for svc in matrix-web mana-core-auth chat-backend chat-web todo-backend todo-web calendar-backend calendar-web clock-backend clock-web contacts-backend contacts-web matrix-mana-bot; do val=$(grep "^$svc=" $GITHUB_OUTPUT | tail -1 | cut -d= -f2) if [ "$val" == "true" ]; then ANY="true" break fi done echo "any-changes=$ANY" >> $GITHUB_OUTPUT # =========================================== # Deploy changed services # =========================================== deploy: name: Deploy runs-on: self-hosted needs: [detect-changes] if: | always() && (needs.detect-changes.result == 'success' && needs.detect-changes.outputs.any-changes == 'true') || github.event_name == 'workflow_dispatch' steps: - name: Pull latest code run: | cd "${{ env.PROJECT_DIR }}" git pull origin main - name: Init deploy tracking id: init run: | cd "${{ env.PROJECT_DIR }}" source scripts/deploy-metrics.sh deploy_timer_start echo "start_epoch=$DEPLOY_START_EPOCH" >> $GITHUB_OUTPUT ensure_deploy_schema - name: Ensure env vars exist run: | cd "${{ env.PROJECT_DIR }}" # Add CALENDAR_ENCRYPTION_KEY if not present if ! grep -q "CALENDAR_ENCRYPTION_KEY" "${{ env.ENV_FILE }}" 2>/dev/null; then echo "CALENDAR_ENCRYPTION_KEY=$(openssl rand -base64 32)" >> "${{ env.ENV_FILE }}" echo "Added CALENDAR_ENCRYPTION_KEY to ${{ env.ENV_FILE }}" fi - name: Determine services to deploy id: services run: | cd "${{ env.PROJECT_DIR }}" SERVICES="" if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then INPUT="${{ inputs.service }}" if [ "$INPUT" == "all" ]; then echo "Manual deploy: all services" echo "deploy-all=true" >> $GITHUB_OUTPUT exit 0 else SERVICES="$INPUT" fi else # Build list from detected changes if [ "${{ needs.detect-changes.outputs.matrix-web }}" == "true" ]; then SERVICES="$SERVICES matrix-web"; fi if [ "${{ needs.detect-changes.outputs.mana-core-auth }}" == "true" ]; then SERVICES="$SERVICES mana-auth"; fi if [ "${{ needs.detect-changes.outputs.chat-backend }}" == "true" ]; then SERVICES="$SERVICES chat-backend"; fi if [ "${{ needs.detect-changes.outputs.chat-web }}" == "true" ]; then SERVICES="$SERVICES chat-web"; fi if [ "${{ needs.detect-changes.outputs.todo-backend }}" == "true" ]; then SERVICES="$SERVICES todo-backend"; fi if [ "${{ needs.detect-changes.outputs.todo-web }}" == "true" ]; then SERVICES="$SERVICES todo-web"; fi if [ "${{ needs.detect-changes.outputs.calendar-backend }}" == "true" ]; then SERVICES="$SERVICES calendar-backend"; fi if [ "${{ needs.detect-changes.outputs.calendar-web }}" == "true" ]; then SERVICES="$SERVICES calendar-web"; fi if [ "${{ needs.detect-changes.outputs.clock-backend }}" == "true" ]; then SERVICES="$SERVICES clock-backend"; fi if [ "${{ needs.detect-changes.outputs.clock-web }}" == "true" ]; then SERVICES="$SERVICES clock-web"; fi if [ "${{ needs.detect-changes.outputs.contacts-backend }}" == "true" ]; then SERVICES="$SERVICES contacts-backend"; fi if [ "${{ needs.detect-changes.outputs.contacts-web }}" == "true" ]; then SERVICES="$SERVICES contacts-web"; fi if [ "${{ needs.detect-changes.outputs.matrix-mana-bot }}" == "true" ]; then SERVICES="$SERVICES matrix-mana-bot"; fi fi echo "services=$SERVICES" >> $GITHUB_OUTPUT echo "deploy-all=false" >> $GITHUB_OUTPUT echo "Services to deploy: $SERVICES" - name: Build and deploy services id: build run: | cd "${{ env.PROJECT_DIR }}" source scripts/deploy-metrics.sh DEPLOY_ALL="${{ steps.services.outputs.deploy-all }}" SERVICES="${{ steps.services.outputs.services }}" # Determine final service list if [ "$DEPLOY_ALL" == "true" ]; then # Get all service names from compose file SERVICES=$(docker compose -f "$COMPOSE_FILE" --env-file "$ENV_FILE" config --services | tr '\n' ' ') echo "=== Rebuilding ALL services ===" elif [ -z "$SERVICES" ]; then echo "No services to deploy" echo "build-times=" >> $GITHUB_OUTPUT exit 0 else echo "=== Rebuilding: $SERVICES ===" fi # Build each service individually to capture build times BUILD_TIMES="" for svc in $SERVICES; do echo "--- Building $svc ---" build_start=$(date +%s) docker compose -f "$COMPOSE_FILE" --env-file "$ENV_FILE" build "$svc" 2>&1 || true build_end=$(date +%s) build_dur=$(( build_end - build_start )) BUILD_TIMES="$BUILD_TIMES $svc:$build_dur" echo " $svc built in ${build_dur}s" done # Start all services at once (no rebuild, images already built) echo "=== Starting services ===" if [ "$DEPLOY_ALL" == "true" ]; then docker compose -f "$COMPOSE_FILE" --env-file "$ENV_FILE" up -d else docker compose -f "$COMPOSE_FILE" --env-file "$ENV_FILE" up -d --no-deps $SERVICES fi echo "=== Waiting for services to start ===" sleep 10 echo "build-times=$BUILD_TIMES" >> $GITHUB_OUTPUT - name: Health checks id: health run: | cd "${{ env.PROJECT_DIR }}" source scripts/deploy-metrics.sh # Service health URL pairs (Bash 3.x compatible — no declare -A) HEALTH_ENTRIES=" mana-auth|http://localhost:3001/health matrix-web|http://localhost:5180/health chat-backend|http://localhost:3030/health chat-web|http://localhost:5010/health todo-backend|http://localhost:3031/health todo-web|http://localhost:5011/health calendar-backend|http://localhost:3032/health calendar-web|http://localhost:5012/health clock-backend|http://localhost:3033/health clock-web|http://localhost:5013/health contacts-backend|http://localhost:3034/health contacts-web|http://localhost:5014/health " HEALTH_RESULTS="" echo "=== Health Checks ===" for entry in $HEALTH_ENTRIES; do svc="${entry%%|*}" url="${entry#*|}" result=$(check_health_timed "$svc" "$url" 2>/dev/null) || true status=$(echo "$result" | awk '{print $1}') elapsed=$(echo "$result" | awk '{print $2}') http_code=$(echo "$result" | awk '{print $3}') if [ -z "$status" ]; then status="skipped" elapsed="0" http_code="0" fi if [ "$status" = "ok" ]; then echo " ✓ $svc: OK (${elapsed}s)" else echo " ✗ $svc: $status (HTTP $http_code, ${elapsed}s)" fi HEALTH_RESULTS="$HEALTH_RESULTS $svc:$status:$http_code:$elapsed" done echo "health-results=$HEALTH_RESULTS" >> $GITHUB_OUTPUT - name: Record deploy metrics if: always() run: | cd "${{ env.PROJECT_DIR }}" source scripts/deploy-metrics.sh START_EPOCH="${{ steps.init.outputs.start_epoch }}" NOW=$(date +%s) DURATION=$(( NOW - START_EPOCH )) # Determine overall status STATUS="success" if [ "${{ job.status }}" != "success" ]; then STATUS="failure" fi # Determine services list DEPLOY_ALL="${{ steps.services.outputs.deploy-all }}" SERVICES="${{ steps.services.outputs.services }}" if [ "$DEPLOY_ALL" == "true" ]; then SERVICES_CSV="all" else SERVICES_CSV=$(echo "$SERVICES" | tr ' ' ',') fi COMMIT_MSG=$(git log -1 --pretty=%s 2>/dev/null | head -c 200 || echo "unknown") BRANCH="${{ github.ref_name }}" # Insert deployment row DEPLOY_ID=$(insert_deployment \ "${{ github.run_id }}" \ "${{ github.run_attempt }}" \ "${{ github.sha }}" \ "$COMMIT_MSG" \ "$BRANCH" \ "${{ github.event_name }}" \ "${{ github.actor }}" \ "$SERVICES_CSV" \ "$STATUS" 2>/dev/null) || DEPLOY_ID="" if [ -n "$DEPLOY_ID" ]; then # Finalise with duration finalise_deployment "$DEPLOY_ID" "$STATUS" "$DURATION" 2>/dev/null || true # Helper: lookup value from "key:val key2:val2" string # Usage: lookup "key" "key:val key2:val2" [field_index] (default: 2nd field) lookup() { local needle="$1" haystack="$2" field="${3:-2}" for item in $haystack; do if [ "${item%%:*}" = "$needle" ]; then echo "$item" | cut -d: -f"$field" return fi done echo "" } BUILD_TIMES="${{ steps.build.outputs.build-times }}" HEALTH_RESULTS="${{ steps.health.outputs.health-results }}" # Collect unique service names from both build and health data ALL_SVCS=$(echo "$BUILD_TIMES $HEALTH_RESULTS" | tr ' ' '\n' | cut -d: -f1 | sort -u | tr '\n' ' ') for svc in $ALL_SVCS; do [ -z "$svc" ] && continue build_dur=$(lookup "$svc" "$BUILD_TIMES" 2) build_dur="${build_dur:-0}" img_mb=$(get_image_size_mb "$svc" 2>/dev/null || echo "0") startup=$(lookup "$svc" "$HEALTH_RESULTS" 4) startup="${startup:-0}" health=$(lookup "$svc" "$HEALTH_RESULTS" 2) health="${health:-skipped}" http_code=$(lookup "$svc" "$HEALTH_RESULTS" 3) http_code="${http_code:-0}" insert_deploy_service "$DEPLOY_ID" "$svc" "$build_dur" "$img_mb" "$startup" "$health" "$http_code" 2>/dev/null || true push_service_metrics "$svc" "$build_dur" "$img_mb" "$health" 2>/dev/null || true done fi # Push overall metrics to Pushgateway push_deploy_metrics "$STATUS" "$DURATION" "$BRANCH" 2>/dev/null || true echo "Deploy tracking recorded: status=$STATUS duration=${DURATION}s" - name: Summary if: always() run: | cd "${{ env.PROJECT_DIR }}" START_EPOCH="${{ steps.init.outputs.start_epoch }}" NOW=$(date +%s) DURATION=$(( NOW - START_EPOCH )) echo "## Deployment Summary" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY echo "**Trigger:** ${{ github.event_name }}" >> $GITHUB_STEP_SUMMARY echo "**Commit:** \`${{ github.sha }}\`" >> $GITHUB_STEP_SUMMARY echo "**Duration:** ${DURATION}s" >> $GITHUB_STEP_SUMMARY echo "**Status:** ${{ job.status }}" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY if [ "${{ steps.services.outputs.deploy-all }}" == "true" ]; then echo "**Services:** All" >> $GITHUB_STEP_SUMMARY else echo "**Services:** ${{ steps.services.outputs.services }}" >> $GITHUB_STEP_SUMMARY fi # Build times table BUILD_TIMES="${{ steps.build.outputs.build-times }}" if [ -n "$BUILD_TIMES" ]; then echo "" >> $GITHUB_STEP_SUMMARY echo "### Build Times" >> $GITHUB_STEP_SUMMARY echo "| Service | Duration |" >> $GITHUB_STEP_SUMMARY echo "|---------|----------|" >> $GITHUB_STEP_SUMMARY for entry in $BUILD_TIMES; do svc="${entry%%:*}" dur="${entry#*:}" echo "| $svc | ${dur}s |" >> $GITHUB_STEP_SUMMARY done fi # Health results table HEALTH_RESULTS="${{ steps.health.outputs.health-results }}" if [ -n "$HEALTH_RESULTS" ]; then echo "" >> $GITHUB_STEP_SUMMARY echo "### Health Checks" >> $GITHUB_STEP_SUMMARY echo "| Service | Status | HTTP | Startup |" >> $GITHUB_STEP_SUMMARY echo "|---------|--------|------|---------|" >> $GITHUB_STEP_SUMMARY for entry in $HEALTH_RESULTS; do svc=$(echo "$entry" | cut -d: -f1) h_status=$(echo "$entry" | cut -d: -f2) h_code=$(echo "$entry" | cut -d: -f3) h_time=$(echo "$entry" | cut -d: -f4) icon="✓" [ "$h_status" != "ok" ] && icon="✗" echo "| $svc | $icon $h_status | $h_code | ${h_time}s |" >> $GITHUB_STEP_SUMMARY done fi