You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails7.0.3 Capistrano部署报SSHKit::Runner::ExecuteError错误

您好

环境信息
  • 技术栈版本:Rails 7.0.3、Ruby 3.1.2
  • 部署架构:生产环境、staging预发布环境服务器、代码仓库均部署在自有Linux服务器
  • 前置校验结果:
    • 手动SSH登录生产服务器正常,生产服务器可正常SSH连接代码仓库服务器
    • 生产服务器手动执行git clone拉取代码测试可正常运行
    • 已在生产服务器执行eval "${ssh-agent}"、ssh-add ~/.ssh/id_rsa命令,执行无报错但问题未解决
问题现象

执行部署命令cap staging deploy --trace时部署失败,核心报错出现在git:create_release阶段,执行git archive master时返回fatal: Not a valid object name,后续tar解压流程直接中断,完整报错日志如下:

[...]

00:01 git:update
      01 git remote set-url origin repo-get@my-server.ch:/repos/pro-cnc/cncoffice.git
    ✔ 01 cncoffice@my-customer.com 0.098s
      02 git remote update --prune
      02 Fetching origin
    ✔ 02 cncoffice@my-customer.com 0.268s
** Execute git:create_release
00:01 git:create_release
      01 mkdir -p /srv/office/releases/20220701115330
    ✔ 01 cncoffice@my-customer.com 0.100s
      02 git archive master | /usr/bin/env tar -x -f - -C /srv/office/releases/20220701115330
      02 fatal: Not a valid object name
      02 tar: This does not look like a tar archive
      02 tar: Exiting with failure status due to previous errors
#<Thread:0x000000011398dc08 /Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:10 run> terminated with exception (report_on_exception is true):
/Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:15:in `rescue in block (2 levels) in execute': Exception while executing as cncoffice@my-customer.com: git exit status: 2 (SSHKit::Runner::ExecuteError)
git stdout: Nothing written
git stderr: fatal: Not a valid object name
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors
        from /Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:11:in `block (2 levels) in execute'
/Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/command.rb:97:in `exit_status=': git exit status: 2 (SSHKit::Command::Failed)
git stdout: Nothing written
git stderr: fatal: Not a valid object name
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors
        from /Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/backends/netssh.rb:170:in `execute_command'
        from /Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:148:in `block in create_command_and_execute'
        from <internal:kernel>:90:in `tap'
        from /Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:148:in `create_command_and_execute'
        from /Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:80:in `execute'
        from /Users/dev/.rvm/gems/ruby-3.1.2/gems/capistrano-3.17.0/lib/capistrano/scm/git.rb:83:in `git'
        from /Users/dev/.rvm/gems/ruby-3.1.2/gems/capistrano-3.17.0/lib/capistrano/scm/git.rb:73:in `archive_to_release_path'
        from /Users/dev/.rvm/gems/ruby-3.1.2/gems/capistrano-3.17.0/lib/capistrano/scm/tasks/git.rake:57:in `block (5 levels) in eval_rakefile'
        from /Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:92:in `within'
        from /Users/dev/.rvm/gems/ruby-3.1.2/gems/capistrano-3.17.0/lib/capistrano/scm/tasks/git.rake:55:in `block (4 levels) in eval_rakefile'
        from /Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:100:in `with'
        from /Users/dev/.rvm/gems/ruby-3.1.2/gems/capistrano-3.17.0/lib/capistrano/scm/tasks/git.rake:54:in `block (3 levels) in eval_rakefile'
        from /Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:31:in `instance_exec'
        from /Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:31:in `run'
        from /Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as cncoffice@my-customer.com: git exit status: 2
git stdout: Nothing written
git stderr: fatal: Not a valid object name
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors
/Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:15:in `rescue in block (2 levels) in execute'
/Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:11:in `block (2 levels) in execute'

Caused by:
SSHKit::Command::Failed: git exit status: 2
git stdout: Nothing written
git stderr: fatal: Not a valid object name
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors
/Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/command.rb:97:in `exit_status='
/Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/backends/netssh.rb:170:in `execute_command'
/Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:148:in `block in create_command_and_execute'
<internal:kernel>:90:in `tap'
/Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:148:in `create_command_and_execute'
/Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:80:in `execute'
/Users/dev/.rvm/gems/ruby-3.1.2/gems/capistrano-3.17.0/lib/capistrano/scm/git.rb:83:in `git'
/Users/dev/.rvm/gems/ruby-3.1.2/gems/capistrano-3.17.0/lib/capistrano/scm/git.rb:73:in `archive_to_release_path'
/Users/dev/.rvm/gems/ruby-3.1.2/gems/capistrano-3.17.0/lib/capistrano/scm/tasks/git.rake:57:in `block (5 levels) in eval_rakefile'
/Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:92:in `within'
/Users/dev/.rvm/gems/ruby-3.1.2/gems/capistrano-3.17.0/lib/capistrano/scm/tasks/git.rake:55:in `block (4 levels) in eval_rakefile'
/Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:100:in `with'
/Users/dev/.rvm/gems/ruby-3.1.2/gems/capistrano-3.17.0/lib/capistrano/scm/tasks/git.rake:54:in `block (3 levels) in eval_rakefile'
/Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:31:in `instance_exec'
/Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:31:in `run'
/Users/dev/.rvm/gems/ruby-3.1.2/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'
Tasks: TOP => git:create_release
The deploy has failed with an error: Exception while executing as cncoffice@my-customer.com: git exit status: 2
git stdout: Nothing written
git stderr: fatal: Not a valid object name
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors
** Invoke deploy:failed (first_time)
** Execute deploy:failed

** DEPLOY FAILED
** Refer to log/capistrano.log for details. Here are the last 20 lines:


 DEBUG [ba3e6dbe] Command: if test ! -d /srv/office-staging/repo; then echo "Directory does not exist '/srv/office-staging/repo'" 1>&2; false; fi

 DEBUG [ba3e6dbe] Finished in 0.095 seconds with exit status 0 (successful).

  INFO [3174d347] Running /usr/bin/env git remote set-url origin repo-get@sedlmair.ch:/repos/pro-cnc/cncoffice.git as cncoffice@office.procnc.ch

 DEBUG [3174d347] Command: cd /srv/office-staging/repo && ( export GIT_ASKPASS="/bin/echo" GIT_SSH="/tmp/git-ssh-7f405f262ce173c371af.sh" ; /usr/bin/env git remote set-url origin repo-get@sedlmair.ch:/repos/pro-cnc/cncoffice.git )

  INFO [3174d347] Finished in 0.099 seconds with exit status 0 (successful).

  INFO [f60a0654] Running /usr/bin/env git remote update --prune as cncoffice@office.procnc.ch

 DEBUG [f60a0654] Command: cd /srv/office-staging/repo && ( export GIT_ASKPASS="/bin/echo" GIT_SSH="/tmp/git-ssh-7f405f262ce173c371af.sh" ; /usr/bin/env git remote update --prune )

 DEBUG [f60a0654]   Fetching origin

  INFO [f60a0654] Finished in 0.267 seconds with exit status 0 (successful).

 DEBUG [95047571] Running if test ! -d /srv/office-staging/repo; then echo "Directory does not exist '/srv/office-staging/repo'" 1>&2; false; fi as cncoffice@office.procnc.ch

 DEBUG [95047571] Command: if test ! -d /srv/office-staging/repo; then echo "Directory does not exist '/srv/office-staging/repo'" 1>&2; false; fi

 DEBUG [95047571] Finished in 0.099 seconds with exit status 0 (successful).

  INFO [bd721b6b] Running /usr/bin/env mkdir -p /srv/office-staging/releases/20220701123555 as cncoffice@office.procnc.ch

 DEBUG [bd721b6b] Command: cd /srv/office-staging/repo && ( export GIT_ASKPASS="/bin/echo" GIT_SSH="/tmp/git-ssh-7f405f262ce173c371af.sh" ; /usr/bin/env mkdir -p /srv/office-staging/releases/20220701123555 )

  INFO [bd721b6b] Finished in 0.106 seconds with exit status 0 (successful).

  INFO [31b8821b] Running /usr/bin/env git archive master | /usr/bin/env tar -x -f - -C /srv/office-staging/releases/20220701123555 as cncoffice@office.procnc.ch

 DEBUG [31b8821b] Command: cd /srv/office-staging/repo && ( export GIT_ASKPASS="/bin/echo" GIT_SSH="/tmp/git-ssh-7f405f262ce173c371af.sh" ; /usr/bin/env git archive master | /usr/bin/env tar -x -f - -C /srv/office-staging/releases/20220701123555 )

 DEBUG [31b8821b]   fatal: Not a valid object name

 DEBUG [31b8821b]   tar: This does not look like a tar archive

tar: Exiting with failure status due to previous errors
故障原因

这个报错和SSH权限、ssh-agent配置没有关系,前面的git remote update --prune已经执行成功,说明服务器到代码仓库的网络、鉴权全是正常的。问题出在git archive master这一步找不到master分支的有效提交对象,常见两种触发场景:

  • 代码仓库默认分支已经从早年Git默认的master改成了main(Git 2.28之后新建仓库默认分支名就是main),但Capistrano没有做对应配置,默认还在查找master分支
  • 服务器上留存的Capistrano缓存裸仓库(路径/srv/office-staging/repo)是历史遗留的旧目录,分支引用损坏,哪怕执行了remote update也没有同步到正确的分支提交对象
解决步骤

按顺序操作即可修复:

  1. 检查本地项目的Capistrano配置,打开config/deploy.rb或者staging环境专属的config/deploy/staging.rb,找到:branch配置项。如果没有手动配置过,Capistrano默认会使用master作为部署分支,直接改成仓库实际使用的分支名即可,比如现在常用的main:
    set :branch, 'main'
    
    不确定分支名的话,就在本地项目根目录执行git branch查看当前所在分支,或者登录服务器进入之前手动clone成功的仓库目录,执行git branch -a列出所有远程分支核对。
  2. 如果配置里的分支名本来就是正确的,直接登录staging服务器,删除旧的缓存仓库目录即可,Capistrano下次部署会自动重新拉取完整仓库:
    rm -rf /srv/office-staging/repo
    
  3. 回到本地项目目录,重新执行部署命令:
    cap staging deploy
    

感谢,
Chris


内容的提问来源于stack exchange,提问作者chmich

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 01:27:28