From e7d9290b4376eb7a6b2eef415ca2a33a08266b24 Mon Sep 17 00:00:00 2001 From: Loonphy Date: Wed, 25 Sep 2024 21:12:14 +0800 Subject: [PATCH] feat: test mirrors --- .github/workflows/test-mirrors.yml | 70 ++++++++++++++++++++++++++++++ README.md | 5 +++ 2 files changed, 75 insertions(+) create mode 100644 .github/workflows/test-mirrors.yml create mode 100644 README.md diff --git a/.github/workflows/test-mirrors.yml b/.github/workflows/test-mirrors.yml new file mode 100644 index 0000000..b92dc78 --- /dev/null +++ b/.github/workflows/test-mirrors.yml @@ -0,0 +1,70 @@ +name: Test Registry + +on: + push: + schedule: + - cron: '0 0 */1 * *' + workflow_dispatch: + +jobs: + ci: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Test Registries and Update README + run: | + # 读取 README.md 中的 registry 列表 + registries=$(awk '/\| Registry \| Status \| Speed \| Time \|/,/^$/' 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" + sed -i "s#| $registry |.*#| $registry | $status | $speed | $time |#" README.md + } + + # 测试每个 registry + image="library/nginx:1.25.1-alpine" + for registry in $registries + do + echo "Testing $registry" + # 清理可能存在的镜像 + docker rmi "$registry/$image" > /dev/null 2>&1 || true + + # 使用子shell隔离每个registry的执行 + ( + # 使用 timeout 命令限制执行时间 + output=$(mktemp) + 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//') + # 计算镜像大小(单位:MB) + image_size=$(docker image inspect "$registry/$image" --format='{{.Size}}' | awk '{print $1/1024/1024}') + # 计算速度(MB/s) + speed=$(echo "scale=2; $image_size / $pull_time" | bc) + echo "$registry is good, Speed: ${speed} MB/s, Time: ${pull_time}s" + else + status="❌ Failed" + speed="-" + pull_time="-" + echo "$registry failed" + fi + update_readme "$registry" "$status" "${speed} MB/s" "${pull_time}s" + docker rmi "$registry/$image" > /dev/null 2>&1 || true + rm "$output" + ) || true # 即使子shell失败,也继续执行下一个registry + done + + - 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 README with latest registry test results") + git push https://${GITHUB_ACTOR}:${PAT}@github.com/${GITHUB_REPOSITORY}.git + env: + PAT: ${{ secrets.PAT }} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..af6e197 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +| Registry | Status | Speed | Time | +|----------|--------|-------|------| +| registry-1.docker.io | | | | +| hub.rat.dev | | | | +| docker.m.daocloud.io | | | |