Question

I have a static page that I want to compile locally using gulp. The command I would run in the local shell, from the directory that contains gulp and the gulpfile (set by compile_path in this example) would be "$> 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

Basically, what I'm trying to achieve is a local precompile so that my deployment consists of simply sending the locally compiled files to a deployment location. when I execute "bundle exec cap staging deploy:gulp_build" I get:

cap aborted! Don't know how to build task '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' (See full trace by running task with --trace)

I realize that there are probably much better ways to deploy this, but it's a companion static site to a rails app which is being deployed successfully via capistrano, and I'd like to just use the same deployment method for both.

Was it helpful?

Solution

This was handled pretty well by creating a new task in the deploy namespace. in my code below are placeholders for real values that I didn't want to post on 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 

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

of course, once I figured this out I immediately deprecated it in favor of new tasks to install gulp in the release dir on the target, compiling there and linking the site root to the pub folder generated by the gulp process. Hopefully this learning experience will be useful for someone working though the use of run_locally, though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top