Deploying Static Websites With Net::SCP
I recently had a couple of projects that consisted of a few static HTML files and a couple images. I could have used a blog or lightweight CMS, but those were overkill. I could use
scp, but remembering the
scp command every time I made a small change proved to be a hassle. I could have used Capistrano, but that too, felt overboard for my simple needs. Lucky for me, in addition to Capistrano, Jamis Buck wrote Net::SCP which was the perfect tool for the job.
From the Net:SCP documentation:
”Net::SCP implements the SCP (Secure CoPy) client protocol, allowing Ruby programs to securely and programmatically transfer individual files or entire directory trees to and from remote servers. It provides support for multiple simultaneous SCP copies working in parallel over the same connection, as well as for synchronous, serial copies.”
The only thing we’re interested in is uploading, so lets get down to the code which I’ve packaged as a rake task.
REMOTE_DIR = "/home/caged/public/website.com/public" desc 'Upload the site' task :deploy do Net::SCP.start("hostname", "caged", :port => 3000) do |scp| scp.upload! 'site/index.html', REMOTE_DIR scp.upload! 'site/stylesheets', REMOTE_DIR, :recursive => true scp.upload! 'site/images', REMOTE_DIR, :recursive => true scp.upload! 'site/article', REMOTE_DIR, :recursive => true end end
Now lets take a look at the code. If you’re not using a public-key authentication, you’ll need to set
:password in the
start method. The
upload! method is a synchronous (blocking) upload. If you’re uploading a lot of large files, you should consider using
upload (no !) which will return immediately and continue processing your script while the upload is in progress. Finally, instead of specifying the name of every file inside a directory, you can set
:recursive => true and Net::SCP will create the directory on the remote server and upload the files contained in it.
That’s it, the no-frills way to deploy a small static website.