# Nightly disk cleanup for self-hosted macOS CI runners.
# Runs at 4:00 AM PST to prevent disk exhaustion from CI workloads.
# After cleanup, reboots runners, then a verification job checks disk space.
# Safe to run manually via workflow_dispatch for testing.
#
# NOTE: Runners must have the GitHub Actions runner service configured as a
# LaunchDaemon (or equivalent) so that it auto-starts after reboot.
# Without this, the verify job will hang waiting for a runner that never
# re-registers with GitHub Actions.

name: Nightly Runner Cleanup

on:
  schedule:
    # 4:00 AM PST = 12:00 UTC (PST is UTC-8)
    - cron: "0 12 * * *"
  workflow_dispatch:

jobs:
  cleanup:
    strategy:
      fail-fast: false
      matrix:
        runner: [ci1, ci2, ci3]
    runs-on: ${{ matrix.runner }}
    steps:
      - name: Checkout (for cleanup script)
        uses: actions/checkout@v4

      - name: Nightly disk cleanup
        env:
          CI_NIGHTLY_CLEANUP: "1"
        run: bash scripts/ci-cleanup-macos.sh

      - name: Reboot runner
        run: sudo reboot

  verify:
    needs: cleanup
    # Run even if cleanup "fails" (reboot kills the runner mid-job), but still
    # respect manual workflow cancellation.
    if: ${{ !cancelled() }}
    timeout-minutes: 15
    strategy:
      fail-fast: false
      matrix:
        runner: [ci1, ci2, ci3]
    runs-on: ${{ matrix.runner }}
    steps:
      - name: Print available disk space
        run: |
          echo "=== Post-Reboot Disk Space ==="
          df -h /
          echo ""
          echo "Available space: $(df -h / | tail -1 | awk '{print $4}')"
