Skip to content

Instantly share code, notes, and snippets.

@maljolani
Created June 11, 2024 12:17
Show Gist options
  • Save maljolani/1a47161fa4d1f7171d3df1e3eb5619b9 to your computer and use it in GitHub Desktop.
Save maljolani/1a47161fa4d1f7171d3df1e3eb5619b9 to your computer and use it in GitHub Desktop.
remove_offline_runners.py
import requests
# Constants
GITHUB_TOKEN = ''
ORG_NAME = ''
LABEL_FILTER = ''
# Headers for the API requests
headers = {
'Authorization': f'token {GITHUB_TOKEN}',
'Accept': 'application/vnd.github.v3+json',
}
# Get all runners
def get_runners():
url = f'https://api.github.com/orgs/{ORG_NAME}/actions/runners?qr=label:{LABEL_FILTER}'
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
# Remove a runner
def remove_runner(runner_id):
url = f'https://api.github.com/orgs/{ORG_NAME}/actions/runners/{runner_id}'
response = requests.delete(url, headers=headers)
response.raise_for_status()
return response.status_code
def main():
runners = get_runners()
offline_runners = [runner for runner in runners['runners'] if runner['status'] == 'offline']
count = 0
for runner in offline_runners:
# if count >= 200:
# break
print(f'Removing runner {runner["id"]}: {runner["name"]}')
status_code = remove_runner(runner['id'])
if status_code == 204:
print(f'Successfully removed runner {runner["id"]}')
count += 1
else:
print(f'Failed to remove runner {runner["id"]}')
print(f'Total runners removed: {count}')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment