Deploy with Heroku pipeline

danejContinuous DeliveryLeave a Comment

Heroku released a new feature called pipelines. This allows you to easily transfer a compiled slug from one application to another. This is useful when your continuous delivery pipeline looks like development -> staging -> production.

When you promote a slug, it just does that, promote it. If you have database migrations, need a restart, or need other changes, this doesn’t work out very well. Here is a rake task which will turn on maintenance mode, promote the slug, migrate, restart and turn off maintenance mode.

development_app = 'development-application'
staging_app     = 'staging-application'
production_app  = 'application'

namespace :deploy do
  namespace :development do
    task :promote do
      start_time = Time.now

      Bundler.with_clean_env do
        puts "Maintenance On"
        puts `heroku maintenance:on -a #{staging_app}`

        puts "Promoting Development to Staging"
        puts `heroku pipeline:promote -a #{development_app}`

        puts "Migrate Staging Database"
        puts `heroku run rake db:migrate -a #{staging_app}`

        puts "Restart Staging"
        puts `heroku restart -a #{staging_app}`

        puts "Maintenance Off"
        puts `heroku maintenance:off -a #{staging_app}`
      end

      puts "[#{Time.now - start_time}] Total Time"
    end
  end
end

You can run the task with

$ bundle exec rake deploy:development:promote
Maintenance On
Enabling maintenance mode for staging-application... done
Promoting Development to Staging
Promoting development-application to staging-application....done, v239
Migrate Staging Database
Running `rake db:migrate` attached to terminal... up, run.5333
Restart Staging
Restarting dynos... done
Maintenance Off
Disabling maintenance mode for staging-application... done
[28.340191849] Total Time

By having a development slug, you can push to Heroku, compile assets, and compile the slug “offline.” This took our deployment window from about 6 mins to consistently under 30 seconds.

Leave a Reply