Skip to content

Instantly share code, notes, and snippets.

@flomotlik
Created July 14, 2014 14:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flomotlik/b37cc1dd45967219b9b9 to your computer and use it in GitHub Desktop.
Save flomotlik/b37cc1dd45967219b9b9 to your computer and use it in GitHub Desktop.
Create new Heroku App for branch
#!/usr/bin/env ruby
require "open3"
git_branch = ENV['CI_BRANCH'] || `git rev-parse --abbrev-ref HEAD`
@app_name = "csqa-#{git_branch[0..24].gsub(/\W/, '-')}"
def exec command
Open3.popen2e(command){ |stdin, stdout_err, wait_thr|
while line = stdout_err.gets
puts line if ENV["DEPLOY_BRANCH_DEBUG"]
end
}
end
def app_name
@app_name
end
def heroku command
exec "heroku #{command} -a #{app_name}"
end
def heroku_bundle_exec rake_command
heroku "run bundle exec rake #{rake_command}"
end
def set_heroku_config_values
{
KEY: "VALUE"
}.each do |key, value|
puts "Setting config value: #{key}"
heroku "config:set #{key}=\"#{value}\""
end
end
def add_heroku_addons
['addon_name'].each do |addon|
puts "Adding addon: #{addon}"
heroku "addons:add #{addon}"
end
end
def deploy_application
puts "Removing heroku remote"
exec "git remote rm #{app_name} 2>&1"
puts "Adding remote for application: #{app_name}"
exec "git remote add #{app_name} git@heroku.com:#{app_name}.git 2>&1"
puts "Deploying application"
exec "git push #{app_name} HEAD:refs/heads/master 2>&1"
end
def run_migrations
puts "Migrating the database"
heroku_bundle_exec "db:migrate"
end
def reset_database
puts "Resetting the database"
heroku "pg:reset DATABASE --confirm #{app_name}"
end
def import_seeds
puts "Loading database seeds"
heroku_bundle_exec "db:seed"
end
def create_heroku_app
puts "Creating application: #{app_name}"
heroku "create #{app_name}"
add_heroku_addons
set_heroku_config_values
end
def heroku_apps
`heroku apps`
end
def message
ENV['CI_MESSAGE']
end
def create_app?
message['[deploy branch]'] || message['--deploy-branch']
end
create_heroku_app if create_app?
if heroku_apps[app_name]
deploy_application
run_migrations
end
import_seeds if create_app?
puts "Application URL:"
puts "https://#{app_name}.herokuapp.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment