Created
December 31, 2013 17:08
-
-
Save copyhacker/8199667 to your computer and use it in GitHub Desktop.
Notify Sprintly of deployed items
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
# Requires the following environment vars: | |
# - SPRINTLY_PRODUCT_ID | |
# - SPRINTLY_EMAIL | |
# - SPRINTLY_API_KEY | |
namespace :deploy do | |
task :after_deploy, :env do |t, args| | |
puts "Running :after_deploy" | |
begin | |
sh "git fetch --tags" | |
prev_tag = get_previous_deploy_tag args[:env] | |
new_tag = get_new_deploy_tag args[:env] | |
sh "git tag #{new_tag}" | |
sh "git push --tags" | |
task('sprintly:notify').invoke args[:env], prev_tag, new_tag, ENV['SPRINTLY_PRODUCT_ID'] | |
rescue => e | |
puts "Error while tagging for deploy to #{args[:env].upcase}: #{e}" | |
end | |
end | |
namespace :sprintly do | |
task :notify, :env, :prev_tag, :new_tag, :product do |t, args| | |
sprintly_email = ENV['SPRINTLY_EMAIL'] || "" | |
sprintly_api_key = ENV['SPRINTLY_API_KEY'] || "" | |
if sprintly_email.empty? || sprintly_api_key.empty? | |
puts "** Unable to notify Sprintly of this deploy." | |
puts "** Please set your SPRINTLY_EMAIL and SPRINTLY_API_KEY environment variables." | |
else | |
items = `git log --oneline #{args[:prev_tag]}..#{args[:new_tag]}` | |
.scan(/#(\d*)/).flatten.uniq.delete_if { |n| n.to_i.zero? }.join(',') | |
`curl -X POST -u #{sprintly_email}:#{sprintly_api_key} -d 'environment=#{args[:env]}&numbers=#{items}' https://sprint.ly/api/products/#{args[:env]}/deploys.json` | |
end | |
end | |
end | |
def get_new_deploy_tag(env) | |
tag_base = "deploy-#{env}-#{Date.today}" | |
tag = tag_base | |
n = 0 | |
while has_tag? tag | |
n += 1 | |
tag = "#{tag_base}-#{n}" | |
end | |
tag | |
end | |
def get_previous_deploy_tag(env) | |
`git tag -l`.split.reverse.find { |tag| tag =~ /^deploy-#{env}/ } | |
end | |
def has_tag? tag | |
(`git tag -l #{tag}` =~ /#{tag}/) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment