Ruby deploy with Capistrano

hiveer · 2014年04月03日 · 最后由 ctwyw 回复于 2016年03月11日 · 4522 次阅读

目前正在学习用 Capistrano3.1.0 来发布一个测试项目,发布的目标服务器是局域网内的一台机子,然后代码在 github。在完成了所有配置之后,运行了 cap production deploy,结果如下

rvm 1.25.20 (master) by Wayne E. Seguin <[email protected]>, Michal Papis <[email protected]> [https://rvm.io/]
ruby-2.1.1
ruby 2.1.1p76 (2014-02-24 revision 45161) [i686-linux]
 INFO [ffe2487d] Running /usr/bin/env mkdir -p /tmp/QYBUser/ on 192.168.1.4
 INFO [ffe2487d] Finished in 0.128 seconds with exit status 0 (successful).
 INFO Uploading /tmp/QYBUser/git-ssh.sh 100.0%
 INFO [7d5305fc] Running /usr/bin/env chmod +x /tmp/QYBUser/git-ssh.sh on 192.168.1.4
 INFO [7d5305fc] Finished in 0.122 seconds with exit status 0 (successful).

没有报错,但是项目根本没有发布上去...... 配置如下: Capfile

# Load DSL and Setup Up Stages
require 'capistrano/setup'

# Includes default deployment tasks
require 'capistrano/deploy'

# Includes tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
#   https://github.com/capistrano/rvm
#   https://github.com/capistrano/rbenv
#   https://github.com/capistrano/chruby
#   https://github.com/capistrano/bundler
#   https://github.com/capistrano/rails
#
require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
require 'capistrano/rails'
# require 'capistrano/rails/assets'
# require 'capistrano/rails/migrations'
require 'whenever/capistrano'

# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
Dir.glob('lib/capistrano/**/*.rb').each { |r| import r }

unicorn

working_directory = "#{deploy_to}/#{application}/current"
app_path working_directory

#listen "/tmp/#{application}.sock", :backlog => 256
listen "127.0.0.1:8080"

timeout 300


pid "#{deploy_to}/#{application}/shared/pids/unicorn.pid"

stderr_path "#{deploy_to}/#{application}/shared/log/unicorn.stderr.log"
stdout_path "#{deploy_to}/#{application}/shared/log/unicorn.stdout.log"

preload_app true

if GC.respond_to?(:copy_on_write_friendly=)
  GC.copy_on_write_friendly = true
end

before_fork do |server, worker|
  ENV['BUNDLE_GEMFILE'] = "#{working_directory}/Gemfile"
  old_pid = "#{deploy_to}/#{application}/shared/pids/unicorn.pid.oldbin"
  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
      puts "Send 'QUIT' signal to unicorn error!"
    end
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
  ActiveRecord::Base.establish_connection
end

config/deploy/production.rb

set :stage, :production
set :branch, :master

set :rvm_type, :system
set :rvm_ruby_version , '2.1.1'

#role :app, %w{192.168.1.4}
#role :web, %w{192.168.1.4}
#role :db,  %w{192.168.1.4}

# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server
# definition into the server list. The second argument
# something that quacks like a hash can be used to set
# extended properties on the server.
set :full_app_name, "#{fetch(:application)}"

server '192.168.1.4', user: 'deployer', roles: %w{web app db}, primary: true

set :deploy_to, "/var/projects/QYBUser"
set :rails_env, :production
set :unicorn_worker_count, 5
set :enable_ssl, false

conifg/deploy.rb

# config valid only for Capistrano 3.1
lock '3.1.0'

set :application, 'QYBUser'
set :deploy_user, 'deployer'

set :repo_url, '[email protected]:cnepaycd/QYBUser.git'

# Default branch is :master
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }

# Default deploy_to directory is /var/www/my_app
# config in stage file

# Default value for :scm is :git
set :scm, :git

# Default value for :format is :pretty
# set :format, :pretty

# Default value for :log_level is :debug
set :log_level, :info

# how many old releases do we want to keep, not much
set :keep_releases, 5

# Default value for :pty is false
# set :pty, true

# Default value for :linked_files is []
set :linked_files, %w{config/database.yml}

# Default value for linked_dirs is []
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}

SSHKit.config.command_map[:rake]  = "bundle exec rake" #8
SSHKit.config.command_map[:rails] = "bundle exec rails"
namespace :deploy do

  #desc 'Restart application'
  #task :restart do
  #  on roles(:app), in: :sequence, wait: 5 do
      # Your restart mechanism here, for example:
      # execute :touch, release_path.join('tmp/restart.txt')
  #  end
  #end

  #after :publishing, :restart

  #after :restart, :clear_cache do
  #  on roles(:web), in: :groups, limit: 3, wait: 10 do
      # Here we can do anything such as:
      # within release_path do
      #   execute :rake, 'cache:clear'
      # end
  #  end
  #end
  after :finishing, 'deploy:cleanup'
end

目前无解中......求各位高手不吝解答

capistrano 3 源码清晰简洁了很多,但是感觉没有 2 好用,很多好用的功能都去掉了,而且文档写的也不是很详细,试了下还是用回了 2

@diudiutang 听了你这番话,感觉心凉凉的呢!在我回到 2.x 之前,至少要发布成功,然后对比下

在各位热心朋友的帮助下,问题解决了 set :log_level, :debug,这样就看到了报错的地方了,然后有错误提示就好办啦

#1 楼 @diudiutang Capistrano 3 其实定位更清晰了,提供更好的架构,更小的核心。把具体功能模块独立到第三方 gem 中。跟 Rails 2 到 3 时候做的事情是一样的。

3 用起来顺手方便多了

有没有办法用 capistrano 发不到本地,如果 staging.rb 不配置 role 或者 server 根本就不能用 T-T

capistrano 发布到其他服务器是可以实现了,但是发不到本地测试环境不行 用 gitlab hook 做了钩子 想用 capi 统一做发布, 先 merge develop 发布到本地,然后通过 merge master hook 发布到服务器。但是本地行不通啊

需要 登录 后方可回复, 如果你还没有账号请 注册新账号