Last active
January 16, 2016 21:01
-
-
Save nileshtrivedi/cae823d265cc5773c864 to your computer and use it in GitHub Desktop.
Deploy a Play framework app with SSHKit
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 'sshkit' | |
require 'sshkit/dsl' | |
# Once "bundle install" has been run, this script can be run as "ruby deploy.rb staging" or "ruby deploy.rb prod" | |
if ARGV.first == "prod" | |
servers = ['deploy@prod1.example.com', 'deploy@prod2.example.com'] | |
elsif ARGV.first == "staging" | |
servers = ['deploy@staging.example.com'] | |
else | |
puts "Unknown env. Exiting." | |
exit | |
end | |
on [:local] do |host| #TODO: Make this step optional? | |
puts "Building on local..." | |
puts "Cleaning..." | |
execute("activator clean") | |
puts "Building dist..." | |
execute("activator dist") | |
puts "Done" | |
end | |
on servers, in: :sequence, wait: 1 do |host| | |
puts "\n\nNow running on #{host}..." | |
deploy_dir = "/home/deploy/" + Time.now.strftime('%Y%m%d%H%M') | |
puts "Making #{deploy_dir} directory..." | |
puts execute(:mkdir, '-p', deploy_dir) | |
puts "Uploading zip..." | |
upload! 'app.zip', deploy_dir | |
hh = host.split("@").last.split(".").first | |
within deploy_dir do | |
execute("unzip app.zip") | |
execute("cp application.conf.#{hh} application.conf") | |
execute("killall java") #stop existing process | |
execute("nohup bin/app &") #start the server | |
end | |
puts "Done on #{host}" | |
end | |
on [:local] do |host| | |
puts "\n\nRunning tests..." | |
# Run some tests to check whether deploy went okay | |
servers.each do |host| | |
domain = host.split("@").last | |
if test "curl http://#{domain}/" | |
puts "#{domain} seems to be up" | |
else | |
puts "#{domain} seems to be down !!!" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment