简体   繁体   中英

Capistrano - deploying one app to same server twice, for staging and production

The documentation seems a little sparse on what this means.

I'm trying to set up an app deployed by a multistage capistrano script.

EDIT: I am trying to deploy the same app, twice to the same server. The only real difference (other than the git branches) is I want to deploy each copy to a different folder. The first instance is a staging where I can test the app in the exact same environment as the second instance, which is a production instance. Is capistrano able to do this?

I ran the staging deployment without any issues. However, when I run any tasks specifying my production stage, (such as deploy:setup, in this case) I receive the following error:

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

Here's my 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

My 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}" 

My 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}" 

My question is, why is this being set? Additionally, what can I do to avoid setting this variable so I can use the deploy commands.

Let me know if I've missed any pertinent information.

I have solved the problem for now by replacing the following line:

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

with:

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

in my production.rb file.

However, this is a hacky solution, and I'd like to understand how I should properly deploy a rails app twice to the same server, for staging and production purposes. Or alternatively, why I should not do this, and what the alternatives are. So, I have left the question unanswered.

I am attempting to expand on this helpful answer by providing a tutorial that includes its steps, as well as setting up a self-hosted git repository instead of github and deployment scripts for staging and production versions of the app on the same server. So this question answers a pivotal piece of this process. I'm trying to get an idea of what the best practices are for a situation like this.

Comments welcomed; I'll add them to this answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM