-
Notifications
You must be signed in to change notification settings - Fork 1
103 lines (87 loc) · 3.18 KB
/
auto_delete.yml
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
name: auto_delete
on:
schedule:
- cron: '0 2 10 * *'
workflow_dispatch:
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: List all releases
id: list_releases
run: |
gh release list --limit 1000 > releases.txt
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Determine releases to keep
id: determine_releases
run: |
current_month=$(date +'%Y-%m')
current_year=$(date +'%Y')
keep_releases=()
while read -r release; do
release_date=$(echo $release | awk '{print $3}')
release_tag=$(echo $release | awk '{print $1}')
release_month=$(date -d $release_date +'%Y-%m')
release_year=$(date -d $release_date +'%Y')
if [[ $release_month == $current_month ]]; then
keep_releases+=($release_tag)
else
last_release_of_month=$(grep $release_month releases.txt | tail -n 1 | awk '{print $1}')
if [[ ! " ${keep_releases[@]} " =~ " ${last_release_of_month} " ]]; then
keep_releases+=($last_release_of_month)
fi
fi
done < releases.txt
echo "keep_releases=${keep_releases[@]}" >> $GITHUB_ENV
- name: Ensure total releases do not exceed 100
id: ensure_total_releases
run: |
keep_releases=(${{ env.keep_releases }})
total_releases=$(wc -l < releases.txt)
if [ $total_releases -gt 100 ]; then
excess_releases=$(($total_releases - 100))
old_releases=$(tail -n $excess_releases releases.txt | awk '{print $1}')
for release_tag in $old_releases; do
if [[ ! " ${keep_releases[@]} " =~ " ${release_tag} " ]]; then
gh release delete $release_tag -y
fi
done
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Delete old releases
run: |
keep_releases=(${{ env.keep_releases }})
while read -r release; do
release_tag=$(echo $release | awk '{print $1}')
if [[ ! " ${keep_releases[@]} " =~ " ${release_tag} " ]]; then
gh release delete $release_tag -y
fi
done < releases.txt
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Delete tags without releases
run: |
all_tags=$(git tag -l)
echo "All tags:"
echo "$all_tags"
release_tags=$(gh release list --limit 1000 | awk '{print $1}')
echo "Release tags:"
echo "$release_tags"
IFS=' ' read -r -a release_tags_array <<< "$release_tags"
for tag in $all_tags; do
if [[ ! " ${release_tags_array[@]} " =~ " ${tag} " ]]; then
echo "Deleting tag: $tag"
git tag -d $tag
git push origin :refs/tags/$tag
else
echo "Keeping tag: $tag"
fi
done
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}