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

Rails 7+Puma 6+Mina部署异常:符号链接无法更新至新版本

Debian 11下Mina部署Rails 7+Puma 6时current符号链接未更新的问题

环境与现象

  • 系统:Debian 11
  • 架构:Rails 7 + Puma 6,使用systemd管理服务
  • 问题:mina deploy流程无报错,mina deploy verbose显示已执行ln -nfs命令,但服务器上执行ls -la /home/deploy/production/app时,current符号链接仍指向首个发布版本/home/deploy/production/app/releases/1

提供的deploy.rb代码片段

require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/version_managers/rbenv'

set :forward_agent, true
set :rbenv_use_path, '/home/deploy/.rbenv/bin/rbenv'

set :shared_dirs, fetch(:shared_dirs, []).push('log', 'tmp/pids', 'tmp/sockets', 'public/assets')
set :shared_files, fetch(:shared_files, []).push('config/database.yml', 
                                                'config/master.key', 
                                                'config/credentials/production.key' )

task :remote_environment do
  invoke :'rbenv:load'
end

task :setup do
  
  in_path(fetch(:shared_path)) do
    command %[mkdir -p config]
    command %[touch "#{fetch(:shared_path)}/config/database.yml"]
    command %[touch "#{fetch(:shared_path)}/config/puma.rb"]
    command %[chmod -R o-rwx config]
  end

end

desc "Deploys the current version to the server."
task :deploy do
  deploy do
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:db_migrate'
    invoke :'rails:assets_precompile'
    invoke :'deploy:cleanup'

    on :launch do
      in_path(fetch(:current_path)) do
        command %{mkdir -p tmp/}
        command %{touch tmp/restart.txt}
        command %{sudo service puma restart}
        command %{sudo service sidekiq restart}
      end
    end
  end
end

排查与解决步骤

1. 补全Mina核心部署路径配置

你的deploy.rb缺少Mina部署必需的核心路径配置,导致Mina可能在错误目录下执行符号链接操作:

  • 在deploy.rb开头添加以下配置(替换为你的实际信息):
    set :application_name, 'your_app_name' # 你的应用名称
    set :domain, 'your_server_ip_or_domain' # 服务器IP或域名
    set :deploy_to, '/home/deploy/production/app' # 你的部署根目录
    set :repository, 'git@your-git-host:your-username/your-repo.git' # Git仓库地址
    set :branch, 'main' # 要部署的分支
    
  • 说明:deploy_to是Mina确定releases、current、shared目录位置的核心配置,缺失时Mina会使用默认路径/var/www/[application_name],导致你查看的current目录和Mina操作的目录不一致。

2. 添加Mina内置的deploy:create_symlink步骤

你的deploy流程中缺少负责更新current符号链接的核心步骤:

  • 在rails:assets_precompile之后、deploy:cleanup之前添加invoke :'deploy:create_symlink',修改后的deploy块如下:
    task :deploy do
      deploy do
        invoke :'git:clone'
        invoke :'deploy:link_shared_paths'
        invoke :'bundle:install'
        invoke :'rails:db_migrate'
        invoke :'rails:assets_precompile'
        invoke :'deploy:create_symlink' # 新增此行,负责更新current符号链接
        invoke :'deploy:cleanup'
    
        on :launch do
          in_path(fetch(:current_path)) do
            command %{mkdir -p tmp/}
            command %{touch tmp/restart.txt}
            command %{sudo service puma restart}
            command %{sudo service sidekiq restart}
          end
        end
      end
    end
    
  • 说明:Mina的deploy:create_symlink是官方标准的current符号链接更新步骤,之前的流程漏掉这一步,导致符号链接从未真正更新。

3. 检查部署目录的权限

如果部署目录的所属用户不是Mina使用的deploy用户,ln命令会静默失败:

  • 登录服务器,执行以下命令检查目录权限:
    ls -ld /home/deploy/production/app
    
  • 如果输出的所有者不是deploy:deploy,执行以下命令修正权限:
    sudo chown -R deploy:deploy /home/deploy/production/app
    

4. 手动验证符号链接更新

如果以上步骤仍未解决,可手动执行符号链接命令验证:

  • 找到最新的release目录,比如/home/deploy/production/app/releases/2
  • 执行命令:
    ln -nfs /home/deploy/production/app/releases/2 /home/deploy/production/app/current
    
  • 执行ls -la /home/deploy/production/app查看current是否更新:
    • 手动执行成功:问题出在Mina流程配置上
    • 手动执行失败:检查是否有进程占用目录或权限问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 09:58:10