-
Notifications
You must be signed in to change notification settings - Fork 4
/
gh-delete-workflow-runs.sh
82 lines (65 loc) · 2.29 KB
/
gh-delete-workflow-runs.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env bash
# Delete all the runs for a given workflow
# https://github.com/orgs/community/discussions/26256#discussioncomment-9425133
# Added as part of SONIC-466
# Example Run:
# bash ./gh-delete-workflow-runs.sh "edx/commerce-coordinator" "ci.yml"
# github.com
# ✓ Logged in to github.com account grmartin (keyring)
# - Active account: true
# - Git operations protocol: ssh
# - Token: gho_************************************
# - Token scopes: 'admin:public_key', 'gist', 'read:org', 'repo'
# Getting all completed runs for workflow ci.yml in edx/commerce-coordinator
#
# Found 852 completed runs for workflow ci.yml
# Processing run 5520556129.
# ✓ Request to delete workflow submitted.
# Processing run 5520343434.
# ✓ Request to delete workflow submitted.
# Processing run 5520269220.
# ✓ Request to delete workflow submitted.
# ...
# NOTE: if you get the following error:
# Resource protected by organization SAML enforcement. You must grant your OAuth token access to this organization. (HTTP 403)
# Rerun `gh auth login` and try again.
# https://github.com/cli/cli/issues/2661
set -oe pipefail
REPOSITORY=$1
WORKFLOW_NAME=$2 # name of the workflow, usually same as the filename
# Display an error message and exit the script with an error code.
fatal_error() {
echo "$1"
exit 1
}
# Validate arguments
if [[ -z "$REPOSITORY" ]]; then
fatal_error "Repository is required"
fi
if [[ -z "$WORKFLOW_NAME" ]]; then
fatal_error "Workflow name is required"
fi
if [[ ! -x "$(which gh)" ]]; then
fatal_error "Application gh is not available or not executable"
fi
gh auth status || gh auth login
echo "Getting all completed runs for workflow $WORKFLOW_NAME in $REPOSITORY"
RUNS=$(
gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/$REPOSITORY/actions/workflows/$WORKFLOW_NAME/runs" \
--paginate \
--jq '.workflow_runs[] | select(.conclusion != "") | .id' \
| awk '!a[$0]++'
)
if [[ -z ${RUNS[*]} ]]; then
echo "Found no completed runs for workflow $WORKFLOW_NAME."
exit 0
fi
echo "Found $(echo "$RUNS" | wc -l) completed runs for workflow $WORKFLOW_NAME"
for RUN in $RUNS; do
echo "Processing run $RUN."
gh run delete --repo "$REPOSITORY" "$RUN" || echo "Failed to delete run $RUN"
sleep 0.1
done