Last active
January 3, 2016 18:19
-
-
Save masutaka/8500864 to your computer and use it in GitHub Desktop.
Heroku deploy tasks using ChatWork
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Require
gem
chatwork
You can add the following to Gemfile.
Setup
Add remote repositories
Put the
deploy.rake
to lib/tasksNeed 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
You can try the dry-run mode. For example..