-
-
Save robertjwhitney/1472595 to your computer and use it in GitHub Desktop.
Simple Rake task for customizing deployment to Heroku
This file contains hidden or 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
# rake deploy:production master | |
# List of environments and their heroku git remotes | |
ENVIRONMENTS = { | |
:staging => 'myapp-staging', | |
:production => 'myapp-production' | |
} | |
namespace :deploy do | |
ENVIRONMENTS.keys.each do |env| | |
desc "Deploy to #{env}" | |
task env do | |
current_branch = `git branch | grep ^* | awk '{ print $2 }'`.strip | |
Rake::Task['deploy:before_deploy'].invoke(env, current_branch) | |
Rake::Task['deploy:update_code'].invoke(env, current_branch) | |
Rake::Task['deploy:after_deploy'].invoke(env, current_branch) | |
end | |
end | |
task :before_deploy, :env, :branch do |t, args| | |
puts "Deploying #{args[:branch]} to #{args[:env]}" | |
# Ensure the user wants to deploy a non-master branch to production | |
if args[:env] == :production && args[:branch] != 'master' | |
print "Are you sure you want to deploy '#{args[:branch]}' to production? (y/n) " and STDOUT.flush | |
char = $stdin.getc | |
if char != ?y && char != ?Y | |
puts "Deploy aborted" | |
exit | |
end | |
end | |
end | |
task :after_deploy, :env, :branch do |t, args| | |
# add ENV config to heroku from local yml file | |
config_vars = [] | |
env = args[:env] | |
config = YAML.load_file("#{File.dirname(__FILE__)}/../config/heroku_config.yml")[env.to_s] rescue nil || {} | |
config.each do |key, value| | |
config_vars << "#{key.upcase}=#{value}" | |
end | |
puts "Adding #{config_vars.join(" ")} to ENV config at #{ENVIRONMENTS[args[:env]]}" | |
system "heroku config:add #{config_vars.join(" ")} --app #{ENVIRONMENTS[args[:env]]}" | |
puts "Deployment Complete" | |
end | |
task :update_code, :env, :branch do |t, args| | |
FileUtils.cd "#{File.dirname(__FILE__)}/../" do | |
puts "Updating #{ENVIRONMENTS[args[:env]]} with branch #{args[:branch]}" | |
system "git push heroku #{args[:branch]}:master" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment