(feat): add cleanup for workflow runs and releases
This commit is contained in:
parent
4445852c42
commit
dbd685bd43
3 changed files with 99 additions and 0 deletions
56
.forgejo/workflows/cleanup.yml
Normal file
56
.forgejo/workflows/cleanup.yml
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
on:
|
||||
workflow_dispatch:
|
||||
schedule:
|
||||
- cron: "30 3 * * *"
|
||||
|
||||
jobs:
|
||||
cleanup-workflow-runs:
|
||||
runs-on: docker
|
||||
steps:
|
||||
- name: Install Packages
|
||||
run: apt-get update && apt-get install -y curl jq
|
||||
|
||||
- name: Delete workflow runs older than 14 days
|
||||
env:
|
||||
TOKEN: ${{ forgejo.token }}
|
||||
API: ${{ forgejo.api_url }}
|
||||
REPO: ${{ forgejo.repository }}
|
||||
CURRENT_RUN_ID: ${{ forgejo.run_id }}
|
||||
run: |
|
||||
set -eu
|
||||
|
||||
CUTOFF="$(date -u -d "14 days ago" +"%Y-%m-%dT%H:%M:%SZ")"
|
||||
PAGE=1
|
||||
DELETE_IDS=delete-workflow-runs.txt
|
||||
|
||||
: > "$DELETE_IDS"
|
||||
|
||||
while :; do
|
||||
curl -fsS \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
"$API/repos/$REPO/actions/runs?page=$PAGE&limit=50" \
|
||||
-o runs.json
|
||||
|
||||
COUNT="$(jq '.workflow_runs | length' runs.json)"
|
||||
test "$COUNT" -gt 0 || break
|
||||
|
||||
jq -r \
|
||||
--arg cutoff "$CUTOFF" \
|
||||
--arg current "$CURRENT_RUN_ID" \
|
||||
'.workflow_runs[]
|
||||
| select((.id | tostring) != $current)
|
||||
| select(["success", "failure", "cancelled", "skipped", "succeeded", "failed"] | index(.status))
|
||||
| select(.created < $cutoff)
|
||||
| .id' runs.json \
|
||||
>> "$DELETE_IDS"
|
||||
|
||||
PAGE="$((PAGE + 1))"
|
||||
done
|
||||
|
||||
while IFS= read -r run_id; do
|
||||
test -n "$run_id" || continue
|
||||
echo "Deleting workflow run $run_id"
|
||||
curl -fsS -X DELETE \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
"$API/repos/$REPO/actions/runs/$run_id"
|
||||
done < "$DELETE_IDS"
|
||||
Loading…
Reference in a new issue