Skip to content

Instantly share code, notes, and snippets.

@allaire
Last active March 28, 2018 21:53
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allaire/8419898 to your computer and use it in GitHub Desktop.
Save allaire/8419898 to your computer and use it in GitHub Desktop.
Capistrano 3 upload file from erb template
def template(from, to, options = {})
template_path = File.expand_path("../../templates/#{from}", __FILE__)
template = ERB.new(File.new(template_path).read).result(binding)
upload! StringIO.new(template), to, mode: 0644 # Otherwise it's set to 0640, which is not readable by other users (https://github.com/net-ssh/net-scp/blob/master/lib/net/scp/upload.rb#L63)
sudo "chown root:root #{to}" if options[:as_root]
end
Copy link

ghost commented Jun 1, 2016

You saved me from a lot of research!!!

I was getting put method not found with this template function

def template(from, to)
erb = File.read(File.expand_path("../templates/#{from}", FILE))
put ERB.new(erb).result(binding), to
end

THANKS!

@ct1104
Copy link

ct1104 commented Feb 20, 2017

how to understand the erb? eg:
group :development do
gem "capistrano", "~> 3.7"
end

@exocode
Copy link

exocode commented Sep 9, 2017

hehe... @uribefche had exactly the same problem :-)

@IwateKyle
Copy link

Does the upload! method include the 'sudo' prefix in the function call?

@allaire
Copy link
Author

allaire commented Dec 7, 2017

@IwateKyle Nope, you have to edit sudoers file, see https://github.com/capistrano/sshkit/blob/158c2867071e015a117e188d1a0ee7b579c9b24e/lib/sshkit/backends/netssh.rb

@uribefache I was missing a ../, I just fixed it :D

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