Repository navigation
104 lines (88 loc) · 4.49 KB
/
Copy pathcleanup.yml
File metadata and controls
104 lines (88 loc) · 4.49 KB
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
104
name: Cleanup old workflow runs, artifacts, and caches
permissions:
actions: write
contents: read
on:
schedule:
- cron: "0 3 * * *" # daily at 03:00 UTC
workflow_dispatch:
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Set cutoff dates
id: cutoff
run: |
echo "cutoff_success=$(date -u -d '3 days ago' +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
echo "cutoff_failure=$(date -u -d '1 days ago' +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
echo "cutoff_cancelled=$(date -u -d '1 days ago' +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
echo "cutoff_timedout=$(date -u -d '1 days ago' +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
echo "cutoff_artifacts=$(date -u -d '1 hours ago' +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
echo "cutoff_caches=$(date -u -d '1 days ago' +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
- name: Delete workflow runs
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
CUTOFF_SUCCESS: ${{ steps.cutoff.outputs.cutoff_success }}
CUTOFF_FAILURE: ${{ steps.cutoff.outputs.cutoff_failure }}
CUTOFF_CANCELLED: ${{ steps.cutoff.outputs.cutoff_cancelled }}
CUTOFF_TIMEDOUT: ${{ steps.cutoff.outputs.cutoff_timedout }}
run: |
gh api repos/$REPO/actions/runs --paginate --jq '.workflow_runs[] | {id: .id, created: .created_at, conclusion: .conclusion} | @json' \
| while read -r run; do
clean_run=$(echo "$run" | sed -e 's/^"//' -e 's/"$//' -e 's/\\"/"/g')
id=$(echo "$clean_run" | jq -r '.id // empty')
created=$(echo "$clean_run" | jq -r '.created // empty')
conclusion=$(echo "$clean_run" | jq -r '.conclusion // empty')
if [[ -z "$id" || "$id" == "null" ]]; then continue; fi
if [[ "$conclusion" == "failure" && "$created" < "$CUTOFF_FAILURE" ]]; then
echo "Deleting FAILED run $id from $created"
gh api repos/$REPO/actions/runs/$id -X DELETE || true
fi
if [[ "$conclusion" == "cancelled" && "$created" < "$CUTOFF_CANCELLED" ]]; then
echo "Deleting CANCELLED run $id from $created"
gh api repos/$REPO/actions/runs/$id -X DELETE || true
fi
if [[ "$conclusion" == "timed_out" && "$created" < "$CUTOFF_TIMEDOUT" ]]; then
echo "Deleting TIMED_OUT run $id from $created"
gh api repos/$REPO/actions/runs/$id -X DELETE || true
fi
if [[ "$conclusion" == "success" && "$created" < "$CUTOFF_SUCCESS" ]]; then
echo "Deleting SUCCESSFUL run $id from $created"
gh api repos/$REPO/actions/runs/$id -X DELETE || true
fi
done
- name: Delete old artifacts
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
CUTOFF_ARTIFACTS: ${{ steps.cutoff.outputs.cutoff_artifacts }}
run: |
gh api repos/$REPO/actions/artifacts --paginate --jq '.artifacts[] | {id: .id, created: .created_at} | @json' \
| while read -r artifact; do
clean_art=$(echo "$artifact" | sed -e 's/^"//' -e 's/"$//' -e 's/\\"/"/g')
id=$(echo "$clean_art" | jq -r '.id // empty')
created=$(echo "$clean_art" | jq -r '.created // empty')
if [[ -z "$id" || "$id" == "null" ]]; then continue; fi
if [[ "$created" < "$CUTOFF_ARTIFACTS" ]]; then
echo "Deleting ARTIFACT $id from $created"
gh api repos/$REPO/actions/artifacts/$id -X DELETE || true
fi
done
- name: Delete old caches
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
CUTOFF_CACHES: ${{ steps.cutoff.outputs.cutoff_caches }}
run: |
gh api repos/$REPO/actions/caches --paginate --jq '.actions_caches[] | {id: .id, created: .created_at} | @json' \
| while read -r cache; do
clean_cache=$(echo "$cache" | sed -e 's/^"//' -e 's/"$//' -e 's/\\"/"/g')
id=$(echo "$clean_cache" | jq -r '.id // empty')
created=$(echo "$clean_cache" | jq -r '.created // empty')
if [[ -z "$id" || "$id" == "null" ]]; then continue; fi
if [[ "$created" < "$CUTOFF_CACHES" ]]; then
echo "Deleting CACHE $id from $created"
gh api repos/$REPO/actions/caches/$id -X DELETE || true
fi
done