Repository navigation
117 lines (102 loc) · 4.35 KB
/
Copy pathsync-release.yml
File metadata and controls
117 lines (102 loc) · 4.35 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
name: Sync Release to R2
on:
workflow_dispatch:
inputs:
tag:
description: GitHub Release tag; empty uses the latest stable release
required: false
type: string
permissions:
contents: read
concurrency:
group: sync-r2-release
cancel-in-progress: false
jobs:
sync-release-to-r2:
runs-on: ubuntu-latest
timeout-minutes: 120
env:
AWS_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: auto
R2_ACCOUNT_ID: ${{ secrets.R2_ACCOUNT_ID }}
R2_BUCKET: ${{ secrets.R2_BUCKET }}
GH_TOKEN: ${{ github.token }}
steps:
- name: Download Release Assets
run: |
if [ -n "${{ inputs.tag }}" ]; then
gh release download "${{ inputs.tag }}" --repo ${{ github.repository }} --dir assets --pattern '*'
else
gh release download --repo ${{ github.repository }} --dir assets --pattern '*'
fi
- name: Sync to Cloudflare R2
run: |
TAG="${{ inputs.tag }}"
if [ -z "$TAG" ]; then
TAG=$(gh release view --repo ${{ github.repository }} --json tagName -q .tagName)
fi
if [ -z "$TAG" ]; then
echo "错误: 未能获取到有效的 Release 标签"
exit 1
fi
mkdir -p upload_dir/$TAG
test -s assets/checksums.txt
cp assets/checksums.txt upload_dir/$TAG/
find assets -type f -name '*.zip' -exec cp {} upload_dir/$TAG/ \;
test -n "$(find upload_dir/$TAG -maxdepth 1 -type f -name '*.zip' -print -quit)"
echo "{\"tag_name\":\"$TAG\"}" > upload_dir/index.json
echo "正在上传 $TAG 到 R2..."
aws s3 sync upload_dir/$TAG/ s3://${R2_BUCKET}/serverdash/$TAG/ \
--cache-control 'public, max-age=31536000, immutable' \
--endpoint-url https://${R2_ACCOUNT_ID}.r2.cloudflarestorage.com
aws s3 cp upload_dir/index.json s3://${R2_BUCKET}/serverdash/index.json \
--cache-control 'no-store, max-age=0' \
--content-type 'application/json' \
--endpoint-url https://${R2_ACCOUNT_ID}.r2.cloudflarestorage.com
- name: Clean Up Old Releases in Cloudflare R2
run: |
TAG="${{ inputs.tag }}"
if [ -z "$TAG" ]; then
TAG=$(gh release view --repo ${{ github.repository }} --json tagName -q .tagName)
fi
if [ -z "$TAG" ]; then
echo "错误: 未能获取到有效的 Release 标签,跳过清理"
exit 0
fi
# 获取仓库最近发布的 Releases 列表(排除 draft)
RELEASE_TAGS=$(gh release list --repo ${{ github.repository }} --exclude-drafts --limit 10 --json tagName -q '.[].tagName')
# 策略:仅保留最新版本和上一个版本
KEEP_TAG_1="$TAG"
KEEP_TAG_2=""
for t in $RELEASE_TAGS; do
if [ "$t" != "$KEEP_TAG_1" ]; then
KEEP_TAG_2="$t"
break
fi
done
echo "=========================================="
echo "R2 版本保留策略: 仅保留最新版本和上一个版本"
echo "保留版本 1 (最新): $KEEP_TAG_1"
if [ -n "$KEEP_TAG_2" ]; then
echo "保留版本 2 (前一版): $KEEP_TAG_2"
else
echo "保留版本 2: 无 (未找到更早的发布版本)"
fi
echo "=========================================="
# 列出 R2 中 serverdash/ 下所有的版本目录
REMOTE_DIRS=$(aws s3 ls s3://${R2_BUCKET}/serverdash/ \
--endpoint-url https://${R2_ACCOUNT_ID}.r2.cloudflarestorage.com \
| awk '/PRE/ {print $2}' | sed 's#/$##')
for dir in $REMOTE_DIRS; do
[ -z "$dir" ] && continue
if [ "$dir" = "$KEEP_TAG_1" ] || [ -n "$KEEP_TAG_2" -a "$dir" = "$KEEP_TAG_2" ]; then
echo "✓ 保留版本: $dir"
else
echo "✗ 清理历史旧版本: s3://${R2_BUCKET}/serverdash/$dir/"
aws s3 rm s3://${R2_BUCKET}/serverdash/$dir/ --recursive \
--endpoint-url https://${R2_ACCOUNT_ID}.r2.cloudflarestorage.com
fi
done
echo "=========================================="
echo "R2 存储桶旧版本清理完成"