Mar 23 2009
Use Rails to Create a Static Site: Rake and Subversion
We have already seen how to Use Rails to Create a Static Site. In that article we left the site running, and we recommended the use of wget to generate the static copy. Although this is good enough, with a little bit of Rake we can make deployment easier.
The final goal is to have an automated process to generate fresh static copy of the site and to upload it to the subversion repository. Although more advanced solutions exist (i.e. Capistrano), I used a 22 line Rake script for dradisframework.org. There are two tasks:-
prepare: useswgetto generate a static copy of the site in the./out/directory.commit: commits the changes in the./out/directory to the subversion repository.
You would only need to synchronise your web server document root with the ./out/ directory of the subversion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | desc 'Prepare the static site in the ./out/ directory' task :prepare do Dir.mkdir('out') unless File.exist?('out') Dir.chdir('out') do `wget -m -nH http://localhost:3000` end `rsync -ruv --exclude=.svn/ --exclude=dispatch* --exclude=500.html --exclude=422.html public/ out/` end desc 'Prepare the static site and commit the changes to the svn repo.' task :commit => :prepare do # Display the messages as they come, don't buffer STDOUT.sync = true # Subversion operations print "Adding the output directory (out/) to subversion... " `svn add --force out` puts "done." print "Commiting... " `svn commit out -m "New static site content #{DateTime.now().to_s}"` puts "done." end |
The only little trick here is the use of rsync in line 7. We want to make sure that everything we have in the ./public/ directory will also be in the static copy of the site. This includes flash videos, demos, full-size screenshots, etc. Using the --exclude parameter we prevent the copy of some files that won’t be required in the production environment.
Every time we want to generate a fresh copy of the site and send it to the subversion repository we will only need to run:-
rake commit
And now we are done for good, from a Rails application in our laptop to a static site in the web server in a couple of lines!
Popularity: 9% [?]

March 25th, 2009 at 2:48 am
[...] What we finally did is use Rails as a tool to create a static site that we could .tar.gz and upload to the server. As a starting point we used a post in http://www.chuckvose.com and this is how we completed it to fit our needs. Follow up: do not miss how to integrate your rails-static site with Rake and subversion in the second article of this series: Use Rails to Create a Static Site: Rake and Subversion. [...]