Created
January 15, 2013 20:26
-
-
Save brcosm/4541735 to your computer and use it in GitHub Desktop.
The Rakefile I use to deploy my Jekyll site.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'jcs3' | |
def content_type_for_extension(extension) | |
return "text/html" if %w[html htm].include?(extension) | |
return "text/css" if %w[css].include?(extension) | |
return "application/javascript" if %w[js].include?(extension) | |
return "image/gif" if %w[gif].include?(extension) | |
return "image/jpeg" if %w[jpeg jpg].include?(extension) | |
return "image/png" if %w[png].include?(extension) | |
return "image/tiff" if %w[tiff tf].include?(extension) | |
return "image/svg+xml" if %w[svg svgz].include?(extension) | |
"text/plain" | |
end | |
def upload_files(deployer, root_dir) | |
files = Dir.glob("#{$root_dir}/**/*.*") | |
files.each do |f| | |
key = (f.split("#{root_dir}/")[1]) | |
next if /(.css|.js|.html)\z/ =~ key | |
options = { | |
:cache_control => "max_age=86400" | |
} | |
if (/.gz\z/ =~ key) | |
key.sub!(".gz", "") | |
options[:content_encoding] = 'gzip' | |
end | |
options[:content_type] = content_type_for_extension(key.split('.').last) | |
puts "Uploading[#{options[:content_type]}] #{key}" | |
deployer.push(key, f, options) | |
end | |
end | |
desc "Generates the static site files with Jekyll and deploys them to Cloudfront via S3" | |
task :deploy => [:setup, :generate, :compress, :push, :cleanup] | |
desc "Sets up the deployment configuration" | |
task :setup do | |
$config = YAML::load(File.open("config.yml")) | |
$root_dir = "src/_site" | |
puts "=============================================" | |
puts " Deploying to #{$config['bucket']}" | |
puts "=============================================" | |
end | |
desc "Generates the static site files using Jekyll" | |
task :generate do | |
system("jekyll ./src ./src/_site") | |
end | |
desc "Compresses all .css, .js, and .html files" | |
task :compress do | |
compressor = Jcs3::Compressor.new($root_dir) | |
compressor.zip_files | |
end | |
desc "Does the deployment" | |
task :push do | |
$config ||= YAML::load_file("config.yml") | |
$root_dir ||= "src/_site" | |
deployer = Jcs3::Deployer.new($config) | |
upload_files(deployer, $root_dir) | |
deployer.invalidate_dirty_keys | |
end | |
desc "Removes the .gz files" | |
task :cleanup do | |
Dir.glob("#{$root_dir}/**/*.gz").each { |f| puts "Deleting #{f}"; system("rm #{f}") } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment