Skip to content

Instantly share code, notes, and snippets.

@airvzxf
Created June 5, 2021 03:49
Show Gist options
  • Save airvzxf/20396dd25a59c6812a9fcad601f48716 to your computer and use it in GitHub Desktop.
Save airvzxf/20396dd25a59c6812a9fcad601f48716 to your computer and use it in GitHub Desktop.
GitHub API -> Delete Runs from Workflows (Actions)
#!/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