Skip to content

Instantly share code, notes, and snippets.

@maciej
Created February 6, 2013 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maciej/4721525 to your computer and use it in GitHub Desktop.
Save maciej/4721525 to your computer and use it in GitHub Desktop.
Capistrano and multimodule projects
load 'deploy' unless defined?(_cset)
_cset :asset_env, "RAILS_GROUPS=assets"
_cset :assets_prefix, "assets"
_cset :assets_role, [:web]
_cset :normalize_asset_timestamps, false
before 'deploy:finalize_update', 'deploy:mm_assets:symlink'
after 'deploy:update_code', 'deploy:mm_assets:precompile'
namespace :deploy do
namespace :mm_assets do
desc <<-DESC
[internal] This task will set up a symlink to the shared directory \
for the assets directory. Assets are shared across deploys to avoid \
mid-deploy mismatches between old application html asking for assets \
and getting a 404 file not found error. The assets cache is shared \
for efficiency. If you customize the assets path prefix, override the \
:assets_prefix variable to match.
DESC
task :symlink, :roles => assets_role, :except => { :no_release => true } do
run <<-CMD
rm -rf #{app_path}/public/#{assets_prefix} &&
mkdir -p #{app_path}/public &&
mkdir -p #{shared_path}/assets &&
ln -s #{shared_path}/assets #{app_path}/public/#{assets_prefix}
CMD
end
desc <<-DESC
Run the asset precompilation rake task. You can specify the full path \
to the rake executable by setting the rake variable. You can also \
specify additional environment variables to pass to rake via the \
asset_env variable. The defaults are:
set :rake, "rake"
set :rails_env, "production"
set :asset_env, "RAILS_GROUPS=assets"
DESC
task :precompile, :roles => assets_role, :except => { :no_release => true } do
run "cd #{app_path} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile"
end
desc <<-DESC
Run the asset clean rake task. Use with caution, this will delete \
all of your compiled assets. You can specify the full path \
to the rake executable by setting the rake variable. You can also \
specify additional environment variables to pass to rake via the \
asset_env variable. The defaults are:
set :rake, "rake"
set :rails_env, "production"
set :asset_env, "RAILS_GROUPS=assets"
DESC
task :clean, :roles => assets_role, :except => { :no_release => true } do
run "cd #{app_path} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:clean"
end
end
end
module Bundler
class Deployment
def self.define_task(context, task_method = :task, opts = {})
if defined?(Capistrano) && context.is_a?(Capistrano::Configuration)
context_name = "capistrano"
role_default = "{:except => {:no_release => true}}"
error_type = ::Capistrano::CommandError
else
context_name = "vlad"
role_default = "[:app]"
error_type = ::Rake::CommandFailedError
end
roles = context.fetch(:bundle_roles, false)
opts[:roles] = roles if roles
context.send :namespace, :bundle do
send :desc, <<-DESC
Install the current Bundler environment. By default, gems will be \
installed to the shared/bundle path. Gems in the development and \
test group will not be installed. The install command is executed \
with the --deployment and --quiet flags. If the bundle cmd cannot \
be found then you can override the bundle_cmd variable to specifiy \
which one it should use.
You can override any of these defaults by setting the variables shown below.
N.B. bundle_roles must be defined before you require 'bundler/#{context_name}' \
in your deploy.rb file.
set :bundle_gemfile, "Gemfile"
set :bundle_dir, File.join(fetch(:shared_path), 'bundle')
set :bundle_flags, "--deployment --quiet"
set :bundle_without, [:development, :test]
set :bundle_cmd, "bundle" # e.g. "/opt/ruby/bin/bundle"
set :bundle_roles, #{role_default} # e.g. [:app, :batch]
DESC
send task_method, :install, opts do
bundle_cmd = context.fetch(:bundle_cmd, "bundle")
bundle_flags = context.fetch(:bundle_flags, "--deployment --quiet")
bundle_dir = context.fetch(:bundle_dir, File.join(context.fetch(:shared_path), 'bundle'))
bundle_gemfile = context.fetch(:bundle_gemfile, "Gemfile")
bundle_without = [*context.fetch(:bundle_without, [:development, :test])].compact
app_path = context.fetch(:app_path)
if app_path.to_s.empty?
raise error_type.new("Cannot detect current release path - make sure you have deployed at least once.")
end
args = ["--gemfile #{File.join(app_path, bundle_gemfile)}"]
args << "--path #{bundle_dir}" unless bundle_dir.to_s.empty?
args << bundle_flags.to_s
args << "--without #{bundle_without.join(" ")}" unless bundle_without.empty?
run "cd #{app_path} && #{bundle_cmd} install #{args.join(' ')}"
end
end
end
end
end
before "deploy:finalize_update", "bundle:install"
Bundler::Deployment.define_task(self, :task, :roles => [:app])
set :rake, lambda { "#{fetch(:bundle_cmd, "bundle")} exec rake" }
set :frontend_app, "jbison-frontend"
set :app_path, lambda { "#{fetch(:latest_release)}/#{fetch(:frontend_app)}" }
set :shared_children, ["public/system", "log", "tmp/pids"].map { |item| "#{frontend_app}/#{item}" }
load "config/recipes/multimodule_assets"
load "config/recipes/multimodule_bundler"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment