Skip to content

Instantly share code, notes, and snippets.

@allspiritseve
Created August 19, 2012 20:39
Show Gist options
  • Save allspiritseve/3397593 to your computer and use it in GitHub Desktop.
Save allspiritseve/3397593 to your computer and use it in GitHub Desktop.
Heroku deploy script
# lib/tasks/deploy.rake
namespace :deploy do
desc 'Deploy to production'
task(:production) { deploy }
desc 'Deploy to staging'
task(:staging) { deploy :staging }
private
def deploy(environment = :production)
announce "Deploying to #{red environment}..."
force_push = ask "Do you want to force push? (y/n) "
announce "Deploying branch #{red current_git_branch}"
if force_push == 'y'
sh "git push -f #{remote_git_url environment} #{current_git_branch}:master"
elsif force_push == 'n'
sh "git push #{remote_git_url environment} #{current_git_branch}:master"
else
announce 'No touching!'
exit
end
sh "heroku run rake db:migrate --app #{heroku_app environment}"
announce 'Ladies and gentlemen, we have successfully deployed to Heroku!'
end
def ask(message)
print message
STDIN.gets.chomp
end
def announce(message)
puts "\n#{message}\n\n"
end
def red(str)
"\e[31m#{str}\e[0m"
end
def current_git_branch
`git symbolic-ref HEAD 2> /dev/null`.strip.gsub(/^refs\/heads\//, '')
end
def remote_git_url(environment = :production)
case environment
when :production
'[email protected]:production.git'
when :staging
'[email protected]:staging.git'
else
announce "Error: unknown remote environment '#{environment}'. Edit method remote_git_url in lib/tasks/deploy.rake"
exit
end
end
def heroku_app(environment = :production)
remote_git_url(environment).match(/[email protected]:([^\.]+)\.git/)[1]
end
end
task :deploy => 'deploy:staging'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment