Created
June 5, 2021 03:49
-
-
Save airvzxf/20396dd25a59c6812a9fcad601f48716 to your computer and use it in GitHub Desktop.
GitHub API -> Delete Runs from Workflows (Actions)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
USERNAME="my_user" | |
REPOSITORY="the_repo" | |
TOKEN="ghp_github_token permissions: repo, workflow" | |
WORKFLOW_RUNS=$( | |
curl \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
--silent \ | |
-u "${USERNAME}":"${TOKEN}" \ | |
"https://api.github.com/repos/${USERNAME}/${REPOSITORY}/actions/runs" | | |
jq '.total_count' | |
) | |
echo "Workflow ran: ${WORKFLOW_RUNS}" | |
if [[ ${WORKFLOW_RUNS} == "0" || ${WORKFLOW_RUNS} == "null" ]]; then | |
exit 0 | |
fi | |
WORKFLOW_IDS=$( | |
curl \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
--silent \ | |
-u "${USERNAME}":"${TOKEN}" \ | |
"https://api.github.com/repos/${USERNAME}/${REPOSITORY}/actions/runs?per_page=100" | | |
jq '.workflow_runs[].id' | |
) | |
WORKFLOWS_FOUND=$(echo "${WORKFLOW_IDS[@]}" | wc -w) | |
echo "Number of workflow ID's found: ${WORKFLOWS_FOUND}" | |
if [[ ${WORKFLOWS_FOUND} == "0" ]]; then | |
exit 0 | |
fi | |
echo "Delete ID's:" | |
while IFS= read -r ID; do | |
echo " ${ID}" | |
curl \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
-X DELETE \ | |
--silent \ | |
-u "${USERNAME}":"${TOKEN}" \ | |
"https://api.github.com/repos/${USERNAME}/${REPOSITORY}/actions/runs/${ID}" | |
done <<<"${WORKFLOW_IDS[@]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment