在Capistrano版本3中使用run_locally创建和调用自定义部署任务

时间:2014-04-15 14:50:53

标签: capistrano capistrano3

我有一个静态页面,我想用gulp在本地编译。我将在本地shell中运行的命令,从包含gulp的目录和gulpfile(在本例中由compile_path设置)将是“$> gulp build”。

# config valid only for Capistrano 3.1
lock '3.1.0'  

set :application, 'appname'
set :repo_url, 'git@bitbucket.org/appname.git'
set :compile_path, '/Users/nico/DevOps/repo/appname'

# Default branch is :master
set :branch, 'cap3'

namespace :deploy do

  after :started, :notify do 
    desc 'Run gulp to compile the static site'
    task :gulp_build do
      run_locally do
        execute "#{fetch(:compile_path)}/gulp",  " build"
      end
    end  
  end 

  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

基本上,我想要实现的是本地预编译,因此我的部署只需将本地编译的文件发送到部署位置。当我执行“bundle exec cap staging deploy:gulp_build”时,我得到:

盖帽中止了! 不知道如何构建任务'deploy:gulp_build' /Users/nico/.rvm/gems/ruby-1.9.3-p545/gems/capistrano-3.1.0/lib/capistrano/application.rb:15:in run' /Users/nico/.rvm/gems/ruby-1.9.3-p545/gems/capistrano-3.1.0/bin/cap:3:in' /Users/nico/.rvm/gems/ruby-1.9.3-p545/bin/cap:23:in load' /Users/nico/.rvm/gems/ruby-1.9.3-p545/bin/cap:23:in' /Users/nico/.rvm/gems/ruby-1.9.3-p545/bin/ruby_executable_hooks:15:in eval' /Users/nico/.rvm/gems/ruby-1.9.3-p545/bin/ruby_executable_hooks:15:in' (通过使用--trace运行任务查看完整跟踪)

我意识到可能有更好的方法来部署它,但它是通过capistrano成功部署的rails应用程序的伴随静态站点,我想对两者使用相同的部署方法。 / p>

1 个答案:

答案 0 :(得分:4)

通过在deploy命名空间中创建新任务,可以很好地处理这个问题。在我的下面的代码中是实际值的占位符,我不想在SO上发布。

LIB / Capistrano的/任务/ gulp_build_local.cap:

#assumes the gulpfile is in root of your cap install
namespace :deploy do
    desc 'Run gulp to compile the static site'
    task :gulp_build do
    #run_locally doesn't play nice with the 'on' directive (it's 'on' localhost)
      run_locally do
        execute "gulp build"
      end
    end  
  end 

deploy.rb:

# config valid only for Capistrano 3.1
lock '3.1.0'

set :application, '<appname>'
set :repo_url, 'git@bitbucket.org/<appname>.git'

namespace :deploy do

 #custom tasks to build via gulp
 before :deploy, 'gulp_build_local' 

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      #nothing here, because there's no app server for this static site.
    end
  end

  after :publishing, :restart

  after :restart, :clear_cache do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
       #nothing here
    end
  end

end

当然,一旦我弄清楚这一点,我立即弃用它,支持新任务在目标上的发布目录中安装gulp,在那里编译并将站点根链接到gulp进程生成的pub文件夹。希望这种学习体验对于通过run_locally使用工作的人来说非常有用。