ci: delete cache-cleaning workflow (#2202)

This commit is contained in:
Clement Tsang
2026-08-17 23:24:09 -04:00
committed by GitHub
parent bb741a81da
commit f6ccfe6417
2 changed files with 0 additions and 147 deletions
@@ -1,49 +0,0 @@
# Simple job to clear the cache used by a workflow.
name: "clear workflow cache"
on:
workflow_dispatch:
inputs:
id:
description: "Which id to clear. Type main/master/all to clean all, and keep-main/keep-master to clean all but the main branch."
required: false
schedule:
- cron: "0 11 * * 0"
permissions: {}
jobs:
clear-cache:
permissions:
actions: write
if: ${{ github.event_name != 'pull_request' || ! github.event.pull_request.head.repo.fork }} # If it is a PR, only if not a fork
runs-on: ubuntu-24.04
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
fetch-depth: 1
persist-credentials: false
# We run each script twice with a small delay in between to try and catch everything.
- name: Clear cache
run: |
if [[ -n "${GITHUB_EVENT_SCHEDULE}" ]]; then
python ./scripts/clear_cache.py keep-main
sleep 5
python ./scripts/clear_cache.py keep-main
elif [[ -z "${GITHUB_EVENT_INPUTS_ID}" ]]; then
python ./scripts/clear_cache.py ${{ github.event.pull_request.number }}
sleep 5
python ./scripts/clear_cache.py ${{ github.event.pull_request.number }}
else
python ./scripts/clear_cache.py ${GITHUB_EVENT_INPUTS_ID}
sleep 5
python ./scripts/clear_cache.py ${GITHUB_EVENT_INPUTS_ID}
fi
env:
GITHUB_EVENT_SCHEDULE: ${{ github.event.schedule }}
GITHUB_EVENT_INPUTS_ID: ${{ github.event.inputs.id }}
-98
View File
@@ -1,98 +0,0 @@
#!/bin/python3
# A simple script to clean caches matching a PR ID.
#
# Expects a GitHub token in the environment variables as GITHUB_TOKEN.
import json
import os
import sys
import time
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen
URL = "https://api.github.com/repos/ClementTsang/bottom/actions/caches"
def cache_list_request(key):
request = Request(URL, method="GET")
request.add_header("Accept", "application/vnd.github+json")
request.add_header("Authorization", "Bearer {}".format(key))
return request
def delete_cache_request(key, id):
request = Request("{}/{}".format(URL, id), method="DELETE")
request.add_header("Accept", "application/vnd.github+json")
request.add_header("Authorization", "Bearer {}".format(key))
return request
def main():
args = sys.argv
env = os.environ
key = env["GITHUB_TOKEN"]
if args[1].isnumeric():
pr_id = int(args[1])
ref = "refs/pull/{}/merge".format(pr_id)
print("Clearing any caches generated by PR {}".format(pr_id))
with urlopen(cache_list_request(key)) as response:
response = json.load(response)
caches = response["actions_caches"]
for cache in caches:
if cache["ref"] == ref:
id = cache["id"]
try:
print("Deleting ID {}...".format(id))
urlopen(delete_cache_request(key, id))
except HTTPError as e:
print("HTTPError with delete, error code {}.".format(e.code))
except URLError as _:
print("URLError with delete.")
else:
print("Successfully deleted cache ID {}!".format(id))
time.sleep(0.1)
elif args[1] == "keep-main" or args[1] == "keep-master":
print("Clearing all but default branch cache.")
with urlopen(cache_list_request(key)) as response:
response = json.load(response)
caches = response["actions_caches"]
for cache in caches:
if not ("master" in cache["ref"] or "main" in cache["ref"]):
id = cache["id"]
try:
print("Deleting ID {}...".format(id))
urlopen(delete_cache_request(key, id))
except HTTPError as e:
print("HTTPError with delete, error code {}.".format(e.code))
except URLError as _:
print("URLError with delete.")
else:
print("Successfully deleted cache ID {}!".format(id))
time.sleep(0.1)
elif args[1] == "main" or args[1] == "master" or args[1] == "all":
print("Clearing all caches.")
with urlopen(cache_list_request(key)) as response:
response = json.load(response)
caches = response["actions_caches"]
for cache in caches:
id = cache["id"]
try:
print("Deleting ID {}...".format(id))
urlopen(delete_cache_request(key, id))
except HTTPError as e:
print("HTTPError with delete, error code {}.".format(e.code))
except URLError as _:
print("URLError with delete.")
else:
print("Successfully deleted cache ID {}!".format(id))
time.sleep(0.1)
else:
print(f"Skipping, given argument {args[1]}.")
if __name__ == "__main__":
main()