-
Notifications
You must be signed in to change notification settings - Fork 836
/
Copy pathworkflow_cleanup.sh
executable file
·62 lines (48 loc) · 1.79 KB
/
workflow_cleanup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash
ORG_NAME=$1
REPO_NAME=$2
WORKFLOW_NAME=$3
if [[ -z $WORKFLOW_NAME || -z $REPO_NAME || -z $ORG_NAME ]]; then
echo "Usage: ./workflow_cleanup.sh <USER/ORG NAME> <REPO NAME> <WORKFLOW NAME>"
exit 1
fi
if ! [ -x "$(command -v gh)" ]; then
echo "Script requires logged-in gh CLI user."
echo "Install gh CLI according to your OS."
exit 1
fi
JQ_SEARCH=".workflows[] | select(.name == \"$WORKFLOW_NAME\") | \"\\(.id)\""
echo "Searching for workflow..."
WORKFLOW_ID=$(gh api "repos/$ORG_NAME/$REPO_NAME/actions/workflows?per_page=100" | jq -r "$JQ_SEARCH")
if [ -z "$WORKFLOW_ID" ]; then
printf "Workflow not found!\nCheck your spelling!"
exit 1
else
echo "...done!"
fi
echo "Retrieving runs..."
WORKFLOW_RUNS=$(gh api "repos/$ORG_NAME/$REPO_NAME/actions/workflows/$WORKFLOW_ID/runs?per_page=100")
echo "done!"
RUN_COUNT=$(echo "$WORKFLOW_RUNS" | jq -r ".total_count")
read -r -p "Are you sure you want to delete all $RUN_COUNT runs? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
DELETED=0
LEFT=1
while [[ $LEFT -gt "0" ]]; do
# Be lazy and try to avoid fetching stale results
sleep 1
BATCH=$(gh api "repos/$ORG_NAME/$REPO_NAME/actions/workflows/$WORKFLOW_ID/runs?per_page=100")
LEFT=$(echo "$BATCH" | jq -r ".total_count")
echo "Deleting up to 100 of $LEFT"
echo "$BATCH" |
jq -r '.workflow_runs[] | "\(.id)"' |
xargs -n1 -I % gh api repos/"$ORG_NAME"/"$REPO_NAME"/actions/runs/% -X DELETE
((DELETED = DELETED + LEFT))
echo "done!"
done
echo ""
echo "All Done! Deleted $DELETED runs from \"$WORKFLOW_NAME\""
else
echo "Writing preliminary data to /tmp/workflow_info.json and aborting!"
echo "$WORKFLOW_RUNS" > /tmp/workflow_info.json
fi