Multi-environment post_build_hook using tddium, Heroku, and Ruby Sinatra

We have been using tddium as a deployment tool for our Ruby Sinatra API for some time now. It has been working great and now manages deployment for 7 of our API environments. We have ran into a few small issues, but there are some sharp engineers on support, so issues get fixed fast. The most recent develop in our system is to implement a post build hook to run migrations after the app is deployed on Heroku.

One of the straight-forward adjustments I had to make to their gist is to dynamically set the app name based on the branch that was pushed to Github. In our case, each Heroku app (“environment”) has a git branch, and this is also how tddium sets up test suites, so everything jives.

The portion that required some support was apparently due to some dependency issues with my gems and the way the git repo was being pushed from tddium. The first step was to add the heroku gem to my gemfile, and from there, it was to modify the tddium post_build_hook a little bit. Here is the full version:

namespace :tddium do
  desc "post_build_hook"
  task :post_build_hook do
    return unless ENV["TDDIUM_MODE"] == "ci"
    return unless ENV["TDDIUM_BUILD_STATUS"] == "passed"

    dir = File.expand_path("~/.heroku/")
    heroku_email = ENV["HEROKU_EMAIL"]
    heroku_api_key = ENV["HEROKU_API_KEY"]
    current_branch = `git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3-`.strip
    abort "invalid current branch" unless current_branch
    puts "Current Branch: #{current_branch}"

    case current_branch
      when 'integration'
        app_name = 'matts-api-integration'
      when 'staging'
        app_name = 'matts-api-staging'
      when 'production'
        app_name = 'matts-api'
      when 'demo'
        app_name = 'matts-api-demo'
      when 'demo-staging'
        app_name = 'matts-api-demo-staging'
      when 'pilot'
        app_name = 'matts-api-pilot'
      when 'pilot-staging'
        app_name = 'matts-api-pilot-staging'

    return unless defined? app_name

    puts "App Name: #{app_name}"
    push_sha = `git rev-parse HEAD`
    push_target = "{app_name}.git"

    abort "invalid current branch" unless current_branch

    FileUtils.mkdir_p(dir) or abort "Could not create #{dir}"

    puts "Writing Heroku Credentials", "credentials"), "w") do |f|
      f.write([heroku_email, heroku_api_key].join("\n"))
    end"~/.netrc"), "a+") do |f|
      ['api', 'code'].each do |host|
        f.puts "machine #{host}"
        f.puts "  login #{heroku_email}"
        f.puts "  password #{heroku_api_key}"
    puts "Pushing to Heroku: #{push_target}..."
    cmd "git push #{push_target} HEAD:master --force" or abort "could not push to #{push_target}"

    puts "Running Heroku Migrations..."
    cmd "heroku run rake db:migrate --app #{app_name}" or abort "aborted migrations"

    puts "Restarting Heroku..."
    cmd "bundle exec heroku restart --app #{app_name}" or abort "aborted heroku restart" 

    puts "Post Build Complete!"