Skip to content
kmayer edited this page Mar 25, 2012 · 10 revisions

There are several best practices that can benefit from automation:

  • Continuous Integration (CI)
  • Staged deploys (i.e. Only deploy to staging when CI is green, only deploy to production after deploying to staging, first)
  • Labeling each release

HerokuSan and AutoTagger can help, and it's super easy.

Builds will be deployed in the following sequence:

  1. ci passes, creates ci tag from HEAD
  2. deploy latest ci tag to staging
  3. deploy latest staging tag to production

Update your CI build script

Add the following to your ci build script, this will create a tag if the build is successful. You should replace spec:all with whatever the build target is in your environment.

rake spec:all autotag:create[ci]

Update your heroku.yml configuration

Mix the following into your heroku.yml file:

...
staging:
  tag: ci/*
...
production:
  tag: staging/*
...

Mix this into your heroku.rake file

STAGES = %w[ci staging production]

def create_and_push(stage)
  auto_tag = AutoTagger::Base.new(stages: STAGES, stage: stage, verbose: true, push_refs: false, refs_to_keep: 100)
  tag = auto_tag.create_ref(auto_tag.last_ref_from_previous_stage.try(:sha))
  sh "git push origin #{tag.name}"
  auto_tag.delete_locally
  auto_tag.delete_on_remote
end

task :before_deploy do
  sh "git fetch --tags"
end

task :after_deploy do
  each_heroku_app do |stage|
    create_and_push(stage.name)
  end
end

namespace :autotag do
  desc "Create an autotag for stage, default: #{STAGES.first}"
  task :create, :stage do |t, args|
    create_and_push(args[:stage] || STAGES.first)
  end
end