【问题标题】:Creating and calling custom deployment task using run_locally in Capistrano version 3在 Capistrano 版本 3 中使用 run_locally 创建和调用自定义部署任务
【发布时间】:2014-05-30 00:23:54
【问题描述】:

我有一个静态页面,我想使用 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 应用程序的配套静态站点,我想对两者使用相同的部署方法。

【问题讨论】:

  • 我能够通过在 /lib/capistrano/tasks/gulp_build.cap 中创建一个新任务来解决这个问题,将其放入“deploy”命名空间并从 deploy.rb 调用它:之前:部署,“部署:gulp_build”。当计时器到期时,我会回答我自己的问题。此外,编辑问题标题以更好地反映所做的工作。

标签: capistrano capistrano3


【解决方案1】:

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

lib/capistrano/tasks/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 

部署.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 工作的人有用。

【讨论】:

    猜你喜欢
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 2014-02-09
    相关资源
    最近更新 更多