Skip to content

Instantly share code, notes, and snippets.

@dangalipo
Created June 22, 2015 02:46
Show Gist options
  • Save dangalipo/1364c8a14ce5bd10abd3 to your computer and use it in GitHub Desktop.
Save dangalipo/1364c8a14ce5bd10abd3 to your computer and use it in GitHub Desktop.
require 'httparty'
def last_deploy_tag
tag = ""
Open3.popen3("git describe --tags `git rev-list --tags --max-count=1`") do |i, o, e, t|
tag = o.read.chomp
end
tag
end
def commits_between_last_deploy
count = 0
Open3.popen3("git rev-list #{last_deploy_tag}..origin/stable --count") do |i, o, e, t|
count = o.read.chomp
end
count.to_i
end
def notify_channel?
notifiable_stages = fetch(:notifiable_stages, ["production"])
(commits_between_last_deploy >= fetch(:channel_notify,5) &&
notifiable_stages.include?(fetch(:stage))) || fetch(:notify_channel, false)
end
namespace :deploy do
namespace :precheck do
desc 'Checks user has a slack API key in ~/.slackapikey'
task :check_slack_api_key do
unless File.exists?(File.join(ENV["HOME"], ".slack_api_key"))
puts "Could not find your Slack API key"
set(:slack_api_key, ask("your Slack API key", nil))
abort("Aborting Deploy: No API Key Supllied") if fetch(:slack_api_key, "").nil? || fetch(:slack_api_key, "").empty?
api_key_file = File.new(File.join(ENV["HOME"], ".slack_api_key"), "w+")
api_key_file.write(fetch(:slack_api_key, ""))
api_key_file.close
end
end
desc 'Verifies that the deploy is occuring during deployable hours'
task :verify_time do
time = Time.now
unless ((1..4).to_a.include?(time.wday) && time.hour < 16) ||
(time.day == 5 && time.hour < 12)
puts "Outside of normal deploy hours. Continue?"
set(:confirm, ask("Y|N", "N"))
abort("Aborting Deploy")if "Y" != fetch(:confirm, "N")
set(:notify_channel, true)
end
end
desc 'Notifies slack of deployment'
task :notify do
text = []
text << "<!channel>" if notify_channel?
token = File.read(File.join(ENV["HOME"], ".slack_api_key"))
set :deploy_msg, ask("a short description of what is being deployed", nil)
text << "deploy #{fetch(:application)} #{fetch(:stage)}"
text << "(#{fetch(:deploy_msg)})"
payload = {
body: {
token: token,
channel: "C0421D7RK",
as_user: true,
text: text.join(" ")
}
}
response = HTTParty.post("https://slack.com/api/chat.postMessage", payload)
unless response.parsed_response["ok"]
puts "Announcement Failed: Please manually notify slack. Continue?"
set(:confirm, ask("Y|N", "N"))
abort("Aborting Deploy")if "Y" != fetch(:confirm, "N")
end
end
desc 'Displays diff and requests user to confirm this is what they want to deploy'
task :verify_diff do
system("git", "diff", last_deploy_tag, "origin/stable")
puts "Continue?"
set(:confirm, ask("Y|N", "N"))
abort("Aborting Deploy")if "Y" != fetch(:confirm, "N")
end
desc "Runs all prechecks"
task :all do
invoke("deploy:precheck:check_slack_api_key")
invoke("deploy:precheck:verify_time") if fetch(:stage) == "production"
invoke("deploy:precheck:notify")
invoke("deploy:precheck:verify_diff") if fetch(:stage) == "production"
end
end
end
before "deploy:starting", 'deploy:precheck:all'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment