Thom Nichols


Technology is evolution outside the gene pool

Deploying Octopress on Amazon S3

So a number of folks have described how to use Octopress with S3.  However most of them don't describe how to easily publish to S3, or require an external command-line tool to upload the generate files.  I solved this by using the aws/s3 gem and creating a new rake task. 

rake deploy_s3[blog]  # deploys all blog files

I also added a task to ping Google and Bing when the sitemap updates:

rake ping # tells Google & Bing to crawl updated sitemaps

You can see the full code here.

# Add this w/ your other imports:
 require 'aws/s3'
 require 'yaml'
 puts "No S3 upload support!"

# and in your config section...
s3_config_file = "amazon_s3.yml" # location of file with S3 access key, private key, bucket name

# Use deploy_s3[.*html] to upload only HTML files since the timestamp matching 
# doesn't really work well. Most files get re-generated even if they haven't 
# changed.
desc "Deploy blog to Amazon S3"
task :deploy_s3, :file_match do |t, args|
 # follow these instructions to setup your bucket: 
 # Note that `s3_config_file` should be set at the top of yer rakefile
 args.with_defaults :file_match => '.*'

  config = YAML.load s3_config_file )
   :access_key_id => config['access_key'],
   :secret_access_key => config['secret_key']
  puts "Please verify your AWS credentials in '#{s3_config_file}'"
 bucket_name = config['bucket_name']
 bucket = AWS::S3::Bucket.find bucket_name

 Dir.glob("#{public_dir}/**/*") do |f|

  next if f
  f_name = f[public_dir.length+1..-1]
  next unless f_name.match( args.file_match ) != nil

  # compare atime for each file, only sync changes
  local_modified = File.stat( f ).mtime ).to_datetime
  existing = bucket[f_name]

  unless existing and local_modified < DateTime.parse( existing.about['last-modified'] )
   puts "Pushing #{f_name} to #{bucket_name}..." f_name,,'r'), bucket_name

desc 'Notify Google & Bing of the new sitemap'
task :ping do
 config = YAML.load config_yaml
 sitemap_url = URI.escape "#{config['url']}#{config['root']}sitemap.xml"
 puts "* Pinging Google sitemap... #{sitemap_url}"
 resp = Net::HTTP.get_response '', "/webmasters/tools/ping?sitemap=#{sitemap_url}"
 puts "Error! #{resp.code} : #{resp.message}" if resp.code.to_i != 200
 puts '* Pinging Bing sitemap...'
 resp = Net::HTTP.get_response '',  "/webmaster/ping.aspx?siteMap=#{sitemap_url}"
 puts "Error! #{resp.code} : #{resp.message}" if resp.code.to_i != 200
 puts '... Done.'

Category: AWS Ruby