Skip to content

Instantly share code, notes, and snippets.

@dpfranklin
Created April 26, 2013 22:05
Show Gist options
  • Save dpfranklin/5470776 to your computer and use it in GitHub Desktop.
Save dpfranklin/5470776 to your computer and use it in GitHub Desktop.
Thor task for deploying Middleman site to S3.
class Deploy < Thor
default_task :s3
desc "cleanS3test", "delete current
deploy"
def cleanS3test
puts "Deleting current deploy..."
system "s3cmd -r -f del s3://www.example.com/"
puts "Done"
end
desc "s3", "new build and deploy to test environment on s3"
def s3
oneyear = 31557600
onemonth = 2592000
oneweek = 604800
oneday = 86400
onehour = 3600
oneyearstr = (Time.now + 31557600).strftime("%a, %d %b %Y %H:%M:%S %Z")
onemonthstr = (Time.now + 2592000).strftime("%a, %d %b %Y %H:%M:%S %Z")
oneweekstr = (Time.now + 604800).strftime("%a, %d %b %Y %H:%M:%S %Z")
onedaystr = (Time.now + 86400).strftime("%a, %d %b %Y %H:%M:%S %Z")
onehourstr = (Time.now + 3600).strftime("%a, %d %b %Y %H:%M:%S %Z")
puts "Deploying to s3 environment"
puts "Building local"
system "bundle exec middleman build --clean"
puts "\n\n\n Swapping gzipped files for uncompressed versions"
FileUtils.cd('build/', :verbose => true) do
Dir["**/*.css", "**/*.js", "**/*.html", "**/*.json"].each do |source|
dirname = File.dirname(source)
if dirname == "."
originaldir = "/"
else
originaldir = "/#{dirname}/"
end
originalfile = File.basename(source)
system "mv .#{originaldir}#{originalfile} .#{originaldir}#{originalfile}.orig"
end
end
FileUtils.cd('build/', :verbose => true) do
Dir["**/*.gz"].each do |source|
dirname = File.dirname(source)
if dirname == "."
originaldir = "/"
else
originaldir = "/#{dirname}/"
end
originalfile = File.basename(source)
newfile = File.basename(source, '.gz')
system "mv .#{originaldir}#{originalfile} .#{originaldir}#{newfile}"
end
end
puts "\n\n\n Done with prep - syncing..."
puts "\n\n\n Syncing images and fonts"
system "s3cmd sync --acl-public --no-preserve --exclude \".DS_Store\" --exclude \"*.*\" --add-header=\"Cache-Control:public, max-age=#{oneyear}\" --include \"*.png\" --include \"*.jpg\" --include \"*.gif\" --include \"*.ico\" --include \"*.eot\" --include \"*.svg\" --include \"*.ttf\" --include \"*.woff\" --include \"*.otf\" build/ s3://www.example.com/"
puts "\n\n\n Syncing js"
system "s3cmd sync --acl-public --no-preserve --exclude \".DS_Store\" --exclude \"*.*\" --add-header=\"Cache-Control:public, max-age=#{oneweek}\" --add-header=\"Content-Encoding:gzip\" --mime-type=\"application/javascript\" --include \"*.js\" build/ s3://www.example.com/"
puts "\n\n\n Syncing css"
system "s3cmd sync --acl-public --no-preserve --exclude \".DS_Store\" --exclude \"*.*\" --add-header=\"Cache-Control:public, max-age=#{oneweek}\" --add-header=\"Content-Encoding:gzip\" --mime-type=\"text/css\" --include \"*.css\" build/ s3://www.example.com/"
puts "\n\n\n Syncing html"
system "s3cmd sync --acl-public --no-preserve --exclude \".DS_Store\" --exclude \"*.*\" --add-header=\"Cache-Control:public, max-age=#{oneweek}, must-revalidate\" --add-header=\"Content-Encoding:gzip\" --mime-type=\"text/html; charset=utf-8\" --include \"*.html\" build/ s3://www.example.com/"
puts "\n\n\n Syncing json"
system "s3cmd sync --acl-public --no-preserve --exclude \".DS_Store\" --exclude \"*.*\" --add-header=\"Cache-Control:public, max-age=#{oneday}, must-revalidate\" --add-header=\"Content-Encoding:gzip\" --mime-type=\"application/json\" --include \"*.json\" build/ s3://www.example.com/"
puts "\n\n\n Syncing xml"
system "s3cmd sync --acl-public --no-preserve --exclude \".DS_Store\" --exclude \"*.*\" --add-header=\"Cache-Control:public, max-age=#{onemonth}\" --add-header=\"Content-Encoding:gzip\" --mime-type=\"application/xml\" --include \"*.xml\" build/ s3://www.example.com/"
puts "\n\n\n Syncing vendor"
system "s3cmd sync --acl-public --no-preserve --exclude \".DS_Store\" --exclude \"*.*\" --add-header=\"Cache-Control:public, max-age=#{oneyear}\" --add-header=\"Content-Encoding:gzip\" --mime-type=\"application/javascript\" --include \"javascripts/vendor/*.js\" build/ s3://www.example.com/"
puts "\n\n\n Overwriting index.html with short expires time"
system "s3cmd --acl-public --no-preserve --add-header=\"Cache-Control:public, max-age=#{oneday}, must-revalidate\" --add-header=\"Expires:#{onedaystr}\" --add-header=\"Content-Encoding:gzip\" --mime-type=\"text/html; charset=utf-8\" put build/index.html s3://www.example.com/"
puts "\n\n\n Syncing remaining and cleaning up"
system "s3cmd sync --acl-public --no-preserve --delete-removed --exclude \".DS_Store\" --exclude \"*.orig\" build/ s3://www.example.com/"
puts "\n\n\n Done deploying to s3 environment - opening in browser"
system "open http://www.example.com.s3-website-us-east-1.amazonaws.com/"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment