Capistrano - 将一个应用程序部署到同一台服务器两次,用于暂存和生产

时间:2013-11-12 04:35:00

标签: ruby-on-rails ruby-on-rails-4 capistrano

这意味着文档似乎有点稀疏。

我正在尝试设置一个由多级capistrano脚本部署的应用程序。

编辑:我正在尝试将同一个应用程序部署到同一台服务器两次。唯一真正的区别(除了git分支)是我想将每个副本部署到不同的文件夹。第一个实例是一个暂存,我可以在与第二个实例完全相同的环境中测试应用程序,第二个实例是一个生产实例。卡皮斯特拉诺能够做到这一点吗?

我没有任何问题地运行了暂存部署。但是,当我运行任何指定我的生产阶段的任务时(例如deploy:setup,在这种情况下)我收到以下错误:

`deploy:setup' is only run for servers matching {:except=>{:no_release=>true}}, but no servers matched

这是我的Deploy.rb

require "rvm/capistrano"
require "bundler/capistrano"
require "capistrano/ext/multistage"

#server "direct.measuremyho.me", :web, :app, :db, primary: true

set :stages, %w{staging production} # Set staging and production environment
set :default_stage, "staging" # Use staging environment as the default one to prevent accidentally deploying to production 

set :application, "mmh"
set :user, "mmh"
set :deploy_via, :remote_cache
#set :deploy_to, "/var/www/#{application}"
set :use_sudo, false
set :keep_releases, 3

set :scm, "git"
set :repository, "git@localhost:#{application}.git"
set :local_repository, "git@direct.measuremyho.me:#{application}.git"
#set :branch, "master"

default_run_options[:pty] = true
ssh_options[:forward_agent] = true

after "deploy:update_code", "deploy:migrate"
after "deploy", "deploy:cleanup"

namespace :deploy do
  %w[start stop].each do |command|
    desc "#{command} nginx server"
    task command, roles: :app, except: {no_release: true} do
      sudo "#{try_sudo} service nginx #{command}"
    end
  end

  desc "restart passenger server"
  task :restart, roles: :app, except: { no_release: true } do
    run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
  end

  task :setup_config, roles: :app do
    # link the nginx config file in the app
    # sudo "ln -nfs #{current_path}/config/nginx.conf "\
    #      "/opt/nginx/conf/nginx.conf"
    # make the shared rails config directory
    run "mkdir -p #{shared_path}/config"
    # ftp the database.yml file to that directory
    put File.read("config/database.yml"), "#{shared_path}/config/database.yml"
    # make the shared uploads directory
    run "mkdir -p #{shared_path}/uploads"
    # tell the user to edit database.yml
    puts "==> IMPORTANT!!! Now edit database.yml in "\
         "#{shared_path}/config <==="
  end

  after "deploy:setup", "deploy:setup_config"

  task :symlink_config, roles: :app do

    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
    run "rm -rf #{release_path}/public/uploads"
    run "ln -nfs #{shared_path}/uploads #{release_path}/public/"
  end

  after "deploy:finalize_update", "deploy:symlink_config"

end

我的staging.rb

set :application_directory, "staging"
set :rails_env, "staging"
set :main_server, 'direct.measuremyho.me'
set :branch do
  default_tag = `git tag`.split("\n").last
  tag = Capistrano::CLI.ui.ask "Tag to deploy (make sure to push the tag first): [#{default_tag}] "
  tag = default_tag if tag.empty?
  branch = "release/#{tag}"
  branch
end

# Do not modify
# Set up the server
server "#{main_server}", :web, :app, :db, :primary => true
set :deploy_to, "/var/www/mmh/#{application_directory}" 

我的production.rb

set :application_directory, "production"
set :rails_env, "production"
set :main_server, 'direct.measuremyho.me'
set :branch, "master"

# Do not modify
# Set up the server
server "#{main_server}", :web, :app, :db, :primary => true
set :deploy_to, "/var/www/mmh/#{application_directory}" 

我的问题是,为什么要这样设定?此外,我该怎么做才能避免设置此变量,以便我可以使用deploy命令。

如果我错过了任何相关信息,请告诉我。

1 个答案:

答案 0 :(得分:3)

我现在通过替换以下行解决了这个问题:

server "#{main_server}", :web, :app, :db, :primary => true

使用:

server "#{main_server}", :web, :app, :db, :primary => true, :no_release => false

在我的production.rb文件中。

然而,这是一个hacky解决方案,我想了解如何将rails应用程序两次正确部署到同一台服务器,以便进行分段和生产。或者,为什么我不应该这样做,以及替代方案是什么。所以,我没有回答这个问题。

我正在尝试通过提供包含其步骤的教程以及为应用程序的暂存和生产版本设置自托管git存储库而不是github和部署脚本来扩展this有用的答案在同一台服务器上。所以这个问题回答了这个过程的关键部分。我试图了解这种情况的最佳实践。

欢迎评论;我会将它们添加到这个答案中。