Skip to content

Instantly share code, notes, and snippets.

@vuongpd95
Last active February 15, 2021 01:34
Show Gist options
  • Save vuongpd95/a0b7c8d2f4bc8983ba4b475eb5d06518 to your computer and use it in GitHub Desktop.
Save vuongpd95/a0b7c8d2f4bc8983ba4b475eb5d06518 to your computer and use it in GitHub Desktop.
Local Precompile Assets for low spec VM
# In deploy.rb
# require_relative '../lib/mina/local_assets/tasks'
# ...
# task :deploy do
# # uncomment this line to make sure you pushed your local branch to the remote origin
# # invoke :'git:ensure_pushed'
# invoke :'local_assets:clobber'
# invoke :'local_assets:precompile'
# invoke :'local_assets:sync_to_remote_tmp'
# ...
# deploy do
# ...
# invoke :'rails:db_migrate'
# invoke :'local_assets:sync_to_public'
# invoke :'deploy:cleanup'
# ...
# end
# invoke :'local_assets:clobber'
# end
# lib/mina/local_assets/tasks.rb
namespace :local_assets do
desc "On local: Compile assets locally."
task :precompile do
run(:local) do
comment "Compile assets locally"
command "NODE_ENV=#{fetch(:rails_env)} RAILS_ENV=#{fetch(:rails_env)} bundle exec rake assets:precompile"
end
end
desc "On local: Sync assets to a tmp folder on remote."
task :sync_to_remote_tmp do
run(:local) do
comment "Rsync assets to a tmp folder on remote server"
command %(ssh #{fetch(:user)}@#{fetch(:domain)} mkdir -p #{fetch(:assets_tmp_dir)})
['assets', 'packs'].each do |dir|
command %(
rsync -Pavz --delete -e "ssh -p #{fetch(:port)}" \
public/#{dir} #{fetch(:user)}@#{fetch(:domain)}:#{fetch(:assets_tmp_dir)}
)
end
end
end
desc "On remote: sync assets from tmp folder to public folder."
task :sync_to_public do
comment "Rsync assets directories to public folder"
['assets', 'packs'].each do |dir|
command %(
rsync -av --delete #{fetch(:assets_tmp_dir)}/#{dir}/ \
#{fetch(:deploy_to)}/shared/public/#{dir}
)
end
end
desc "On local: Clobber assets of remote environment locally."
task :clobber do
run(:local) do
comment "Clobber existing local assets"
command "yarn install"
command "bundle exec rake assets:clobber"
comment "Finished clobbering local assets!"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment