Created
April 30, 2025 17:56
-
-
Save Richard-Barrett/c104a8eec61496a2b74645a1aa237996 to your computer and use it in GitHub Desktop.
List GitHub Runners By Organization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import requests | |
| import argparse | |
| import json | |
| import csv | |
| import sys | |
| def get_org_runners(org, api_url, headers): | |
| url = f"{api_url}/orgs/{org}/actions/runners" | |
| response = requests.get(url, headers=headers) | |
| if response.status_code == 404: | |
| print(f"⚠️ No runners or org '{org}' not found.") | |
| return [] | |
| response.raise_for_status() | |
| return response.json().get("runners", []) | |
| def main(): | |
| parser = argparse.ArgumentParser(description="List GitHub Actions self-hosted runners for organizations.") | |
| parser.add_argument("--token", required=True, help="GitHub Personal Access Token") | |
| parser.add_argument("--orgs", required=True, help="Comma-separated list of orgs (e.g., impinj,impinj-infra)") | |
| parser.add_argument("--output", choices=["json", "csv"], default="json", help="Output format (default: json)") | |
| parser.add_argument("--api-url", default="https://api.github.com", help="GitHub API URL") | |
| args = parser.parse_args() | |
| headers = { | |
| "Authorization": f"token {args.token}", | |
| "Accept": "application/vnd.github+json" | |
| } | |
| all_data = [] | |
| org_list = [o.strip() for o in args.orgs.split(",")] | |
| for org in org_list: | |
| try: | |
| runners = get_org_runners(org, args.api_url, headers) | |
| except requests.HTTPError as e: | |
| print(f"❌ Failed to fetch runners for org {org}: {e}") | |
| continue | |
| for runner in runners: | |
| all_data.append({ | |
| "organization": org, | |
| "runner_id": runner["id"], | |
| "name": runner["name"], | |
| "os": runner["os"], | |
| "status": runner["status"], | |
| "labels": ",".join(label["name"] for label in runner.get("labels", [])) | |
| }) | |
| if args.output == "csv": | |
| csv_file = "github_org_runners.csv" | |
| with open(csv_file, mode="w", newline="", encoding="utf-8") as f: | |
| writer = csv.DictWriter(f, fieldnames=["organization", "runner_id", "name", "os", "status", "labels"]) | |
| writer.writeheader() | |
| writer.writerows(all_data) | |
| print(f"📄 CSV written to: {csv_file}") | |
| else: | |
| print(json.dumps(all_data, indent=2)) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment