|
| 1 | +name: Monitor Self-Hosted Runners |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: "0 1 * * 1" # every Monday at 1AM UTC |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +permissions: |
| 9 | + actions: read # Required to read artifacts |
| 10 | + |
| 11 | +jobs: |
| 12 | + check: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + |
| 15 | + steps: |
| 16 | + - name: Get list of runners |
| 17 | + id: runners |
| 18 | + run: | |
| 19 | + # Try to get repo-level runners first |
| 20 | + curl -s \ |
| 21 | + -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ |
| 22 | + https://api.github.com/repos/${{ github.repository }}/actions/runners \ |
| 23 | + > repo_runners.json |
| 24 | + |
| 25 | + # Also get org-level runners |
| 26 | + curl -s \ |
| 27 | + -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ |
| 28 | + https://api.github.com/orgs/${{ github.repository_owner }}/actions/runners \ |
| 29 | + > org_runners.json |
| 30 | + |
| 31 | + # Combine and deduplicate runner names |
| 32 | + repo_names=$(jq -r '.runners[]?.name // empty' repo_runners.json 2>/dev/null || echo "") |
| 33 | + org_names=$(jq -r '.runners[]?.name // empty' org_runners.json 2>/dev/null || echo "") |
| 34 | + |
| 35 | + # Fallback to known runner list if API fails |
| 36 | + known_runners="xsjevo04" |
| 37 | + |
| 38 | + if [ -n "$repo_names" ] || [ -n "$org_names" ]; then |
| 39 | + all_names=$(echo -e "$repo_names\n$org_names" | sort -u | tr '\n' ',' | sed 's/,$//') |
| 40 | + echo "names=$all_names" >> $GITHUB_OUTPUT |
| 41 | + echo "Using API discovered runners: $all_names" |
| 42 | + else |
| 43 | + echo "names=$known_runners" >> $GITHUB_OUTPUT |
| 44 | + echo "Using fallback runner list: $known_runners" |
| 45 | + fi |
| 46 | +
|
| 47 | + - name: Get artifacts |
| 48 | + id: artifacts |
| 49 | + run: | |
| 50 | + curl -s \ |
| 51 | + -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ |
| 52 | + https://api.github.com/repos/${{ github.repository }}/actions/artifacts \ |
| 53 | + > artifacts.json |
| 54 | +
|
| 55 | + - name: Check each runner heartbeat |
| 56 | + id: check |
| 57 | + run: | |
| 58 | + missing="" |
| 59 | + now=$(date -u +%s) |
| 60 | +
|
| 61 | + for r in $(echo "${{ steps.runners.outputs.names }}" | tr ',' ' '); do |
| 62 | + [ -z "$r" ] && continue |
| 63 | + echo "Checking $r" |
| 64 | +
|
| 65 | + ts=$(jq -r --arg r "$r" '[.artifacts[] | select(.name=="heartbeat-"+$r)] | sort_by(.updated_at) | last | .updated_at // empty' artifacts.json) |
| 66 | +
|
| 67 | + if [ -z "$ts" ] || [ "$ts" == "null" ]; then |
| 68 | + echo "No heartbeat found for $r" |
| 69 | + missing="$missing $r" |
| 70 | + continue |
| 71 | + fi |
| 72 | +
|
| 73 | + hb=$(date -d "$ts" +%s) |
| 74 | + diff=$((now - hb)) |
| 75 | +
|
| 76 | + if [ $diff -gt 691200 ]; then # 8 days (weekly + 1 day buffer) |
| 77 | + echo "Heartbeat stale for $r (last seen: $ts)" |
| 78 | + missing="$missing $r" |
| 79 | + fi |
| 80 | + done |
| 81 | +
|
| 82 | + echo "missing=$missing" >> $GITHUB_OUTPUT |
| 83 | +
|
| 84 | + - name: Fail if any runner missing |
| 85 | + if: steps.check.outputs.missing != '' |
| 86 | + run: | |
| 87 | + echo "Missing or stale runners: ${{ steps.check.outputs.missing }}" |
| 88 | + exit 1 |
| 89 | +
|
| 90 | + - name: Send Teams alert via Power Automate |
| 91 | + if: failure() |
| 92 | + run: | |
| 93 | + echo "Sending Teams alert via Power Automate..." |
| 94 | + response=$(curl -s -w "%{http_code}" -X POST \ |
| 95 | + -H "Content-Type: application/json" \ |
| 96 | + -d '{ |
| 97 | + "type": "AdaptiveCard", |
| 98 | + "version": "1.3", |
| 99 | + "body": [ |
| 100 | + { |
| 101 | + "type": "TextBlock", |
| 102 | + "text": "⚠️ Self-hosted Runner Alert", |
| 103 | + "weight": "bolder", |
| 104 | + "size": "medium", |
| 105 | + "color": "attention" |
| 106 | + }, |
| 107 | + { |
| 108 | + "type": "TextBlock", |
| 109 | + "text": "The following runners are offline or missing heartbeats:", |
| 110 | + "wrap": true |
| 111 | + }, |
| 112 | + { |
| 113 | + "type": "TextBlock", |
| 114 | + "text": "${{ steps.check.outputs.missing }}", |
| 115 | + "wrap": true, |
| 116 | + "fontType": "monospace" |
| 117 | + } |
| 118 | + ] |
| 119 | + }' \ |
| 120 | + "${{ secrets.TEAMS_WEBHOOK_URL }}") |
| 121 | + |
| 122 | + http_code="${response: -3}" |
| 123 | + response_body="${response%???}" |
| 124 | + |
| 125 | + echo "HTTP Status: $http_code" |
| 126 | + echo "Response: $response_body" |
| 127 | + |
| 128 | + |
| 129 | + if [ "$http_code" != "200" ] && [ "$http_code" != "202" ]; then |
| 130 | + echo "Power Automate webhook failed with status $http_code" |
| 131 | + else |
| 132 | + echo "Teams alert sent successfully via Power Automate" |
| 133 | + fi |
0 commit comments