-
Notifications
You must be signed in to change notification settings - Fork 1.3k
134 lines (117 loc) · 3.99 KB
/
Copy pathpurge-jsdelivr.yml
File metadata and controls
134 lines (117 loc) · 3.99 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
name: Purge jsDelivr Cache
on:
push:
branches:
- main
paths:
- 'cfg/**'
- 'game_rule/**'
- 'script/**'
- 'shell/**'
- 'overwrite/**'
- '!**/archived/**'
- '!**/README.md'
- '!README.md'
workflow_run:
workflows:
- Auto generate rules
- Auto update mainland config
types:
- completed
branches:
- main
workflow_dispatch: # 支持手动触发
jobs:
purge-jsdelivr:
if: |
(
github.event_name == 'workflow_run' &&
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.head_repository.full_name == github.repository &&
github.event.workflow_run.head_branch == 'main'
) ||
github.event_name == 'workflow_dispatch' ||
(
github.event_name == 'push' &&
github.actor != 'github-actions[bot]' &&
github.ref_name == 'main'
)
runs-on: ubuntu-latest
permissions:
contents: read
continue-on-error: true
defaults:
run:
shell: bash
steps:
- name: Checkout code
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
ref: main
fetch-depth: 0
- name: Soft Debounce (Wait & Check)
id: debounce
env:
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
EVENT_NAME: ${{ github.event_name }}
run: |
CURRENT_SHA=$(git rev-parse HEAD)
echo "Event: $EVENT_NAME"
echo "Target branch: main"
if ! git ls-remote --exit-code --heads origin main >/dev/null 2>&1; then
echo "Remote branch 'main' not found. Skipping purge."
echo "should_purge=false" >> "$GITHUB_OUTPUT"
exit 0
fi
if [ "$EVENT_NAME" = "workflow_run" ] || [ "$EVENT_NAME" = "workflow_dispatch" ]; then
echo "Workflow run head SHA: $HEAD_SHA"
echo "A source workflow completed or an explicit purge was requested."
# 规则提交由 Auto generate rules 完成后统一触发此处,避免对源文件和
# 生成产物分别清理两次。即使没有生成文件变化,源规则本身也需要刷新缓存。
echo "should_purge=true" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "Current Commit SHA: $CURRENT_SHA"
echo "Waiting for 60 seconds to batch multiple changes..."
# 倒计时
for i in {60..1}; do
# 仅每10秒打印一次日志防止刷屏
if (( i % 10 == 0 )); then
echo "$i seconds remaining..."
fi
sleep 1
done
echo "Fetching latest remote info..."
git fetch origin main
LATEST_SHA=$(git rev-parse origin/main)
echo "Latest Remote SHA: $LATEST_SHA"
if [ "$CURRENT_SHA" != "$LATEST_SHA" ]; then
echo "A newer commit ($LATEST_SHA) has been detected."
echo "Skipping this run to avoid redundant purge."
echo "should_purge=false" >> "$GITHUB_OUTPUT"
exit 0
else
echo "This is still the latest commit. Proceeding with purge..."
echo "should_purge=true" >> "$GITHUB_OUTPUT"
fi
- name: Purge jsDelivr Cache
if: steps.debounce.outputs.should_purge == 'true'
run: |
REPO="${{ github.repository }}"
echo "Purging jsDelivr cache for $REPO"
for i in {1..3}; do
echo "Attempt $i: Purging..."
result=$(curl -s -X GET "https://purge.jsdelivr.net/gh/${REPO}")
echo "$result"
if echo "$result" | grep -q '"status": "finished"'; then
echo "Purge succeeded."
break
else
echo "Purge failed, retrying in 5s..."
sleep 5
fi
if [ "$i" -eq 3 ]; then
echo "Purge failed after 3 attempts."
exit 1
fi
done