Skip to content

Instantly share code, notes, and snippets.

@dsandstrom
Forked from twetzel/deploy.rb
Last active December 26, 2019 22:17
Show Gist options
  • Save dsandstrom/4e6d118b4ca22e0fc7d40d40c5a8f22d to your computer and use it in GitHub Desktop.
Save dsandstrom/4e6d118b4ca22e0fc7d40d40c5a8f22d to your computer and use it in GitHub Desktop.
Compile assets locally with capistrano 3.5.0 and rails 4.2.x (Nginx-Unicorn zero downtime)
# Clear existing task so we can replace it rather than "add" to it.
Rake::Task["deploy:compile_assets"].clear
desc "Precompile assets locally and then rsync to web servers"
task :compile_assets do
# compile assets locally
run_locally do
with rails_env: fetch(:stage) do
execute :bundle, "exec rake assets:precompile"
end
end
# rsync to each server
local_dir = "./public/assets/"
on roles(:web) do
# this needs to be done outside run_locally in order for host to exist
remote_dir = "#{host.user}@#{host.hostname}:#{shared_path}/public/assets/"
run_locally { execute "rsync -av --delete #{local_dir} #{remote_dir}" }
end
# clean up
run_locally { execute "rm -rf #{local_dir}" }
end
@dsandstrom
Copy link
Author

Downloads the manifest file before compiling locally. This way, a new manifest is not generated. This makes the app always use the latest assets and gives Sprockets a complete list of old assets that it can clean up.

@jkarnesPerfectCube
Copy link

jkarnesPerfectCube commented Feb 20, 2017

Consider amending fetch(:user) to fetch(:ssh_options).fetch(:user) to match latest capistrano SSH settings. This is how our SSHKit hash is set up:

# Global options
# --------------
 set :ssh_options, {
   user: "dev",
   keys: %w(/home/justin/.ssh/<REDACTED>),
   forward_agent: true,
   auth_methods: %w(publickey)
   # password: "please use keys"
 }

@dsandstrom
Copy link
Author

Seems to require ungrouping the therubyracer gem, if you use it. I had it in the production group, but had problems precompiling until I moved the gem out of the group.

@dsandstrom
Copy link
Author

@jkarnesPerfectCube Excellent point. I had:

set :user, 'deploy'
set :ssh_options, user: fetch(:user)

so I didn't experience the bug.

@charusat09
Copy link

if anyone using pem file instead of ssh then refer this: https://gist.github.com/charusat09/eda660d4f8cb587d26c937d72d4ffa48

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment