Jenkins 间歇性 git 连接错误

Jenkins intermittent git connectivity error

我有一个 Jenkins 作业,99% 的时间 运行 都没有问题,但偶尔我在尝试获取时会收到 Git 插件生成的错误。有问题的 Git 存储库托管在 GitLab.

此错误的来源可能是 Jenkins、网络连接或 GitLab。

我的问题是,有没有人在 Git 中使用 Jenkins 和 Git 托管在 GitLab 或其他地方看到过类似的问题?

堆栈跟踪(出于安全原因已编辑):

Building remotely on [MACHINE_NAME] (build) in workspace c:\workspace\location
 > C:\location\of\git\bin\git.exe rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > C:\location\of\git\bin\git.exe config remote.origin.url git@git.<gitlab-server>:My/project.git # timeout=10
Fetching upstream changes from git@git.<gitlab-server>:My/project.git
 > C:\location\of\git\bin\git.exe --version # timeout=10
using GIT_SSH to set credentials 
 > C:\location\of\git\bin\git.exe fetch --tags --progress git@git.<gitlab-server>:My/project.git +refs/heads/*:refs/remotes/origin/*
ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from git@git.<gitlab-server>:My/project.git
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:797)
    ...
    at hudson.model.Executor.run(Executor.java:404)
Caused by: hudson.plugins.git.GitException: Command "C:\location\of\git\bin\git.exe fetch --tags --progress git@git.<gitlab-server>:My/project.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: ssh_exchange_identification: Connection closed by remote host
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1752)
    ...
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:795)
    ... 11 more
ERROR: null
Warning: you have no plugins providing access control for builds, so falling back to legacy behavior of permitting any downstream builds to be triggered
Finished: FAILURE

编辑(基于反对票)
到目前为止进行的研究(我运行定期安排 Jenkins 工作):

  1. 修改 Jenkins 以从机器 Jenkins 运行s 上的特定文件读取 ssh 私钥 - 间歇性观察到错误
  2. 将 ssh 私钥直接复制并粘贴到 Jenkins 中(如果是文件 1.) 的访问权限问题 - 间歇性观察到错误
  3. Git 命令 运行 作为 Jenkins 运行 所在 windows 机器上的计划作业(尝试消除 Jenkins 作为问题的根源)-等待结果...

如果第 3 点产生错误,那么我可以确定这是 Git实验室或网络连接问题。如果没有,那么我可以确定这是 Jenkins 的问题。如果有人在我之前注意到这个问题,我将不胜感激知道他们是如何解决它的。

更新(根据研究结果)

上面第 3 点提到的工作是周末每小时 运行。没有观察到错误。 Jenkins 作业在周末也是每小时 运行,并且观察到两个错误。 这支持了问题出在 Jenkins Git 插件上的理论。

在 Jenkins 服务器上 运行 ProcessExplorer 并查找 orphaned Git 进程。例如,使用 SourceTree:

解决方案:

大概会有几个Git.exe进程运行宁野,杀掉它们,杀掉进程树。一次一个,重要的是您尝试缩小范围并分享您的发现:)

要测试这是否有效,请返回 Jenkins 重新键入 git 回购路径(这会导致刷新),然后您将不会看到尝试连接到 Git.[=12= 的错误]

您可以使用任务管理器而不是 ProcessExplorer,我发现使用 ProcessExplorer 进行诊断更好,通常使用 SysInternal 工具进行诊断。

在我的例子中,问题是由于 sshd 中的 "MaxStartups" 指令引起的,它指定了与 SSH 守护进程的并发未验证连接的最大数量(有关详细信息,请参见 man sshd_config)。

当许多 Jenkins 作业 运行 并发时,这可能会导致许多并发的未经身份验证的 SSH 守护程序连接,在这种情况下,sshd 将开始断开连接。您可以在 /etc/ssh/sshd_config 中设置 "MaxStartups" 指令以增加 sshds 将开始丢弃连接的并发未验证连接数(默认为 10)。

来自这个相关问题:https://serverfault.com/questions/924058/jenkins-occasionally-fails-to-clone-fetch

就我而言,公司的某个人编辑了 /etc/ssh/ssh_config 文件并添加了自定义 ProxyCommand。我注释掉了那一行,它起作用了。