Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AndrewFarley/b5496dfe09295d86aff90b4c725705a7 to your computer and use it in GitHub Desktop.
Save AndrewFarley/b5496dfe09295d86aff90b4c725705a7 to your computer and use it in GitHub Desktop.
Github Actions API Python script to delete workflows of a specific type
#!/usr/bin/env python3
from ghapi.all import GhApi
import time
import json
api = GhApi()
# If you have rate limit issues, you might need this to check it...
#data = api.rate_limit.get()
#print(data)
#exit(0)
owner = 'github-user-or-org'
repo = 'repo-name'
# Put known-good names of workflows you want to keep here
skip_names = [
"Dev - On Push, Build/Push Docker, Deploy via Helm",
"Dev - After Merge Into Master, Cleanup",
"GM - On Push, Build/Push Docker, Deploy via Helm",
"Live - On v Semver Tag, Deploy via Helm",
]
# Put old/bad names of workflows you want to delete here
delete_names = [
"Build and Publish",
".github/workflows/test.yml",
"Docker Build and Push To ECR",
".github/workflows/dev-dynamic-build-and-deploy.yml",
".github/workflows/dev-docker-build-and-deploy-on-push.yml",
"Build and Publish Artifact",
"Dev - Build, Push to ECR, Deploy to K8s",
"Dev - On Push, Build and Publish Docker, Deploy via Helm",
"Dev - Cleanup After Merge",
".github/workflows/dev-remove-environment.yml",
]
count = 0
deleted = 0
per_page = 100
page = 0
while True:
page = page + 1
print("Page {}".format(page))
runs = api.actions.list_workflow_runs_for_repo(owner, repo, per_page=per_page, page=page)
for run in runs.workflow_runs:
count = count + 1
if run.name in skip_names:
continue
if run.name in delete_names:
print("Deleting {} - {} ...".format(run.id, run.name))
try:
api.actions.delete_workflow_run(owner, repo, run.id)
except:
print("FAILED!!!")
time.sleep(1)
continue
print("Unknown Workflow Add To Vars Above {} - {}".format(run.id, run.name))
if len(runs.workflow_runs) <= per_page:
print("We are done...")
# time.sleep(10)
break
print("Handled {} records...".format(count))
print("Deleted {} records...".format(deleted))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment