How to make capistrano write a .rvmrc file when deploying?
My git repo has my local rvmrc file in it, and when 开发者_开发知识库I deploy I want to use a different rvm gemeset name etc.
Is there a way I can make capistrano create a .rvmrc file (or overrite it if present) whenever I deploy?
Capistrano's put
command can write a file from a string, as shown here:
desc 'Generate a config yaml in shared path'
task :generate_yaml, :roles => :app do
sphinx_yaml = <<-EOF
development: &base
morphology: stem_en
config_file: #{shared_path}/config/sphinx.conf
test:
<<: *base
production:
<<: *base
EOF
run "mkdir -p #{shared_path}/config"
put sphinx_yaml, "#{shared_path}/config/sphinx.yml"
end
Note: example lifted from Making Your Capistrano Recipe Book
put
is documented in the Capistrano gitub repo
put
method no longer works in Capistrano 3
This solution worked for me
task :generate_yml do
on roles(:app) do
set :db_username, ask("DB Server Username", nil)
set :db_password, ask("DB Server Password", nil)
db_config = <<-EOF
development:
database: #{fetch(:application)}_development
adapter: mysql2
encoding: utf8
reconnect: false
pool: 5
username: #{fetch(:db_username)}
password: #{fetch(:db_password)}
test:
database: #{fetch(:application)}_test
adapter: mysql2
encoding: utf8
reconnect: false
pool: 5
username: #{fetch(:db_username)}
password: #{fetch(:db_password)}
production:
database: #{fetch(:application)}_production
adapter: mysql2
encoding: utf8
reconnect: false
pool: 5
username: #{fetch(:db_username)}
password: #{fetch(:db_password)}
EOF
location = fetch(:template_dir, "config/deploy") + '/database.yml'
execute "mkdir -p #{shared_path}/config"
File.open(location,'w+') {|f| f.write db_config }
upload! "#{location}", "#{shared_path}/config/database.yml"
end
end
Yes, you can write a rake task to write a .rvmrc file, for example, the following Ruby command will execute a bash script that writes rvm 1.9.2@mygemset to .rvmrc:
system "echo 'rvm 1.9.2@mygemset' > .rvmrc"
My recommendation would also be to not store your .rvmrc file in your git repository - this file is really a system specific file and could cause problems for other developers that use different system setups, for example if they use a different gemset then anytime they pull from the central repository they will have to re-write their own .rvmrc files.
It's a late answer, but according to the accepted answer I've created a task in the rvm namespace to do the job. It uses the new ruby-version:
after 'deploy:update_code', 'rvm:create_ruby_version'
namespace :rvm do
task :create_ruby_version do
run "cd #{latest_release} && rvm rvmrc create #{rvm_ruby_string} --ruby-version"
end
end
精彩评论