-
Notifications
You must be signed in to change notification settings - Fork 7
59 lines (54 loc) · 2.45 KB
/
delete_old.yml
File metadata and controls
59 lines (54 loc) · 2.45 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
name: Delete Old Build Artifacts
on:
workflow_dispatch:
schedule:
- cron: "0 21 * * *"
jobs:
cleanup:
runs-on: ubuntu-latest
permissions:
actions: write
steps:
- name: Delete old workflow runs
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
run: |
echo "Current run ID: ${{ github.run_id }}"
echo "Cutoff date: $(date -d '-7 day' -Iseconds)"
# 获取 BuildImage-all 最近一次成功的 run ID
LATEST_ALL=$(gh run list --workflow "build_image_all.yml" --status success --limit 1 --json databaseId \
| jq -r '.[0].databaseId // empty')
echo "Latest successful BuildImage-all ID: ${LATEST_ALL:-none}"
# 获取 BuildImage-MT7981 最近一次成功的 run ID
LATEST_MT7981=$(gh run list --workflow "build_image_mt7981.yml" --status success --limit 1 --json databaseId \
| jq -r '.[0].databaseId // empty')
echo "Latest successful BuildImage-MT7981 ID: ${LATEST_MT7981:-none}"
# 删除 7 天前的 runs(排除当前 run 和两个工作流各自最近成功的构建)
gh run list --limit 200 --json databaseId,createdAt \
| jq -r --arg current "${{ github.run_id }}" \
--arg cutoff "$(date -d '-7 day' -Iseconds)" \
--arg latest_all "$LATEST_ALL" \
--arg latest_mt7981 "$LATEST_MT7981" \
'.[] | select(.createdAt < $cutoff
and (.databaseId | tostring) != $current
and (.databaseId | tostring) != $latest_all
and (.databaseId | tostring) != $latest_mt7981) | .databaseId' \
| while read -r id; do
[ -z "$id" ] && continue
echo "Deleting old run: $id"
gh run delete "$id" --repo "$GH_REPO" || true
done
- name: Cleanup this workflow runs
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
run: |
# 删除本工作流的旧 runs(保留最近 5 个)
gh run list --workflow "delete_old.yml" --limit 100 --json databaseId \
| jq -r '.[5:] | .[].databaseId' \
| while read -r id; do
[ -z "$id" ] && continue
echo "Deleting cleanup workflow run: $id"
gh run delete "$id" --repo "$GH_REPO" || true
done