-
Notifications
You must be signed in to change notification settings - Fork 3
97 lines (83 loc) · 3.71 KB
/
test-mirrors.yml
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
name: Test Registry
on:
push:
schedule:
- cron: '0 0 */1 * *'
workflow_dispatch:
jobs:
ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Test Registries and Update README
run: |
# 读取 README.md 中的 registry 列表
registries=$(awk '/\| Registry \| Status \| Speed \| Time \| Integrity \|/,/^$/' README.md | tail -n +3 | sed '/^$/d' | awk -F'|' '{print $2}' | sed 's/^ *//;s/ *$//')
# 更新 README.md 的函数
update_readme() {
local registry="$1"
local status="$2"
local speed="$3"
local time="$4"
local integrity="$5"
sed -i "s#| $registry |.*#| $registry | $status | $speed | $time | $integrity |#" README.md
}
# 测试 registry 的函数,包含重试逻辑和完整性检查
test_registry() {
local registry="$1"
local image="$2"
local max_attempts=3
local attempt=1
local output=$(mktemp)
# 首先拉取官方镜像
docker pull docker.io/$image > /dev/null 2>&1
official_digest=$(docker inspect --format='{{index .RepoDigests 0}}' docker.io/$image | cut -d'@' -f2)
while [ $attempt -le $max_attempts ]; do
echo "Attempt $attempt of $max_attempts for $registry"
if timeout 60s bash -c "time docker pull $registry/$image" > "$output" 2>&1; then
status="✅ Good"
pull_time=$(grep real "$output" | awk '{print $2}' | sed 's/0m//;s/s//')
image_size=$(docker image inspect "$registry/$image" --format='{{.Size}}' | awk '{print $1/1024/1024}')
speed=$(echo "scale=2; $image_size / $pull_time" | bc)
echo "$registry is good, Speed: ${speed} MB/s, Time: ${pull_time}s"
# 检查镜像完整性
registry_digest=$(docker inspect --format='{{index .RepoDigests 0}}' $registry/$image | cut -d'@' -f2)
if [ "$official_digest" = "$registry_digest" ]; then
integrity="✅ Verified"
else
integrity="❌ Mismatch"
fi
update_readme "$registry" "$status" "${speed} MB/s" "${pull_time}s" "$integrity"
rm "$output"
return 0
else
echo "$registry failed on attempt $attempt"
attempt=$((attempt + 1))
sleep 5 # 等待5秒后重试
fi
done
status="❌ Failed"
update_readme "$registry" "$status" "-" "-" "-"
rm "$output"
return 1
}
# 测试每个 registry
image="library/nginx:alpine"
for registry in $registries; do
echo "Testing $registry"
# 清理可能存在的镜像
docker rmi "$registry/$image" > /dev/null 2>&1 || true
# 使用子shell隔离每个registry的执行
(
test_registry "$registry" "$image"
docker rmi "$registry/$image" > /dev/null 2>&1 || true
) || true # 即使子shell失败,也继续执行下一个registry
done
# 清理官方镜像
docker rmi docker.io/$image > /dev/null 2>&1 || true
- name: Commit and push if changed
run: |
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
git add README.md
git diff --quiet && git diff --staged --quiet || (git commit -m "update registry test results"; git push)