Files
matrix/scripts/setup-runner-mirrors.sh
T
Matrix Music Bot Dev 6ec609b1ce perf(国内网络): runner 镜像加速配置 + Dockerfile cargo 镜像
按用户全选 4 个加速维度实施:

1. Docker 镜像加速(最大影响面)
   - scripts/setup-runner-mirrors.sh 写入 /etc/docker/daemon.json
   - registry-mirrors: docker.m.daocloud.io + ghcr.nju.edu.cn
   - 影响所有 docker pull:actions / base images / Gitea registry

2. crates.io 镜像
   - .cargo/config-docker.toml(与 .cargo/config.toml 区分,后者用于 Windows 本地开发)
   - rsproxy.cn 镜像(rustcc 维护,国内最稳)
   - Dockerfile COPY 此文件到 /root/.cargo/config.toml(在 cargo build 前)

3. GitHub git clone 加速
   - scripts/setup-runner-mirrors.sh 自动配置 git config --global
   - url.https://ghproxy.com/https://github.com.insteadOf https://github.com

4. 优先做 Docker 镜像加速(4 = 1 的子集,重复)

setup-runner-mirrors.sh 特性:
  - 自动备份原配置(.bak)
  - 用 python3 合并 daemon.json(保留用户已有配置)
  - 重启 docker 后验证 Registry Mirrors
  - 测试 hello-world pull 验证生效
  - --undo 回滚模式

镜像源选型理由:
  - docker.m.daocloud.io:DaoCloud 维护,最稳
  - ghcr.nju.edu.cn:NJU CTF 团队维护的 GitHub Container Registry 镜像
  - rsproxy.cn:rustcc 维护,sparse 协议兼容
  - ghproxy.com:通用 GitHub 镜像

使用方法(runner 上):
  sudo ./scripts/setup-runner-mirrors.sh
  sudo ./scripts/setup-runner-mirrors.sh --undo  # 回滚

下一步:处理 runner 缺 docker 能力的问题
2026-07-03 00:45:25 +08:00

160 lines
4.9 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# setup-runner-mirrors.sh —— Gitea Actions runner 国内网络加速配置
#
# 目的:让 workflow 在国内服务器上跑得更快
# 范围:Docker 镜像拉取 / crates.io 依赖下载 / git clone
# 适用:在 hermes-runner (ubuntu-22.04) 上以 root 跑一次
#
# 用法:
# chmod +x scripts/setup-runner-mirrors.sh
# sudo ./scripts/setup-runner-mirrors.sh
#
# 回滚:
# sudo ./scripts/setup-runner-mirrors.sh --undo
set -euo pipefail
# ============ 镜像源配置 ============
# Docker Hub 镜像源(选一个最稳的)
DOCKER_HUB_MIRROR="https://docker.m.daocloud.io"
# ghcr.io 镜像源(GitHub Container Registry
GHCR_MIRROR="https://ghcr.nju.edu.cn"
# crates.io 镜像源(rsproxy - rustcc 维护)
CARGO_MIRROR="sparse+https://rsproxy.cn/index/"
# github.com git clone 镜像(ghproxy - 通用)
GIT_MIRROR="https://ghproxy.com/https://github.com"
# ============ 日志 ============
log() { echo -e "\033[1;32m[$(date +%H:%M:%S)]\033[0m $*"; }
warn() { echo -e "\033[1;33m[WARN]\033[0m $*" >&2; }
err() { echo -e "\033[1;31m[ERR]\033[0m $*" >&2; }
# ============ 权限检查 ============
if [[ $EUID -ne 0 ]]; then
err "请用 root 运行:sudo $0"
exit 1
fi
# ============ 回滚模式 ============
if [[ "${1:-}" == "--undo" ]]; then
log "回滚模式:恢复所有备份"
if [[ -f /etc/docker/daemon.json.bak ]]; then
mv /etc/docker/daemon.json.bak /etc/docker/daemon.json
systemctl restart docker || true
log "✅ /etc/docker/daemon.json 已恢复"
else
warn "未找到 daemon.json.bak"
fi
if [[ -f /root/.cargo/config.toml.bak ]]; then
mv /root/.cargo/config.toml.bak /root/.cargo/config.toml
log "✅ /root/.cargo/config.toml 已恢复"
fi
exit 0
fi
# ============ 1. Docker daemon registry-mirrors ============
log "[1/3] 配置 Docker daemon registry-mirrors..."
DAEMON_CONFIG="/etc/docker/daemon.json"
mkdir -p /etc/docker
# 备份现有配置
if [[ -f "$DAEMON_CONFIG" ]] && [[ ! -f "${DAEMON_CONFIG}.bak" ]]; then
cp "$DAEMON_CONFIG" "${DAEMON_CONFIG}.bak"
log " 备份原配置到 ${DAEMON_CONFIG}.bak"
fi
# 合并现有配置 + mirrors
# 使用 python 而非 jq 保证镜像源可用
python3 - "$DAEMON_CONFIG" "$DOCKER_HUB_MIRROR" "$GHCR_MIRROR" <<'PYEOF'
import json, sys, os
config_path = sys.argv[1]
docker_hub_mirror = sys.argv[2]
ghcr_mirror = sys.argv[3]
# 读取现有配置
existing = {}
if os.path.exists(config_path):
try:
with open(config_path) as f:
existing = json.load(f)
except json.JSONDecodeError:
existing = {}
# 合并 registry-mirrors
mirrors = existing.get("registry-mirrors", [])
# 去重添加
for m in [docker_hub_mirror, ghcr_mirror]:
if m not in mirrors:
mirrors.append(m)
existing["registry-mirrors"] = mirrors
# 写回
with open(config_path, "w") as f:
json.dump(existing, f, indent=2)
print(f" mirrors 已设置: {mirrors}")
PYEOF
log " 重启 docker daemon..."
systemctl restart docker
sleep 2
docker info 2>/dev/null | grep -A 5 "Registry Mirrors" || warn " Registry Mirrors 未显示,可能需要手动验证"
# ============ 2. Cargo registry mirror ============
log "[2/3] 配置 /root/.cargo/config.toml (crates.io 镜像)..."
mkdir -p /root/.cargo
if [[ -f /root/.cargo/config.toml ]] && [[ ! -f /root/.cargo/config.toml.bak ]]; then
cp /root/.cargo/config.toml /root/.cargo/config.toml.bak
fi
cat > /root/.cargo/config.toml <<EOF
# rsproxy.cn 镜像(rustcc 维护,国内最稳)
# 由 scripts/setup-runner-mirrors.sh 自动生成
[source.crates-io]
replace-with = "rsproxy-sparse"
[source.rsproxy-sparse]
registry = "$CARGO_MIRROR"
[net]
git-fetch-with-cli = true
EOF
log " ✅ /root/.cargo/config.toml 已配置 rsproxy 镜像"
# ============ 3. Git 配置(GitHub 镜像 fallback============
log "[3/3] 配置 git url rewriteGitHub 镜像 fallback..."
git config --global url."$GIT_MIRROR".insteadOf "https://github.com"
log " ✅ git config: github.com → $GIT_MIRROR"
# ============ 4. 验证 ============
log "验证加速效果..."
# 验证 docker mirror
log " Docker mirror 测试(拉 hello-world):"
docker pull hello-world 2>&1 | tail -3 || warn " hello-world 拉取失败"
# 验证 cargo mirror
log " Cargo mirror 测试(cargo search):"
cargo search serde --limit 1 2>&1 | head -3 || warn " cargo search 失败"
# ============ 完成 ============
echo
log "✅ Runner 镜像加速配置完成!"
echo
echo "效果:"
echo " - Docker pullactions / base images):通过 DaoCloud/NJU 镜像加速"
echo " - cargo buildDockerfile 内):通过 rsproxy 镜像加速"
echo " - git cloneDockerfile 内如有 git 依赖):通过 ghproxy 镜像加速"
echo
echo "回滚命令:sudo $0 --undo"
echo
echo "下一步:"
echo " 1. 推送仓库触发 workflow 验证"
echo " 2. workflow 中 Dockerfile COPY .cargo/config-docker.toml 后 cargo build 走镜像"