Windows环境Jenkins通过SSH克隆Git子模块报错cannot spawn ssh
问题场景
Windows 10 CMD环境验证
在Windows CMD中执行以下命令时,所有操作运行正常,通过SSH拉取Git子模块也无异常:
git clone https://bbk.example.com/scm/myproject/myrepository.git cd myrepository git submodule update --init --recursive
子模块为SSH方式添加,需保持该配置不变,.gitmodules配置内容如下:
[submodule "submodules/validator"] path = submodules/validator url = ssh://git@bbk.example.com:7999/myproject/validator.git
系统相关程序路径如下:
where git C:\Program Files\Git\cmd\git.exe
where ssh C:\Windows\System32\OpenSSH\ssh.exe
已为Jenkins配置相同的Git路径,在配置的「Branch Sources」中添加了以下子模块行为:
Advanced sub-modules behaviours - Recursively update submodules: 已勾选 - Use credentials from default remote of parent repository: 已勾选 - Path of the reference repo to use during submodule update: 留空
同时添加了Checkout over SSH选项并配置了对应SSH凭据。
Jenkins运行报错情况
Jenkins执行任务拉取子模块时失败,报错error: cannot spawn ssh: No such file or directory,完整执行日志如下:
using GIT_ASKPASS to set credentials userpass > git fetch --no-tags --force --progress -- https://bbk.example.com/scm/myproject/myrepository.git +refs/heads/bug/AUTO-534/AUTO-744:refs/remotes/origin/bug/AUTO-534/AUTO-744 # timeout=10 Checking out Revision 2b300d6f20e615894152bdf1aab13efec55eed16 (bug/AUTO-534/AUTO-744) > git config core.sparsecheckout # timeout=10 > git checkout -f 2b300d6f20e615894152bdf1aab13efec55eed16 # timeout=10 Commit message: "test" > git rev-list --no-walk 2b300d6f20e615894152bdf1aab13efec55eed16 # timeout=10 > git remote # timeout=10 > git submodule init # timeout=10 > git submodule sync # timeout=10 > git config --get remote.origin.url # timeout=10 > git submodule init # timeout=10 > git config -f .gitmodules --get-regexp ^submodule\.(.+)\.url # timeout=10 > git config --get submodule.submodules/validator.url # timeout=10 > git remote # timeout=10 > git config --get remote.origin.url # timeout=10 > git config -f .gitmodules --get submodule.submodules/validator.path # timeout=10 using GIT_ASKPASS to set credentials userpass > git submodule update --init --recursive submodules/validator # timeout=10 [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline hudson.plugins.git.GitException: Command "git submodule update --init --recursive submodules/validator" returned status code 1: stdout: stderr: Cloning into 'C:/ProgramData/Jenkins/.jenkins/workspace/ation-core_bug_AUTO-534_AUTO-744/submodules/validator'... error: cannot spawn ssh: No such file or directory fatal: unable to fork fatal: clone of 'ssh://git@bbk.example.com:7999/myproject/validator.git' into submodule path 'C:/ProgramData/Jenkins/.jenkins/workspace/ation-core_bug_AUTO-534_AUTO-744/submodules/validator' failed Failed to clone 'submodules/validator'. Retry scheduled Cloning into 'C:/ProgramData/Jenkins/.jenkins/workspace/ation-core_bug_AUTO-534_AUTO-744/submodules/validator'... error: cannot spawn ssh: No such file or directory fatal: unable to fork fatal: clone of 'ssh://git@bbk.example.com:7999/myproject/validator.git' into submodule path 'C:/ProgramData/Jenkins/.jenkins/workspace/ation-core_bug_AUTO-534_AUTO-744/submodules/validator' failed Failed to clone 'submodules/validator' a second time, aborting at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2671) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:2096) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$500(CliGitAPIImpl.java:84) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$7.lambda$execute$0(CliGitAPIImpl.java:1505) at com.google.common.util.concurrent.TrustedListenableFutureTask$TrustedFutureInterruptibleTask.runInterruptibly(TrustedListenableFutureTask.java:131) at com.google.common.util.concurrent.InterruptibleTask.run(InterruptibleTask.java:74) at com.google.common.util.concurrent.TrustedListenableFutureTask.run(TrustedListenableFutureTask.java:82) at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at com.google.common.util.concurrent.MoreExecutors$DirectExecutorService.execute(MoreExecutors.java:323) at java.base/java.util.concurrent.ExecutorCompletionService.submit(ExecutorCompletionService.java:184) at org.jenkinsci.plugins.gitclient.cgit.GitCommandsExecutor.submitRemainingCommand(GitCommandsExecutor.java:98) at org.jenkinsci.plugins.gitclient.cgit.GitCommandsExecutor.invokeAll(GitCommandsExecutor.java:87) Caused: hudson.plugins.git.GitException at org.jenkinsci.plugins.gitclient.cgit.GitCommandsExecutor.checkResult(GitCommandsExecutor.java:110) at org.jenkinsci.plugins.gitclient.cgit.GitCommandsExecutor.invokeAll(GitCommandsExecutor.java:91) at org.jenkinsci.plugins.gitclient.cgit.GitCommandsExecutor.invokeAll(GitCommandsExecutor.java:49) at org.jenkinsci.plugin
环境信息
- Jenkins版本:2.332.3,通过
java -jar "C:\Program Files\Jenkins\jenkins.war" --version验证 - 插件:
Bitbucket Server Integration版本为3.2.2 - 系统环境变量已配置
GIT_SSH = C:/Windows/System32/OpenSSH/ssh.exe - 已通过PowerShell启用SSH相关服务,执行命令如下:
Set-Service -Name ssh-agent -StartupType Manual Start-Service ssh-agent ssh-add ~\.ssh\id_rsa
需求约束:主仓库可通过HTTP拉取,但子模块必须仅通过SSH方式下载。
已尝试方案
移除Jenkins内置的子模块行为配置,在流水线步骤中手动执行以下命令:
sh 'set GIT_SSH=C:\\Windows\\System32\\OpenSSH\\ssh.exe' sh 'git submodule update --init --recursive'
仍然报相同错误,将路径中\\替换为/也无法解决问题。
求助问题
如何修复error: cannot spawn ssh: No such file or directory错误,实现Jenkins正常通过SSH拉取Git子模块?
内容的提问来源于stack exchange,提问作者mercury
相关产品推荐
相关产品推荐

