标准错误:致命错误:无法访问 'https://github.com/org/repo.git/':无法解析主机:github.com
stderr: fatal: unable to access 'https://github.com/org/repo.git/': Could not resolve host: github.com
ID: cicd/jenkins-pipeline-git-checkout-failed
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Jenkins 2.440.x | active | — | — | — |
| Jenkins 2.450.x | active | — | — | — |
| Git plugin 5.2.x | active | — | — | — |
根因分析
Jenkins 管道无法克隆 Git 仓库,因为 Jenkins 代理由于 DNS 配置错误、网络代理设置或代理处于受限网络环境中而无法解析主机名。
English
Jenkins pipeline fails to clone a Git repository because the Jenkins agent cannot resolve the hostname due to DNS misconfiguration, network proxy settings, or the agent being in a restricted network environment.
官方文档
https://www.jenkins.io/doc/book/pipeline/scm/解决方案
-
Configure the Jenkins agent to use a specific DNS server by setting JVM arguments: `-Djava.security.policy=/path/to/policy -Dnetworkaddress.cache.ttl=60` and ensure the agent's `/etc/resolv.conf` contains valid DNS servers like `8.8.8.8`.
-
If behind a proxy, configure Git to use the proxy: `git config --global http.proxy http://proxy.example.com:8080` and `git config --global https.proxy http://proxy.example.com:8080` in the pipeline script before checkout.
无效尝试
常见但无效的做法:
-
70% 失败
Adding `github.com` to the `/etc/hosts` file on the agent may work temporarily but fails if the IP address changes, and does not address the root cause of DNS resolution failure.
-
80% 失败
Changing the Git URL from HTTPS to SSH does not resolve DNS issues; SSH also requires hostname resolution and may introduce additional authentication failures.