新手问题 关于在 capistrano 的 deploy.rb 中 task 的问题

geektony · October 18, 2014 · Last by ensonmj replied at October 21, 2014 · 2286 hits
  • 如何实现 database.yml 自动创建和设定
  • 如何实现在 git clone 后实现 db:create 和 db:migrate
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
end

请问,是在这里添加,还是开一个新的 namespace 去做这些事情 但我是希望能在 deploy 的过程中实现自动化

谢谢

如果只针对于一个项目,自定义的 task 不多,直接写在 deploy.rb 里就行了。 简单来说 2 个步骤,1.创建自定义 task 2.自定义 task 插入已有的 task 流之中。

举例:创建 database.yml 创建 database.yml 的 task 一般来说就是做一个符号链接,先准备好 database.yml 放在某个目录下: 然后增加一个 task,比如:

# 增加一个自定义task
task :link_database_config_file do
  on roles(:app) do
    execute :ln, '-s', 'your database config file path', "#{release_path}/config/database.yml"
  end
end

# 把自定义的task插入到task流中,这里我们把自定义的task插入到update task之前
before :updated, 'deploy:link_database_config_file'

#这样自定义的这个动作就会在task流执行到update之前执行

想了解 task 执行的顺序参考:http://capistranorb.com/documentation/getting-started/flow/

还有就是 capistrano-rails 这个 gem 已经包含了 db:migrate task 只要编辑 Capfile

# 把这行注释去掉就行了
require 'capistrano/rails/migrations'

至于 db:create,只是第一次部署需要执行一次,手工完成就好了。

@beiersi 太感动了,看前先答谢一下!

如果使用 postgres 的话,有个 gem capistrano-postgresql 可以自动创建 database.yml

You need to Sign in before reply, if you don't have an account, please Sign up first.