diff --git a/gitflow.rb b/gitflow.rb index 0b4af2c..cca128a 100644 --- a/gitflow.rb +++ b/gitflow.rb @@ -11,7 +11,7 @@ def last_tag_matching(pattern) allTagsMatching.sort! do |a,b| String.natcmp(b,a,true) end - + if allTagsMatching.length > 0 lastTag = allTagsMatching[0] end @@ -22,6 +22,14 @@ def last_staging_tag() return last_tag_matching('staging-*') end + def last_demo_tag() + return last_tag_matching('demo-*') + end + + def last_uat_tag() + return last_tag_matching('uat-*') + end + def last_production_tag() return last_tag_matching('production-*') end @@ -51,10 +59,14 @@ def last_production_tag() toTag = nil # do different things based on stage - if stage == :production - fromTag = last_production_tag - elsif stage == :staging - fromTag = last_staging_tag + if (stage == :production or stage = :production_vagrant) + fromTag = last_tag_matching("#{stage}-*") + elsif stage == :demo + fromTag = last_demo_tag + elsif stage == :uat + fromTag = last_uat_tag + elsif (stage == :staging or stage = :staging_vagrant) + fromTag = last_tag_matching("#{stage}-*") else raise "Unsupported stage #{stage}" end @@ -64,9 +76,13 @@ def last_production_tag() if toTag == nil puts "Calculating 'end' tag for :update_log for '#{stage}'" # do different things based on stage - if stage == :production + if (stage == :production or stage = :production_vagrant) toTag = last_staging_tag - elsif stage == :staging + elsif stage == :demo + toTag = last_staging_tag + elsif stage == :uat + toTag = last_staging_tag + elsif (stage == :staging or stage = :staging_vagrant) toTag = 'head' else raise "Unsupported stage #{stage}" @@ -86,7 +102,7 @@ def last_production_tag() desc "Mark the current code as a staging/qa release" task :tag_staging do # find latest staging tag for today - newTagDate = Date.today.to_s + newTagDate = Date.today.to_s newTagSerial = 1 lastStagingTag = last_tag_matching("staging-#{newTagDate}.*") @@ -114,13 +130,104 @@ def last_production_tag() set :branch, newStagingTag end + desc "Push the passed staging tag to demo_vagrant. Pass in tag to deploy with '-s tag=staging-YYYY-MM-DD.X'." + task :tag_demo_vagrant do + promoteToDemoTag = configuration[:tag] + raise "Staging tag required; use '-s tag=staging-YYYY-MM-DD.X'" unless promoteToDemoTag + raise "Staging tag required; use '-s tag=staging-YYYY-MM-DD.X'" unless promoteToDemoTag =~ /staging-.*/ + raise "Staging Tag #{promoteToDemoTag} does not exist." unless last_tag_matching(promoteToDemoTag) + + promoteToDemoTag =~ /staging-([0-9]{4}-[0-9]{2}-[0-9]{2}\.[0-9]*)/ + newDemoTag = "demo_vagrant-#{$1}" + puts "promoting staging tag #{promoteToDemoTag} to demo_vagrant as '#{newDemoTag}'" + system "git tag -a -m 'tagging current code for deployment to demo_vagrant' #{newDemoTag} #{promoteToDemoTag}" + + set :branch, newDemoTag + end + + desc "Push the passed staging tag to demo. Pass in tag to deploy with '-s tag=staging-YYYY-MM-DD.X'." + task :tag_demo do + promoteToDemoTag = configuration[:tag] + raise "Staging tag required; use '-s tag=staging-YYYY-MM-DD.X'" unless promoteToDemoTag + raise "Staging tag required; use '-s tag=staging-YYYY-MM-DD.X'" unless promoteToDemoTag =~ /staging-.*/ + raise "Staging Tag #{promoteToDemoTag} does not exist." unless last_tag_matching(promoteToDemoTag) + + promoteToDemoTag =~ /staging-([0-9]{4}-[0-9]{2}-[0-9]{2}\.[0-9]*)/ + newDemoTag = "demo-#{$1}" + puts "promoting staging tag #{promoteToDemoTag} to demo as '#{newDemoTag}'" + system "git tag -a -m 'tagging current code for deployment to demo' #{newDemoTag} #{promoteToDemoTag}" + + set :branch, newDemoTag + end + + def generate_uat_tag() + # find latest uat tag for today + newTagDate = Date.today.to_s + newTagSerial = 1 + + lastUatTag = last_tag_matching("uat-#{newTagDate}.*") + if lastUatTag + # calculate largest serial and increment + lastUatTag =~ /uat-[0-9]{4}-[0-9]{2}-[0-9]{2}\.([0-9]*)/ + newTagSerial = $1.to_i + 1 + end + newUatTag = "uat-#{newTagDate}.#{newTagSerial}" + return newUatTag + end + + def staging_to_uat(promoteToUatTag) + raise "Staging tag required; use '-s tag=staging-YYYY-MM-DD.X'" unless promoteToUatTag =~ /staging-.*/ + raise "Staging Tag #{promoteToUatTag} does not exist." unless last_tag_matching(promoteToUatTag) + + promoteToUatTag =~ /staging-([0-9]{4}-[0-9]{2}-[0-9]{2}\.[0-9]*)/ + newUatTag = "uat-#{$1}" + puts "promoting staging tag #{promoteToUatTag} to uat as '#{newUatTag}'" + system "git tag -a -m 'tagging current code for deployment to uat' #{newUatTag} #{promoteToUatTag}" + return newUatTag + end + + desc "Mark the current code as a uat release" + task :tag_uat do + promoteToUatTag = configuration[:tag] + if promoteToUatTag + #if the user inputs -s tag=staging.... + raise "Staging tag required; use '-s tag=staging-YYYY-MM-DD.X'" unless promoteToUatTag =~ /staging-.*/ + raise "Staging Tag #{promoteToUatTag} does not exist." unless last_tag_matching(promoteToUatTag) + promoteToUatTag =~ /staging-([0-9]{4}-[0-9]{2}-[0-9]{2}\.[0-9]*)/ + end + + lastUatTag = last_tag_matching("uat-#{Date.today.to_s}.*") + + newUatTag = generate_uat_tag + shaOfCurrentCheckout = `git log --pretty=format:%H HEAD -1` + shaOfLastUatTag = nil + + if lastUatTag + shaOfLastUatTag = `git log --pretty=format:%H #{lastUatTag} -1` + end + + if shaOfLastUatTag == shaOfCurrentCheckout + puts "Not re-tagging uat because the most recent tag (#{lastUatTag}) already points to current head" + newUatTag = lastUatTag + elsif promoteToUatTag + # if there was a staging tag... + puts "Tagging current branch for deployment to uat as '#{newUatTag}'" + system "git tag -a -m 'tagging current code for deployment to uat' #{newUatTag} #{promoteToUatTag}" + else + # there was no staging tag --> we generated a new uat tag + puts "Tagging current branch for deployment to uat as '#{newUatTag}'" + system "git tag -a -m 'tagging current code for deployment to uat' #{newUatTag}" + end + set :branch, newUatTag + end + desc "Push the passed staging tag to production. Pass in tag to deploy with '-s tag=staging-YYYY-MM-DD.X'." task :tag_production do promoteToProductionTag = configuration[:tag] raise "Staging tag required; use '-s tag=staging-YYYY-MM-DD.X'" unless promoteToProductionTag raise "Staging tag required; use '-s tag=staging-YYYY-MM-DD.X'" unless promoteToProductionTag =~ /staging-.*/ raise "Staging Tag #{promoteToProductionTag} does not exist." unless last_tag_matching(promoteToProductionTag) - + promoteToProductionTag =~ /staging-([0-9]{4}-[0-9]{2}-[0-9]{2}\.[0-9]*)/ newProductionTag = "production-#{$1}" puts "promoting staging tag #{promoteToProductionTag} to production as '#{newProductionTag}'" @@ -128,5 +235,51 @@ def last_production_tag() set :branch, newProductionTag end + + desc "Mark the current code as a staging/qa release" + task :tag_staging_vagrant do + # find latest staging tag for today + newTagDate = Date.today.to_s + newTagSerial = 1 + + lastStagingTag = last_tag_matching("staging_vagrant-#{newTagDate}.*") + if lastStagingTag + # calculate largest serial and increment + lastStagingTag =~ /staging_vagrant-[0-9]{4}-[0-9]{2}-[0-9]{2}\.([0-9]*)/ + newTagSerial = $1.to_i + 1 + end + newStagingTag = "staging_vagrant-#{newTagDate}.#{newTagSerial}" + + shaOfCurrentCheckout = `git log --pretty=format:%H HEAD -1` + shaOfLastStagingTag = nil + if lastStagingTag + shaOfLastStagingTag = `git log --pretty=format:%H #{lastStagingTag} -1` + end + + if shaOfLastStagingTag == shaOfCurrentCheckout + puts "Not re-tagging staging_vagrant because the most recent tag (#{lastStagingTag}) already points to current head" + newStagingTag = lastStagingTag + else + puts "Tagging current branch for deployment to staging_vagrant as '#{newStagingTag}'" + system "git tag -a -m 'tagging current code for deployment to staging_vagrant' #{newStagingTag}" + end + + set :branch, newStagingTag + end + + desc "Push the passed staging tag to production_vagrant. Pass in tag to deploy with '-s tag=staging-YYYY-MM-DD.X'." + task :tag_production_vagrant do + promoteToProductionTag = configuration[:tag] + raise "Staging tag required; use '-s tag=staging-YYYY-MM-DD.X'" unless promoteToProductionTag + raise "Staging tag required; use '-s tag=staging-YYYY-MM-DD.X'" unless promoteToProductionTag =~ /staging-.*/ + raise "Staging Tag #{promoteToProductionTag} does not exist." unless last_tag_matching(promoteToProductionTag) + + promoteToProductionTag =~ /staging-([0-9]{4}-[0-9]{2}-[0-9]{2}\.[0-9]*)/ + newProductionTag = "production_vagrant-#{$1}" + puts "promoting staging tag #{promoteToProductionTag} to production_vagrant as '#{newProductionTag}'" + system "git tag -a -m 'tagging current code for deployment to production_vagrant' #{newProductionTag} #{promoteToProductionTag}" + + set :branch, newProductionTag + end end end