Skip to content

Purge jsDelivr Cache #1628

Purge jsDelivr Cache

Purge jsDelivr Cache #1628

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