capistrano-gitflow_version 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in capistrano-gitflow_version.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ capistrano-gitflow_version (0.0.1)
5
+ capistrano-ext (>= 1.2.1)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ capistrano (2.5.19)
11
+ highline
12
+ net-scp (>= 1.0.0)
13
+ net-sftp (>= 2.0.0)
14
+ net-ssh (>= 2.0.14)
15
+ net-ssh-gateway (>= 1.0.0)
16
+ capistrano-ext (1.2.1)
17
+ capistrano (>= 1.0.0)
18
+ highline (1.6.1)
19
+ net-scp (1.0.4)
20
+ net-ssh (>= 1.99.1)
21
+ net-sftp (2.0.5)
22
+ net-ssh (>= 2.0.9)
23
+ net-ssh (2.1.3)
24
+ net-ssh-gateway (1.0.1)
25
+ net-ssh (>= 1.99.1)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ capistrano-ext (>= 1.2.1)
32
+ capistrano-gitflow_version!
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2011 Alice Brown
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,82 @@
1
+ = gitflow_version: a Capistrano recipe for git deployment using tags in a multistage environment using MAJOR.MINOR.REVISION.BUILD
2
+
3
+ The best thing about this recipe is that there is almost nothing to learn -- your cap deploy process barely changes.
4
+ Gitflow simply adds some tagging/logging/workflow magic to the multistage recipe.
5
+
6
+ # BEFORE
7
+ $ cap deploy # 'deploy' goes to staging
8
+ $ cap production deploy # 'deploy' goes to production
9
+
10
+ # AFTER
11
+ $ cap deploy
12
+ # 'deploy' goes to staging; tagged test-MAJOR.MINOR.REVISION.BUILD
13
+ $ cap production deploy
14
+ # deploys latest staging tag, or if last tag is a production tag then that, to production
15
+ # for specifying the tag by hand add `-s tag=MAJOR.MINOR.REVISION.BUILD`
16
+ # tag 'test-MAJOR.MINOR.REVISION.BUILD' goes to production
17
+ # tag 'vMAJOR.MINOR.REVISION.BUILD' created; points to test-MAJOR.MINOR.REVISION.BUILD
18
+
19
+ # BONUS
20
+ cap gitflow_version:commit_log
21
+ # displays a commit log pushed to staging
22
+ # ... alternatively, if you're using GitHub, will open a page using branch compare
23
+ cap production gitflow_version:log_log
24
+ # displays a commit log of what will be pushed to production
25
+
26
+ == INSTALLATION
27
+
28
+ First, install the gem:
29
+
30
+ gem install capistrano-gitflow_version
31
+
32
+ Then update config/deploy.rb
33
+
34
+ require 'capistrano/gitflow_version'
35
+
36
+ == DETAILS
37
+
38
+ After experimenting with several workflows for deployment in git, I've finally found one I really like.
39
+
40
+ * You can push to staging at any time; every staging push is automatically tagged with a unique tag.
41
+ * You can only push a staging tag to production. This helps to enforce QA of all pushes to production.
42
+
43
+ === PUSH TO STAGING
44
+
45
+ Whenever you want to push the currently checked-out code to staging, just do:
46
+
47
+ cap staging deploy
48
+
49
+ gitflow will automatically:
50
+
51
+ * if the last tag was a test-MAJOR.MINOR.REVISION.BUILD will automatically increment BUILD
52
+ * if the last tag was vMAJOR.MINOR.REVISION.BUILD will ask you what the next tag should be
53
+ * create a new tag
54
+ * configure multistage to use that tag for the deploy
55
+ * push the code and tags to the remote "origin"
56
+ * and run the normal deploy task for the staging stage.
57
+
58
+ === PUSH TO PRODUCTION:
59
+
60
+ Whenever you want to push code to production, you must specify the staging tag you wish to promote to production:
61
+
62
+ cap production deploy -s tag=test-0.8.4.3
63
+
64
+ gitflow will automatically:
65
+
66
+ * alias the staging tag to a production tag without the test- prefix
67
+ * configure multistage to use that tag for the deploy
68
+ * push the code and tags to the remote "origin"
69
+ * and run the normal deploy task for the production stage.
70
+
71
+ === NOTES:
72
+
73
+ * you may need to wipe out the cached-copy on the remote server that cap uses when switching to this workflow; I have seen situations where the cached copy cannot cleanly checkout to the new branch/tag. it's safe to try without wiping it out first, it will fail gracefully.
74
+ * if your stages already have a "set :branch, 'my-staging-branch'" call in your configs, remove it. This workflow configures it automatically.
75
+
76
+ == CREDIT
77
+
78
+ Originally forked from Alan Pinstein's git_deployment repo. Gemified and hacked by Josh Nichols. There wasn't really a license originally, so...
79
+
80
+ Forked from Josh Nichols's capistrano-gitflow repo. Hacked by Alice Brown. Put under the MIT License. See the LICENSE file.
81
+
82
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "capistrano/gitflow_version/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "capistrano-gitflow_version"
7
+ s.version = Capistrano::GitflowVersion::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Joshua Nichols", "Alice Brown"]
10
+ s.email = ["josh@technicalpickles.com", "alice@alum.mit.edu"]
11
+ s.homepage = "https://github.com/ambtus/capistrano-gitflow"
12
+ s.summary = %q{Capistrano recipe for tagged deployment}
13
+ s.description = %q{Capistrano recipe for a deployment workflow based on git tags in MAJOR.MINOR.REVISION.BUILD format}
14
+
15
+ s.rubyforge_project = "capistrano-gitflow_version"
16
+
17
+ s.add_dependency('capistrano-ext', '>=1.2.1')
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+ end
@@ -0,0 +1,197 @@
1
+ require 'capistrano'
2
+ require 'capistrano/ext/multistage'
3
+ require 'capistrano-gitflow_version/natcmp'
4
+ require 'stringex'
5
+
6
+ module Capistrano
7
+ module GitflowVersion
8
+ def self.load_into(capistrano_configuration)
9
+ capistrano_configuration.load do
10
+ before "deploy:update_code", "gitflow_version:calculate_tag"
11
+ before "gitflow_version:calculate_tag", "gitflow_version:verify_up_to_date"
12
+
13
+ namespace :gitflow_version do
14
+ def last_tag_matching(pattern)
15
+ matching_tags = `git tag -l '#{pattern}'`.split
16
+ matching_tags.sort! do |a,b|
17
+ String.natcmp(b, a, true)
18
+ end
19
+
20
+ last_tag = if matching_tags.length > 0
21
+ matching_tags[0]
22
+ else
23
+ nil
24
+ end
25
+ end
26
+
27
+ def last_staging_tag()
28
+ last_tag_matching('test-*')
29
+ end
30
+
31
+ def next_staging_tag
32
+ new_tag_serial = if last_production_tag == next_production_tag
33
+ Capistrano::CLI.ui.ask("What is the release after #{last_production_tag}? (must be in MAJOR.MINOR.REVISION.BUILD format) ")
34
+ else
35
+ if last_staging_tag && last_staging_tag =~ /test-([0-9]+.[0-9]+.[0-9]+.)([0-9]+)/
36
+ $1 + ($2.to_i + 1).to_s
37
+ else
38
+ release = Capistrano::CLI.ui.ask("What is the starting release? (must be in MAJOR.MINOR.REVISION.BUILD format) ")
39
+ unless release =~ /[0-9]+.[0-9]+.[0-9]+.[0-9]+/
40
+ abort "#{release} must be in MAJOR.MINOR.REVISION.BUILD format. example: 0.8.5.0"
41
+ end
42
+ release
43
+ end
44
+ end
45
+
46
+
47
+ "test-#{new_tag_serial}"
48
+ end
49
+
50
+ def last_production_tag()
51
+ last_tag_matching('v*')
52
+ end
53
+
54
+ def next_production_tag
55
+ last_staging_tag = last_tag_matching("test-*")
56
+
57
+ last_staging_tag =~ /^test-(.*)$/
58
+ "v#{$1}"
59
+ end
60
+
61
+ def using_git?
62
+ fetch(:scm, :git).to_sym == :git
63
+ end
64
+
65
+ task :verify_up_to_date do
66
+ if using_git?
67
+ set :local_branch, `git branch --no-color 2> /dev/null | sed -e '/^[^*]/d'`.gsub(/\* /, '').chomp
68
+ set :local_sha, `git log --pretty=format:%H HEAD -1`.chomp
69
+ set :origin_sha, `git log --pretty=format:%H #{local_branch} -1`
70
+ unless local_sha == origin_sha
71
+ abort """
72
+ Your #{local_branch} branch is not up to date with origin/#{local_branch}.
73
+ Please make sure you have pulled and pushed all code before deploying:
74
+
75
+ git pull origin #{local_branch}
76
+ #run tests, etc
77
+ git push origin #{local_branch}
78
+
79
+ """
80
+ end
81
+ end
82
+ end
83
+
84
+ desc "Calculate the tag to deploy"
85
+ task :calculate_tag do
86
+ if using_git?
87
+ # make sure we have any other deployment tags that have been pushed by others so our auto-increment code doesn't create conflicting tags
88
+ `git fetch`
89
+
90
+ send "tag_#{stage}" if respond_to?(stage)
91
+
92
+ system "git push --tags origin #{local_branch}"
93
+ if $? != 0
94
+ abort "git push failed"
95
+ end
96
+ end
97
+ end
98
+
99
+ desc "Show log between most recent staging tag (or given tag=XXX) and last production release."
100
+ task :commit_log do
101
+ from_tag = if stage == :production
102
+ last_production_tag
103
+ elsif stage == :staging
104
+ last_staging_tag
105
+ else
106
+ abort "Unsupported stage #{stage}"
107
+ end
108
+
109
+ # no idea how to properly test for an optional cap argument a la '-s tag=x'
110
+ to_tag = capistrano_configuration[:tag]
111
+ to_tag ||= begin
112
+ puts "Calculating 'end' tag for :commit_log for '#{stage}'"
113
+ to_tag = if stage == :production
114
+ last_staging_tag
115
+ elsif stage == :staging
116
+ 'master'
117
+ else
118
+ abort "Unsupported stage #{stage}"
119
+ end
120
+ end
121
+
122
+
123
+ command = if `git config remote.origin.url` =~ /git@github.com:(.*)\/(.*).git/
124
+ "open https://github.com/#{$1}/#{$2}/compare/#{from_tag}...#{to_tag || 'master'}"
125
+ else
126
+ log_subcommand = if ENV['git_log_command'] && ENV['git_log_command'].strip != ''
127
+ ENV['git_log_command']
128
+ else
129
+ 'log'
130
+ end
131
+ "git #{log_subcommand} #{fromTag}..#{toTag}"
132
+ end
133
+ puts command
134
+ system command
135
+ end
136
+
137
+ desc "Mark the current code as a staging/qa release"
138
+ task :tag_staging do
139
+ current_sha = `git log --pretty=format:%H HEAD -1`
140
+ last_staging_tag_sha = if last_staging_tag
141
+ `git log --pretty=format:%H #{last_staging_tag} -1`
142
+ end
143
+
144
+ if last_staging_tag_sha == current_sha
145
+ puts "Not re-tagging staging because the most recent tag (#{last_staging_tag}) already points to current head"
146
+ new_staging_tag = last_staging_tag
147
+ else
148
+ new_staging_tag = next_staging_tag
149
+ puts "Tagging current branch for deployment to staging as '#{new_staging_tag}'"
150
+ system "git tag -a -m 'tagging current code for deployment to staging' #{new_staging_tag}"
151
+ end
152
+
153
+ set :branch, new_staging_tag
154
+ end
155
+
156
+ desc "Push the last staging tag to production."
157
+ task :tag_production do
158
+ promote_to_production_tag = last_staging_tag
159
+
160
+ unless promote_to_production_tag && promote_to_production_tag =~ /test-.*/
161
+ abort "Couldn't find a staging tag to deploy; use '-s tag=test-MAJOR.MINOR.REVISION.BUILD'"
162
+ end
163
+ unless last_tag_matching(promote_to_production_tag)
164
+ abort "Staging tag #{promote_to_production_tag} does not exist."
165
+ end
166
+
167
+ promote_to_production_tag =~ /^test-(.*)$/
168
+ new_production_tag = "v#{$1}"
169
+
170
+ if new_production_tag == last_production_tag
171
+ puts "using current production tag '#{new_production_tag}'"
172
+ else
173
+ puts "promoting staging tag #{promote_to_production_tag} to production as '#{new_production_tag}'"
174
+ system "git tag -a -m 'tagging current code for deployment to production' #{new_production_tag} #{promote_to_production_tag}"
175
+ end
176
+
177
+ set :branch, new_production_tag
178
+ end
179
+ end
180
+
181
+ namespace :deploy do
182
+ namespace :pending do
183
+ task :compare do
184
+ gitflow_version.commit_log
185
+ end
186
+ end
187
+ end
188
+
189
+ end
190
+
191
+ end
192
+ end
193
+ end
194
+
195
+ if Capistrano::Configuration.instance
196
+ Capistrano::GitflowVersion.load_into(Capistrano::Configuration.instance)
197
+ end
@@ -0,0 +1,76 @@
1
+ # natcmp.rb
2
+ #
3
+ # Natural order comparison of two strings
4
+ # e.g. "my_prog_v1.1.0" < "my_prog_v1.2.0" < "my_prog_v1.10.0"
5
+ # which does not follow alphabetically
6
+ #
7
+ # Based on Martin Pool's "Natural Order String Comparison" originally written in C
8
+ # http://sourcefrog.net/projects/natsort/
9
+ #
10
+ # This implementation is Copyright (C) 2003 by Alan Davies
11
+ # <cs96and_AT_yahoo_DOT_co_DOT_uk>
12
+ #
13
+ # This software is provided 'as-is', without any express or implied
14
+ # warranty. In no event will the authors be held liable for any damages
15
+ # arising from the use of this software.
16
+ #
17
+ # Permission is granted to anyone to use this software for any purpose,
18
+ # including commercial applications, and to alter it and redistribute it
19
+ # freely, subject to the following restrictions:
20
+ #
21
+ # 1. The origin of this software must not be misrepresented; you must not
22
+ # claim that you wrote the original software. If you use this software
23
+ # in a product, an acknowledgment in the product documentation would be
24
+ # appreciated but is not required.
25
+ # 2. Altered source versions must be plainly marked as such, and must not be
26
+ # misrepresented as being the original software.
27
+ # 3. This notice may not be removed or altered from any source distribution.
28
+
29
+ class String
30
+
31
+ # 'Natural order' comparison of two strings
32
+ def String.natcmp(str1, str2, caseInsensitive=false)
33
+ str1, str2 = str1.dup, str2.dup
34
+ compareExpression = /^(\D*)(\d*)(.*)$/
35
+
36
+ if caseInsensitive
37
+ str1.downcase!
38
+ str2.downcase!
39
+ end
40
+
41
+ # Remove all whitespace
42
+ str1.gsub!(/\s*/, '')
43
+ str2.gsub!(/\s*/, '')
44
+
45
+ while (str1.length > 0) or (str2.length > 0) do
46
+ # Extract non-digits, digits and rest of string
47
+ str1 =~ compareExpression
48
+ chars1, num1, str1 = $1.dup, $2.dup, $3.dup
49
+
50
+ str2 =~ compareExpression
51
+ chars2, num2, str2 = $1.dup, $2.dup, $3.dup
52
+
53
+ # Compare the non-digits
54
+ case (chars1 <=> chars2)
55
+ when 0 # Non-digits are the same, compare the digits...
56
+ # If either number begins with a zero, then compare alphabetically,
57
+ # otherwise compare numerically
58
+ if (num1[0] != 48) and (num2[0] != 48)
59
+ num1, num2 = num1.to_i, num2.to_i
60
+ end
61
+
62
+ case (num1 <=> num2)
63
+ when -1 then return -1
64
+ when 1 then return 1
65
+ end
66
+ when -1 then return -1
67
+ when 1 then return 1
68
+ end # case
69
+
70
+ end # while
71
+
72
+ # Strings are naturally equal
73
+ return 0
74
+ end
75
+
76
+ end # class String
@@ -0,0 +1,5 @@
1
+ module Capistrano
2
+ module GitflowVersion
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ # Just need to add to LOAD_PATH, so we can require 'gitflow'
2
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-gitflow_version
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Joshua Nichols
13
+ - Alice Brown
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-03 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: capistrano-ext
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ segments:
30
+ - 1
31
+ - 2
32
+ - 1
33
+ version: 1.2.1
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ description: Capistrano recipe for a deployment workflow based on git tags in MAJOR.MINOR.REVISION.BUILD format
37
+ email:
38
+ - josh@technicalpickles.com
39
+ - alice@alum.mit.edu
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - Gemfile.lock
50
+ - LICENSE
51
+ - README.rdoc
52
+ - Rakefile
53
+ - capistrano-gitflow_version.gemspec
54
+ - lib/capistrano/gitflow_version.rb
55
+ - lib/capistrano/gitflow_version/natcmp.rb
56
+ - lib/capistrano/gitflow_version/version.rb
57
+ - recipes/gitflow_recipes.rb
58
+ has_rdoc: true
59
+ homepage: https://github.com/ambtus/capistrano-gitflow
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ requirements: []
84
+
85
+ rubyforge_project: capistrano-gitflow_version
86
+ rubygems_version: 1.3.7
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Capistrano recipe for tagged deployment
90
+ test_files: []
91
+