Skip to content

Instantly share code, notes, and snippets.

@masutaka
Last active January 3, 2016 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masutaka/8500864 to your computer and use it in GitHub Desktop.
Save masutaka/8500864 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment