Skip to content

Commit

Permalink
update cleanup script to handle multiple workflows with same name
Browse files Browse the repository at this point in the history
  • Loading branch information
5u6r054 committed Aug 7, 2024
1 parent 8403489 commit 6e1b4d1
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions .github/workflows/delete_workflow_runs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@ REPO_INFO=$(gh repo view --json nameWithOwner)
REPO=$(echo $REPO_INFO | jq -r .nameWithOwner)
echo "Repository: $REPO"

echo "Searching for workflow: '$WORKFLOW_NAME'"
WORKFLOW_ID=$(gh api "/repos/$REPO/actions/workflows" | jq -r ".workflows[] | select(.name == \"$WORKFLOW_NAME\") | .id")
echo "Searching for workflows with name: '$WORKFLOW_NAME'"
WORKFLOW_IDS=$(gh api "/repos/$REPO/actions/workflows" | jq -r ".workflows[] | select(.name == \"$WORKFLOW_NAME\") | .id")

if [ -z "$WORKFLOW_ID" ]; then
echo "No workflow found with name '$WORKFLOW_NAME'"
if [ -z "$WORKFLOW_IDS" ]; then
echo "No workflows found with name '$WORKFLOW_NAME'"
exit 1
fi

echo "Found workflow with ID: $WORKFLOW_ID"
echo "Found workflow(s) with ID(s): $WORKFLOW_IDS"

delete_runs() {
local page=$1
echo "Fetching runs (page $page)..."
RUNS=$(gh api "/repos/$REPO/actions/workflows/$WORKFLOW_ID/runs?per_page=100&page=$page")
local workflow_id=$1
local page=$2
echo "Fetching runs for workflow $workflow_id (page $page)..."
RUNS=$(gh api "/repos/$REPO/actions/workflows/$workflow_id/runs?per_page=100&page=$page")
RUN_COUNT=$(echo $RUNS | jq '.workflow_runs | length')

if [ "$RUN_COUNT" -eq 0 ]; then
Expand All @@ -44,14 +45,19 @@ delete_runs() {
return 0
}

page=1
while delete_runs $page; do
((page++))
done
for WORKFLOW_ID in $WORKFLOW_IDS; do
echo "Processing workflow ID: $WORKFLOW_ID"
page=1
while delete_runs $WORKFLOW_ID $page; do
((page++))
done

echo "All runs deleted for workflow '$WORKFLOW_NAME'"
echo "All runs deleted for workflow ID $WORKFLOW_ID"

echo "Deleting the workflow itself..."
gh api -X DELETE "/repos/$REPO/actions/workflows/$WORKFLOW_ID"
echo "Deleting the workflow itself..."
gh api -X DELETE "/repos/$REPO/actions/workflows/$WORKFLOW_ID"

echo "Workflow with ID $WORKFLOW_ID has been deleted."
done

echo "Workflow '$WORKFLOW_NAME' and all its runs have been deleted."
echo "All workflows named '$WORKFLOW_NAME' and their runs have been deleted."

0 comments on commit 6e1b4d1

Please sign in to comment.