-
-
Save robotzhang/95c8daeb9f92be83d7292558c18ef028 to your computer and use it in GitHub Desktop.
Compile assets locally with Capistrano 3.8.1 and Rails 5.1.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!! | |
# put the file to /lib/capistrano/tasks | |
# Resolve the conflict between local precompile as production mode and webpack/hot/dev-server | |
# you must edit config/webpacker.yml | |
# development: | |
# <<: *default | |
# public_output_path: packs-dev | |
# | |
# Clear existing task so we can replace it rather than "add" to it. | |
Rake::Task["deploy:compile_assets"].clear | |
namespace :deploy do | |
desc 'Compile assets' | |
task :compile_assets => [:set_rails_env] do | |
# invoke 'deploy:assets:precompile' | |
invoke 'deploy:assets:precompile_local' | |
invoke 'deploy:assets:backup_manifest' | |
end | |
namespace :assets do | |
desc "Precompile assets locally and then rsync to web servers" | |
task :precompile_local do | |
# compile assets locally | |
run_locally do | |
execute "RAILS_ENV=#{fetch(:stage)} bundle exec rake assets:precompile" | |
end | |
# rsync to each server | |
dirs = { | |
'./public/assets/' => 'public/assets/', | |
'./public/packs/' => 'public/packs/' | |
} | |
on roles( fetch(:assets_roles, [:web]) ) do | |
# this needs to be done outside run_locally in order for host to exist | |
dirs.each do |local, remote| | |
remote_dir = "#{host.user}@#{host.hostname}:#{release_path}/#{remote}" | |
run_locally { execute "rsync -av --delete #{local} #{remote_dir}" } | |
end | |
end | |
# clean up | |
run_locally { execute "rm -rf #{dirs.keys.join(" ")}" } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment