Octopress S3 Sync

One lacking feature in the Octopress-distribution is that support for automatic deployment to Amazon S3 is missing. I have been looking for a solution for some days, but most of them were somewhat lacking since they didn´t support setting cache-headers (important if you use Amazon CloudFront for distribution), as well as missing support for deleting removed files from the S3-bucket/CloudFront distribution.

Jerome Bernard has an good solution, but I´ve customized it by adding support for cache-expiration headers and adding support for deleting removed files on the S3 bucket. I´ve also added configuration variables to control the behaviour in the Rakefile.

The solution relies on the s3cmd command. If you are using a Mac with the Homebrew package manager, you may install it by entering this into a terminal window:

brew install s3cmd
s3cmd --configure

With s3cmd in place, edit your Rakefile, straight after the require section;

## -- S3 Deploy config -- #
s3_bucket      = "www.yoursitename.com"  # Enter your S3 bucket name here
s3_cache_secs  = "3600" # Number of seconds to keep objects in cache - 3600 = 1 hour
s3_delete      = true # True if you want to remove deleted files in you public-directory from the S3 bucket

You must also change the deploy_default variable in your Rakefile to use S3:

deploy_default = "s3" # By default, deploy to S3

As well as the changes to the config-section above, insert the following code-block into your Rakefile;

desc "Deploy website via s3cmd"
task :s3 do
  puts "## Deploying website via s3cmd"
  ok_failed system("s3cmd sync --acl-public #{"--delete-removed" unless s3_delete == false}  --add-header "Cache-Control: max-age=#{s3_cache_secs}"  public/* s3://#{s3_bucket}/")
end

To distribute a new version of your site to S3 is very simple:

rake generate
rake deploy

One thought on “Octopress S3 Sync

Leave a Reply