Skip to content

Instantly share code, notes, and snippets.

@btsuhako
Created November 20, 2023 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btsuhako/8dbaee9778b8c324673abe558a600842 to your computer and use it in GitHub Desktop.
Save btsuhako/8dbaee9778b8c324673abe558a600842 to your computer and use it in GitHub Desktop.
Python script to clean up GHA runners in org
import requests
import os
GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
API_URL = 'https://api.github.com/orgs/{org}/actions/runners'
def get_all_runners(org):
runners = []
page = 1
while True:
response = requests.get(API_URL.format(org=org) + f'?per_page=100&page={page}', headers={'Authorization': f'token {GITHUB_TOKEN}'})
if response.status_code == 200:
runners.extend(response.json()['runners'])
if 'next' in response.links:
page += 1
else:
break
else:
break
return runners
def remove_runner(org, runner_id):
response = requests.delete(API_URL.format(org=org) + f'/{runner_id}', headers={'Authorization': f'token {GITHUB_TOKEN}'})
if response.status_code == 204:
print(f'Successfully removed runner {runner_id}')
else:
print(f'Failed to remove runner {runner_id}')
org = 'your-github-org'
runners = get_all_runners(org)
for runner in runners:
if runner['status'] == 'offline':
remove_runner(org, runner['id'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment