Skip to content

Instantly share code, notes, and snippets.

@masutaka masutaka/deploy.rake
Last active Jan 3, 2016

Embed
What would you like to do?
Heroku deploy tasks using ChatWork
namespace :deploy do
nowrite ENV['DRYRUN']
desc 'Deploy to production server'
task :prod do
before_last_release = last_release :prod
sh 'git push prod master'
after_last_release = last_release :prod
unless before_last_release == after_last_release
notify_to_chatwork :prod, before_last_release, after_last_release
end
end
desc 'Deploy to staging server'
task :staging do
before_last_release = last_release :staging
branch = ENV['BRANCH']
if branch
sh "git push staging #{branch}"
sh "git push -f staging #{branch}:master"
else
sh 'git push staging master'
end
after_last_release = last_release :staging
unless before_last_release == after_last_release
notify_to_chatwork :staging, before_last_release, after_last_release
end
end
def last_release stage
return { deploy_version: nil, commit_hash: nil } if nowrite
deploy_version = nil
commit_hash = nil
`heroku releases --app=#{heroku_app_name stage}`.each_line do |line|
if line =~ /^(v\d+) +Deploy ([a-f\d]+) /
deploy_version = $1
commit_hash = $2
break
end
end
{ deploy_version: deploy_version, commit_hash: commit_hash }
end
def heroku_app_name stage
stage == :prod ? '<Heroku prod app name>' : '<Heroku staging app name>'
end
def notify_to_chatwork stage, before_last_release, after_last_release
room_id = stage == :prod ? '<Production room_id>' : '<Staging room_id>'
user_name = `git config user.name`.chomp
after_deploy_version = after_last_release[:deploy_version]
url = "<Your GitHub URL>/compare/#{before_last_release[:commit_hash]}...#{after_last_release[:commit_hash]}"
ChatWork.api_key = '<Your API Key>'
ChatWork::Message.create room_id: room_id, body: "#{user_name} deployed #{after_deploy_version} #{url}"
end
end
@masutaka

This comment has been minimized.

Copy link
Owner Author

masutaka commented Jan 19, 2014

Require

gem chatwork

You can add the following to Gemfile.

group :development do
  gem 'chatwork'
end

Setup

Add remote repositories

$ git remote add prod git@heroku.com:<Your Heroku App Name of Staging>.git
$ git remote add staging git@heroku.com:<Your Heroku App Name of Production>.git

Put the deploy.rake to lib/tasks

Need to modify the follwoing

  • <Heroku prod app name>
  • <Heroku staging app name>
  • <Production room_id>
  • <Staging room_id>
  • <Your GitHub URL>
  • <Your API Key>

How to deploy

# to Staging
$ bundle exec rake deploy:staging

# to Staging any branch
$ bundle exec rake deploy:staging BRANCH=<any-branch>

# to Production
$ bundle exec rake deploy:prod

You can try the dry-run mode. For example..

$ bundle exec rake deploy:prod DRYRUN=1
@masutaka

This comment has been minimized.

Copy link
Owner Author

masutaka commented Feb 12, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.