|
| 1 | +name: Monitor & Rerun stuck CodeQL |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: '*/10 * * * *' # every 10 minutes |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +permissions: |
| 9 | + actions: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + monitor: |
| 13 | + name: Monitor CodeQL runs and rerun stuck jobs |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - name: Setup jq |
| 17 | + run: | |
| 18 | + sudo apt-get update -y |
| 19 | + sudo apt-get install -y jq |
| 20 | +
|
| 21 | + - name: Find CodeQL workflow and rerun stuck runs |
| 22 | + env: |
| 23 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 24 | + THRESHOLD_MINUTES: 15 |
| 25 | + TARGET_WORKFLOW_NAME: "CodeQL" |
| 26 | + TARGET_BRANCH: "automation/bot-auto-pr" |
| 27 | + run: | |
| 28 | + set -euo pipefail |
| 29 | + API="https://api.github.com/repos/${GITHUB_REPOSITORY}" |
| 30 | +
|
| 31 | + echo "Looking up workflows..." |
| 32 | + wf_list=$(curl -sS -H "Authorization: token ${GITHUB_TOKEN}" "${API}/actions/workflows") |
| 33 | + workflow_id=$(echo "$wf_list" | jq -r --arg NAME "$TARGET_WORKFLOW_NAME" '.workflows[] | select(.name == $NAME) | .id' | head -n 1) |
| 34 | + if [ -z "$workflow_id" ] || [ "$workflow_id" = "null" ]; then |
| 35 | + echo "Workflow '$TARGET_WORKFLOW_NAME' not found in repo." |
| 36 | + exit 0 |
| 37 | + fi |
| 38 | +
|
| 39 | + echo "Workflow id: $workflow_id" |
| 40 | + now_epoch=$(date -u +%s) |
| 41 | + threshold_seconds=$(( THRESHOLD_MINUTES * 60 )) |
| 42 | +
|
| 43 | + echo "Listing in_progress and queued runs for branch $TARGET_BRANCH..." |
| 44 | + runs=$(curl -sS -H "Authorization: token ${GITHUB_TOKEN}" "${API}/actions/workflows/${workflow_id}/runs?branch=${TARGET_BRANCH}&status=in_progress") |
| 45 | + runs_queued=$(curl -sS -H "Authorization: token ${GITHUB_TOKEN}" "${API}/actions/workflows/${workflow_id}/runs?branch=${TARGET_BRANCH}&status=queued") |
| 46 | + all_runs=$(jq -s '[.[0].workflow_runs[], .[1].workflow_runs[]] | unique_by(.id)' <(echo "$runs") <(echo "$runs_queued")) |
| 47 | +
|
| 48 | + echo "$all_runs" | jq -c '.[]' | while read -r run; do |
| 49 | + run_id=$(echo "$run" | jq -r '.id') |
| 50 | + created_at=$(echo "$run" | jq -r '.created_at') |
| 51 | + created_epoch=$(date -u -d "$created_at" +%s) |
| 52 | + age=$(( now_epoch - created_epoch )) |
| 53 | + status=$(echo "$run" | jq -r '.status') |
| 54 | + conclusion=$(echo "$run" | jq -r '.conclusion') |
| 55 | +
|
| 56 | + echo "Run $run_id status=$status conclusion=$conclusion age=${age}s" |
| 57 | + if [ "$age" -gt "$threshold_seconds" ]; then |
| 58 | + echo "Run $run_id is older than threshold (${THRESHOLD_MINUTES}m) — attempting rerun..." |
| 59 | + resp=$(curl -sS -X POST -H "Authorization: token ${GITHUB_TOKEN}" "${API}/actions/runs/${run_id}/rerun" -w "%{http_code}" -o /dev/null || echo "err") |
| 60 | + echo "Rerun request response: $resp" |
| 61 | + fi |
| 62 | + done |
| 63 | +
|
0 commit comments