|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Monitor GitHub Actions CI runs until success.""" |
| 3 | +import subprocess |
| 4 | +import json |
| 5 | +import time |
| 6 | +import sys |
| 7 | + |
| 8 | +def get_latest_run_id(): |
| 9 | + """Get latest Code Quality run ID.""" |
| 10 | + try: |
| 11 | + result = subprocess.run( |
| 12 | + ['gh', 'run', 'list', '--workflow=Code Quality', '--limit=1', '--json', 'databaseId'], |
| 13 | + capture_output=True, |
| 14 | + text=True, |
| 15 | + check=True |
| 16 | + ) |
| 17 | + data = json.loads(result.stdout) |
| 18 | + if data: |
| 19 | + return data[0]['databaseId'] |
| 20 | + return None |
| 21 | + except Exception as e: |
| 22 | + print(f"Error getting run ID: {e}") |
| 23 | + return None |
| 24 | + |
| 25 | +def get_run_status(run_id): |
| 26 | + """Get status of a specific run.""" |
| 27 | + try: |
| 28 | + result = subprocess.run( |
| 29 | + ['gh', 'run', 'view', str(run_id), '--json', 'status', 'conclusion'], |
| 30 | + capture_output=True, |
| 31 | + text=True, |
| 32 | + check=True |
| 33 | + ) |
| 34 | + data = json.loads(result.stdout) |
| 35 | + return data.get('status'), data.get('conclusion') |
| 36 | + except Exception as e: |
| 37 | + print(f"Error getting run status: {e}") |
| 38 | + return None, None |
| 39 | + |
| 40 | +def get_job_errors(run_id): |
| 41 | + """Get error logs from failed jobs.""" |
| 42 | + try: |
| 43 | + result = subprocess.run( |
| 44 | + ['gh', 'run', 'view', str(run_id), '--log'], |
| 45 | + capture_output=True, |
| 46 | + text=True, |
| 47 | + check=True |
| 48 | + ) |
| 49 | + logs = result.stdout |
| 50 | + |
| 51 | + # Extract error lines |
| 52 | + errors = [] |
| 53 | + for line in logs.split('\n'): |
| 54 | + if 'Error:' in line or 'Traceback' in line or 'X' in line: |
| 55 | + if 'Checkout code' not in line and 'Set up Python' not in line: |
| 56 | + errors.append(line) |
| 57 | + |
| 58 | + return errors[:20] # Return first 20 errors |
| 59 | + except Exception as e: |
| 60 | + print(f"Error getting job logs: {e}") |
| 61 | + return [] |
| 62 | + |
| 63 | +def monitor_run(run_id, max_wait=1800): |
| 64 | + """Monitor a run until it completes or times out.""" |
| 65 | + print(f"\n{'='*60}") |
| 66 | + print(f"Monitoring Run ID: {run_id}") |
| 67 | + print(f"{'='*60}\n") |
| 68 | + |
| 69 | + start_time = time.time() |
| 70 | + check_interval = 30 # Check every 30 seconds |
| 71 | + |
| 72 | + while True: |
| 73 | + elapsed = time.time() - start_time |
| 74 | + if elapsed > max_wait: |
| 75 | + print(f"\nTimeout after {max_wait} seconds") |
| 76 | + return False |
| 77 | + |
| 78 | + status, conclusion = get_run_status(run_id) |
| 79 | + |
| 80 | + if status is None: |
| 81 | + print(f"Unable to get status, retrying...") |
| 82 | + time.sleep(check_interval) |
| 83 | + continue |
| 84 | + |
| 85 | + if status == 'queued': |
| 86 | + print(f"[QUEUED] ({int(elapsed)}s elapsed)") |
| 87 | + elif status == 'in_progress': |
| 88 | + print(f"[IN PROGRESS] ({int(elapsed)}s elapsed)") |
| 89 | + elif status == 'completed': |
| 90 | + print(f"\nRun completed! Status: {status}, Conclusion: {conclusion}") |
| 91 | + |
| 92 | + if conclusion == 'success': |
| 93 | + print("\n*** All CI checks passed! ***") |
| 94 | + return True |
| 95 | + else: |
| 96 | + print(f"\nRun failed with conclusion: {conclusion}") |
| 97 | + errors = get_job_errors(run_id) |
| 98 | + if errors: |
| 99 | + print("\nErrors found:") |
| 100 | + for i, error in enumerate(errors, 1): |
| 101 | + print(f"{i}. {error}") |
| 102 | + return False |
| 103 | + |
| 104 | + time.sleep(check_interval) |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + print("GitHub Actions CI Monitor") |
| 108 | + print("="*60) |
| 109 | + |
| 110 | + # Get latest run ID |
| 111 | + run_id = get_latest_run_id() |
| 112 | + |
| 113 | + if not run_id: |
| 114 | + print("Could not find any Code Quality runs") |
| 115 | + sys.exit(1) |
| 116 | + |
| 117 | + # Check if run is already completed |
| 118 | + status, conclusion = get_run_status(run_id) |
| 119 | + if status == 'completed': |
| 120 | + print(f"\nLatest run ({run_id}) already completed") |
| 121 | + print(f"Status: {status}, Conclusion: {conclusion}") |
| 122 | + if conclusion == 'success': |
| 123 | + print("\nAll checks passed!") |
| 124 | + sys.exit(0) |
| 125 | + else: |
| 126 | + print("\nRun failed") |
| 127 | + errors = get_job_errors(run_id) |
| 128 | + if errors: |
| 129 | + print("\nErrors found:") |
| 130 | + for i, error in enumerate(errors, 1): |
| 131 | + print(f"{i}. {error}") |
| 132 | + sys.exit(1) |
| 133 | + |
| 134 | + # Monitor the run |
| 135 | + success = monitor_run(run_id) |
| 136 | + |
| 137 | + sys.exit(0 if success else 1) |
0 commit comments