Skip to content

Instantly share code, notes, and snippets.

@reidab
Created December 8, 2010 03:45
Show Gist options
  • Save reidab/732853 to your computer and use it in GitHub Desktop.
Save reidab/732853 to your computer and use it in GitHub Desktop.
# === In capistrano stage config (config/deploy/staging.rb, et al.)
set :deployed_url, 'https://paydici.com'
# Pivotal Tracker
set :pivotal_project_id, 39238
set :pivotal_token, 'OMG SEEKRIT TOKEN'
set :deliver_when_deploying_to, [:live,:dev]
before "deploy", "deploy:gather_information"
after "deploy", "deploy:tag_pivotal_stories", "deploy:summarize"
after "deploy:summarize", "deploy:notify", "deploy:display_summary"
namespace :deploy do
desc "Gather information about this deployment"
task :gather_information, :roles => :web do
# Who's deploying?
name = `git config --get user.name`.strip || ""
name = Etc.getlogin if name == ""
set :deployer, name
# What's the URL of the github project?
if repository[0..14] == "[email protected]:"
set :project_url, "https://github.com/#{repository[15..-5]}"
set :compare_link,
"#{project_url}/compare/#{currently_deployed[0..7]}...#{real_revision[0..7]}"
end
if defined?(HTTParty)
# What pivotal story IDs are claimed to be fixed by these commits?
fix_commits = \
`git log --grep='\\[.*fixes.*#.*]' -i --pretty='%H ::::: %s' #{current_revision[0..7]}...#{real_revision[0..7]}`.split("\n").map{|c|
(hash, message) = c.split(' ::::: ')
fixes = message.scan(/fixes \#(\d*)/i)
[hash, fixes].flatten
}
unless fix_commits.empty?
# What are the details of those pivotal stories?
set :fixed_stories,
HTTParty.get(
"https://www.pivotaltracker.com/services/v3/projects/#{pivotal_project_id}/stories",
:headers => {'X-TrackerToken' => pivotal_token},
:query => {
:filter => "id:#{fix_commits.map(&:last).join(',')} includedone:true"
}
)["stories"]
end
end
end
desc "Tag stories in pivotal tracker with the deployment stage"
task :tag_pivotal_stories, :roles => :web do
if defined?(HTTParty) && exists?(:fixed_stories) && !fixed_stories.empty?
puts "Tagging pivotal stories with deployment target [#{stage}]"
fixed_stories.each do |story|
labels = (story['labels'] || '').split(',').tap{|l| l << stage}.join(',')
if deliver_when_deploying_to.include?(stage) && story['current_state'] == 'finished'
mark_delivered = {'story[current_state]' => 'delivered'}
else
mark_delivered = {}
end
response = HTTParty.put(
"https://www.pivotaltracker.com/services/v3/projects/#{pivotal_project_id}/stories/#{story['id']}",
:headers => {'X-TrackerToken' => pivotal_token, 'Content-Length' => '0'},
:query => {
'story[labels]' => labels
}.merge(mark_delivered)
)
puts "- ##{story['id']}: #{story['name']}"
end
end
end
desc "Summarize the deployment"
task :summarize, :roles => :web do
commit_log = `git log --graph --pretty=oneline --abbrev-commit #{previous_revision[0..7]}...#{real_revision[0..7]}`
commit_log = "None!" if previous_revision == real_revision
if exists?(:fixed_stories)
fixed_story_summary = \
"\n--- Includes completed stories #{'-'*49}\n" +
fixed_stories.map{|story| "##{story['id']}: #{story['name']}"}.join("\n") +
"\n"
end
set :deployment_summary, <<-SUMMARY
#{deployer} deployed #{branch} to [#{stage}] - #{deployed_url} (now at #{real_revision[0..7]})
#{compare_link}
#{fixed_story_summary}
--- New commits deployed #{'-'*55}
#{commit_log}
SUMMARY
end
task :display_summary, :roles => :web do
puts "#{'='*80}\n\n#{deployment_summary}"
end
desc "Notify of deployment"
task :notify, :roles => :web do
unless previous_revision == real_revision
put deployment_summary, File.join(current_path,'tmp','deployment_summary.txt')
run "cd #{current_path} && RAILS_ENV=#{rails_env} rake deploy:notify"
end
end
end
namespace :deploy do
desc "Send a deployment notification if tmp/deployment_summary.txt exists"
task :notify => :environment do
summary_path = 'tmp/deployment_summary.txt'
summary = File.read(summary_path) rescue nil
if summary
message = <<-EOM
From: Deploy-O-Tron <[email protected]>
To: The League of Deployment Watchers <deploy@example>
Subject: #{summary.split("\n").first}
#{summary}
EOM
Net::SMTP.start('localhost') do |smtp|
smtp.send_message(message, "[email protected]", "deploy@example")
end
File.delete(summary_path)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment