generated from sindresorhus/node-module-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
54 lines (45 loc) · 1.84 KB
/
clean-workflows.yaml
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
name: Clean Up Obsolete Workflows
on:
schedule:
- cron: "0 0 1 * *" # Run monthly on the 1st day of each month at midnight UTC
workflow_dispatch: # Allow manual triggering
jobs:
cleanup:
runs-on: ubuntu-latest
permissions:
actions: write
contents: read
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Clean up obsolete workflows
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -euo pipefail
# Get all workflow files in the current repository
current_workflows=$(find .github/workflows -print0 -name "*.yml" -o -name "*.yaml" | xargs -0 -I {} basename {})
# Get all workflows from the Actions tab
all_workflows=$(gh api repos/${{ github.repository }}/actions/workflows | jq -r '.workflows[].path' | xargs -I {} basename {})
# Find obsolete workflows
obsolete_workflows=$(comm -23 <(echo "$all_workflows" | sort) <(echo "$current_workflows" | sort) | grep -v "dependabot-updates" || true)
# Check if there are any obsolete workflows
if [ -z "$obsolete_workflows" ]; then
echo "No obsolete workflows found. Exiting."
exit 0
fi
# Delete runs of obsolete workflows
for workflow in $obsolete_workflows; do
echo "Cleaning up obsolete workflow: $workflow"
runs=$(gh run list --workflow "$workflow" --json databaseId --jq '.[].databaseId')
if [ -z "$runs" ]; then
echo "No runs found for workflow: $workflow"
continue
fi
for run_id in $runs; do
gh run delete "$run_id"
echo "Deleted run $run_id of workflow $workflow"
done
done
echo "Cleanup completed successfully."
exit 0