DEBUG.WIKI
阿里云;从建站到云上业务;云服务器 · 数据库 · 对象存储;查看产品 →
PanstarCloud:部署应用,接入 AI,云服务器与 AI 网关
IPPeak:从目标地区访问网页,住宅代理、SEO 监测、广告验证

解决 Bazel 报错 Download failed: Connect timed out 外部仓库拉取超时修复

排查 Bazel 构建下载外部 rules(如 rules_go、rules_jvm_external)报 Connect timed out 或 403 rate limit,分析 网络代理与预拉取镜像源配置。

问题现象

首次拉取代码执行 Bazel 构建时,进程卡在下载阶段并最终超时报错:

ERROR: An error occurred during the fetch of repository 'io_bazel_rules_go':
   java.io.IOException: Error downloading [https://github.com/bazelbuild/rules_go/releases/download/v0.42.0/rules_go-v0.42.0.zip] to /root/.cache/bazel/...: Connect timed out

触发根因

  1. 国内网络环境下无法稳定直连 GitHub Releases 官方外链;
  2. http_archive 中只配置了单一公网源,未配置内部镜像源或备用源(mirror URLs);
  3. 系统未向 Bazel 传递终端出口 HTTP/HTTPS 代理配置。

解决步骤

在 .bazelrc 中传递宿主机网络代理环境变量

确保 Bazel 抓取工具继承代理:

# .bazelrc
build --action_env=HTTP_PROXY="http://10.0.1.10:7890"
build --action_env=HTTPS_PROXY="http://10.0.1.10:7890"
build --action_env=NO_PROXY="localhost,127.0.0.1,internal.corp"

在 http_archive 中提供内网高速镜像源数组

支持双通道故障转移:

http_archive(
    name = "io_bazel_rules_go",
    sha256 = "d6b...",
    urls = [
        "https://mirrors.internal.corp/bazel/rules_go-v0.42.0.zip",
        "https://github.com/bazelbuild/rules_go/releases/download/v0.42.0/rules_go-v0.42.0.zip",
    ],
)

使用 --distdir 开启离线预热模式

在无法出网的内网专区构建:bazel build --distdir=/opt/bazel_cache //...。

验证确认

重新执行 Bazel 构建:

外部依赖秒级从镜像源下载并完成 SHA-256 校验,构建流水线顺畅推进。