1 Capistrano::Configuration.instance(true).load do |configuration|
2 before "deploy:update_code", "git:calculate_tag"
13 def last_tag_matching(pattern)
16 matching_tags = `git tag -l '#{pattern}'`
17 matching_tags = matching_tags.split
18 natcmp_src = File.join(File.dirname(__FILE__), '/natcmp.rb')
20 matching_tags.sort! do |a,b|
21 String.natcmp(b,a,true)
24 if matching_tags.length > 0
25 last_tag = matching_tags[0]
31 return last_tag_matching('devqa-*')
34 def last_production_tag()
35 return last_tag_matching('production-*')
38 task :calculate_tag do
41 # ignore the tag-based deployment process and a deploy non-master branch
42 if branch != "master" && stage == "devqa"
45 method = "tag_#{stage}"
48 # push tags and latest code
54 desc "Mark the current code as a devqa release"
56 # find latest devqa tag for today
57 new_tag_date = Date.today.to_s
60 last_devqa_tag = last_tag_matching("devqa-#{new_tag_date}.*")
62 # calculate largest serial and increment
63 last_devqa_tag =~ /devqa-[0-9]{4}-[0-9]{2}-[0-9]{2}\.([0-9]*)/
64 new_tag_serial = $1.to_i + 1
67 new_devqa_tag = "devqa-#{new_tag_date}.#{new_tag_serial}"
68 current_co_sha = `git log --pretty=format:%H HEAD -1`
69 last_devqa_tag_sha = nil
72 last_devqa_tag_sha = `git log --pretty=format:%H #{last_devqa_tag} -1`
75 if last_devqa_tag_sha == current_co_sha
76 puts "Not re-tagging devqa because the most recent tag (#{last_devqa_tag}) already points to current head"
77 new_devqa_tag = last_devqa_tag
79 puts "Tagging current branch for deployment to devqa as '#{new_devqa_tag}'"
80 sh "git tag -a -m 'tagging current code for deployment to devqa' #{new_devqa_tag}"
83 set :branch, new_devqa_tag
86 desc "Push the passed devqa tag to production. Pass in tag to deploy with '-s tag=devqa-YYYY-MM-DD.X'."
87 task :tag_production do
88 production_tag = configuration[:tag]
89 if !production_tag && production_tag !~ /devqa-.*/
90 puts "devqa tag required; use '-s tag=devqa-YYYY-MM-DD.X'"
94 raise "devqa tag #{production_tag} does not exist." unless last_tag_matching(production_tag)
96 production_tag =~ /devqa-([0-9]{4}-[0-9]{2}-[0-9]{2}\.[0-9]*)/
97 new_production_tag = "production-#{$1}"
98 puts "promoting devqa tag #{production_tag} to production as '#{new_production_tag}'"
99 sh "git tag -a -m 'tagging current code for deployment to production' #{new_production_tag} #{production_tag}"
101 set :branch, new_production_tag