Created
November 20, 2023 19:00
-
-
Save btsuhako/8dbaee9778b8c324673abe558a600842 to your computer and use it in GitHub Desktop.
Python script to clean up GHA runners in org
This file contains 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
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