From 276b51bc8e463411bdd1c714a1133a1aafe98d43 Mon Sep 17 00:00:00 2001 From: Mike Glenn Date: Fri, 1 Jan 2016 10:46:42 -0500 Subject: [PATCH 1/8] update gem dev environment --- Rakefile | 6 +++-- VERSION | 2 +- gitploy.gemspec | 62 ++++++++++++++++++++++++------------------------- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/Rakefile b/Rakefile index f6537b4..57bfedf 100644 --- a/Rakefile +++ b/Rakefile @@ -4,9 +4,11 @@ begin gemspec.name = "gitploy" gemspec.summary = "Deployment DSL created with git in mind" gemspec.description = "Dead-simple deployments. No, for real this time." - gemspec.email = "brentdillingham@gmail.com" + gemspec.email = ["brentdillingham@gmail.com", "mglenn@ilude.com"] gemspec.homepage = "http://github.com/brentd/gitploy" - gemspec.authors = ["Brent Dillingham"] + gemspec.authors = ["Brent Dillingham", "Mike Glenn"] + gemspec.add_development_dependency 'rspec' + gemspec.add_development_dependency 'aruba' end Jeweler::GemcutterTasks.new rescue LoadError diff --git a/VERSION b/VERSION index ee1372d..7179039 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.2 +0.2.3 diff --git a/gitploy.gemspec b/gitploy.gemspec index 752d43c..e34eb89 100644 --- a/gitploy.gemspec +++ b/gitploy.gemspec @@ -1,57 +1,55 @@ # Generated by jeweler # DO NOT EDIT THIS FILE DIRECTLY -# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command +# Instead, edit Jeweler::Tasks in rakefile, and run 'rake gemspec' # -*- encoding: utf-8 -*- +# stub: gitploy 0.2.2 ruby lib Gem::Specification.new do |s| - s.name = %q{gitploy} + s.name = "gitploy" s.version = "0.2.2" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= - s.authors = ["Brent Dillingham"] - s.date = %q{2011-03-17} - s.default_executable = %q{gitploy} - s.description = %q{Dead-simple deployments. No, for real this time.} - s.email = %q{brentdillingham@gmail.com} + s.require_paths = ["lib"] + s.authors = ["Brent Dillingham", "Mike Glenn"] + s.date = "2016-01-01" + s.description = "Dead-simple deployments. No, for real this time." + s.email = ["brentdillingham@gmail.com", "mglenn@ilude.com"] s.executables = ["gitploy"] s.extra_rdoc_files = [ "README.markdown" ] s.files = [ - ".gitignore", - "README.markdown", - "Rakefile", - "VERSION", - "bin/gitploy", - "features/gitploy.feature", - "features/step_definitions/gitploy_steps.rb", - "features/support/env.rb", - "gitploy.gemspec", - "lib/gitploy.rb", - "lib/gitploy/script.rb", - "spec/gitploy_config_spec.rb", - "spec/gitploy_spec.rb", - "spec/spec_helper.rb" - ] - s.homepage = %q{http://github.com/brentd/gitploy} - s.rdoc_options = ["--charset=UTF-8"] - s.require_paths = ["lib"] - s.rubygems_version = %q{1.3.7} - s.summary = %q{Deployment DSL created with git in mind} - s.test_files = [ + "README.markdown", + "Rakefile", + "VERSION", + "bin/gitploy", + "features/gitploy.feature", + "features/step_definitions/gitploy_steps.rb", + "features/support/env.rb", + "gitploy.gemspec", + "lib/gitploy.rb", + "lib/gitploy/script.rb", "spec/gitploy_config_spec.rb", - "spec/gitploy_spec.rb", - "spec/spec_helper.rb" + "spec/gitploy_spec.rb", + "spec/spec_helper.rb" ] + s.homepage = "http://github.com/brentd/gitploy" + s.rubygems_version = "2.4.4" + s.summary = "Deployment DSL created with git in mind" if s.respond_to? :specification_version then - current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION - s.specification_version = 3 + s.specification_version = 4 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) else + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) end else + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) end end From ea691aaf570a9260697fe9c870785d5b55b4e145 Mon Sep 17 00:00:00 2001 From: Mike Glenn Date: Fri, 1 Jan 2016 10:48:57 -0500 Subject: [PATCH 2/8] fix rspec depercation warnings --- spec/spec_helper.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index cb49a9a..7bb871c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,2 +1,10 @@ require 'rspec' +RSpec.configure do |config| + config.expect_with :rspec do |c| + c.syntax = :should + end + config.mock_with :rspec do |c| + c.syntax = :should + end +end \ No newline at end of file From 6d049cb8f14305f36876692a332132fabcc74687 Mon Sep 17 00:00:00 2001 From: Mike Glenn Date: Fri, 1 Jan 2016 10:50:10 -0500 Subject: [PATCH 3/8] current_user var for use in deployment scripts --- lib/gitploy.rb | 9 +++++++++ spec/gitploy_config_spec.rb | 2 +- spec/gitploy_spec.rb | 17 +++++++++++++++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/gitploy.rb b/lib/gitploy.rb index 8814ce7..76a13c4 100644 --- a/lib/gitploy.rb +++ b/lib/gitploy.rb @@ -117,6 +117,15 @@ def current_branch end end + def current_user + if user = `git config user.email` + user + else + 'unknown' + end + end + + def pretend? pretend = %w(-p --pretend) ARGV.any? { |v| pretend.include? v } diff --git a/spec/gitploy_config_spec.rb b/spec/gitploy_config_spec.rb index 5d43d53..b9c83a1 100644 --- a/spec/gitploy_config_spec.rb +++ b/spec/gitploy_config_spec.rb @@ -40,7 +40,7 @@ config.missing_options.should eql [:path] end it 'raises error when checked' do - Gitploy.stub!(:current_stage).and_return('test') + Gitploy.stub(:current_stage).and_return('test') config = Gitploy::Config.new config.user = 'test' config.host = 'test' diff --git a/spec/gitploy_spec.rb b/spec/gitploy_spec.rb index ceff128..ba741d8 100644 --- a/spec/gitploy_spec.rb +++ b/spec/gitploy_spec.rb @@ -125,7 +125,7 @@ end end it 'flushes run queue after running remote block' do - stub!(:pretty_run).and_return(nil) + stub(:pretty_run).and_return(nil) remote do run 'test' run 'test' @@ -148,7 +148,7 @@ instance_variable_get(:@run_queue).should == [] end it 'executes local command from block' do - stub!(:pretty_run).and_return(nil) + stub(:pretty_run).and_return(nil) local do run 'test' run 'test' @@ -161,11 +161,24 @@ push! end end + it 'remote command from block evals current_user var' do + stub(:current_user).and_return('deploy@example.org') + should_receive(:pretty_run).with("example.org", "ssh deploy@example.org 'test && deploy@example.org'") + remote do + run 'test' + run "#{current_user}" + end + end end context 'detecting environment' do it 'returns current git branch' do should_receive(:`).with('git symbolic-ref HEAD').and_return('refs/heads/test') send(:current_branch).should == 'test' end + + it 'returns current user email' do + should_receive(:`).with('git config user.email').and_return('deploy@example.org') + send(:current_user).should == 'deploy@example.org' + end end end From d9f8bedf2fd82cb07ac750e12c067b2f9fc27ff4 Mon Sep 17 00:00:00 2001 From: Mike Glenn Date: Fri, 1 Jan 2016 10:54:43 -0500 Subject: [PATCH 4/8] add echo command --- lib/gitploy.rb | 4 ++++ spec/gitploy_spec.rb | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/lib/gitploy.rb b/lib/gitploy.rb index 76a13c4..dbd4ad5 100644 --- a/lib/gitploy.rb +++ b/lib/gitploy.rb @@ -66,6 +66,10 @@ def sudo(cmd) run("sudo #{cmd}") end + def echo(cmd) + run("echo \"#{cmd}\"") + end + def rake(task) run("rake #{task}") end diff --git a/spec/gitploy_spec.rb b/spec/gitploy_spec.rb index ba741d8..7795a50 100644 --- a/spec/gitploy_spec.rb +++ b/spec/gitploy_spec.rb @@ -100,6 +100,11 @@ rake 'test' instance_variable_get(:@run_queue).should == ['rake test', 'rake test'] end + it 'adds echo commands to queue' do + echo 'test' + echo 'test' + instance_variable_get(:@run_queue).should == ["echo \"test\"", "echo \"test\""] + end it 'flushes command queue' do run 'test' run 'test' From cb044d8a01ea884b0b9e749164010ac9c7cda571 Mon Sep 17 00:00:00 2001 From: Mike Glenn Date: Fri, 1 Jan 2016 15:02:53 -0500 Subject: [PATCH 5/8] add newrelic deployment marker --- lib/gitploy.rb | 23 +++++++++++++++++++++-- spec/gitploy_spec.rb | 11 +++++++++++ spec/newrelic_deployment_marker_spec.rb | 20 ++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 spec/newrelic_deployment_marker_spec.rb diff --git a/lib/gitploy.rb b/lib/gitploy.rb index dbd4ad5..24b0251 100644 --- a/lib/gitploy.rb +++ b/lib/gitploy.rb @@ -78,6 +78,10 @@ def push! local { run "git push #{config.user}@#{config.host}:#{config.path}/.git #{config.local_branch}:#{config.remote_branch}" } end + def newrelic_deployment_marker(stage=current_stage, user=current_user, revision=current_revision, message=commit_message) + run "bundle exec newrelic deployments -e #{stage} --user=#{user} --revision=#{revision} #{message}" + end + private #FIXME move to representation class @@ -122,13 +126,28 @@ def current_branch end def current_user - if user = `git config user.email` - user + if result = `git config user.email` + result.chomp else 'unknown' end end + def current_revision + if result = `git rev-parse --short HEAD` + result.chomp + else + 'unknown' + end + end + + def commit_message + if result = `git log -1 --pretty=%B` + result.chomp + else + 'unknown' + end + end def pretend? pretend = %w(-p --pretend) diff --git a/spec/gitploy_spec.rb b/spec/gitploy_spec.rb index 7795a50..1007a32 100644 --- a/spec/gitploy_spec.rb +++ b/spec/gitploy_spec.rb @@ -84,6 +84,7 @@ end.should raise_error end end + context 'running queue' do it 'adds command to queue' do run 'test' @@ -185,5 +186,15 @@ should_receive(:`).with('git config user.email').and_return('deploy@example.org') send(:current_user).should == 'deploy@example.org' end + + it 'returns latest git revision fingerprint' do + should_receive(:`).with('git rev-parse --short HEAD').and_return('d9f8bed') + send(:current_revision).should == 'd9f8bed' + end + + it 'returns latest git commit message' do + should_receive(:`).with('git log -1 --pretty=%B').and_return('add echo command') + send(:commit_message).should == 'add echo command' + end end end diff --git a/spec/newrelic_deployment_marker_spec.rb b/spec/newrelic_deployment_marker_spec.rb new file mode 100644 index 0000000..7d5560a --- /dev/null +++ b/spec/newrelic_deployment_marker_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' +require File.expand_path(File.dirname(__FILE__) + "/../lib/gitploy") +include Gitploy + +describe 'Giploy' do + context 'newrelic_deployment_marker' do + it 'works without parameters' do + should_receive(:current_stage).and_return('test') + should_receive(:current_user).and_return('deploy@example.org') + should_receive(:current_revision).and_return('d9f8bed') + should_receive(:commit_message).and_return('add echo command') + newrelic_deployment_marker + send(:flush_run_queue).should == 'bundle exec newrelic deployments -e test --user=deploy@example.org --revision=d9f8bed add echo command' + end + it 'allows parameter overrides' do + newrelic_deployment_marker('staging', 'bob@barker.com', 'some_hash', 'test commit') + send(:flush_run_queue).should == 'bundle exec newrelic deployments -e staging --user=bob@barker.com --revision=some_hash test commit' + end + end +end \ No newline at end of file From f605730b9a2066206bacd475c602eba876d293ef Mon Sep 17 00:00:00 2001 From: Mike Glenn Date: Fri, 1 Jan 2016 15:08:47 -0500 Subject: [PATCH 6/8] add push with force option --- lib/gitploy.rb | 8 +++++++- spec/gitploy_spec.rb | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/gitploy.rb b/lib/gitploy.rb index 24b0251..543b13a 100644 --- a/lib/gitploy.rb +++ b/lib/gitploy.rb @@ -75,7 +75,8 @@ def rake(task) end def push! - local { run "git push #{config.user}@#{config.host}:#{config.path}/.git #{config.local_branch}:#{config.remote_branch}" } + force = "--force " if force? + local { run "git push #{force}#{config.user}@#{config.host}:#{config.path}/.git #{config.local_branch}:#{config.remote_branch}" } end def newrelic_deployment_marker(stage=current_stage, user=current_user, revision=current_revision, message=commit_message) @@ -153,4 +154,9 @@ def pretend? pretend = %w(-p --pretend) ARGV.any? { |v| pretend.include? v } end + + def force? + force = %w(-f --force) + ARGV.any? { |v| force.include? v } + end end diff --git a/spec/gitploy_spec.rb b/spec/gitploy_spec.rb index 1007a32..828a6d0 100644 --- a/spec/gitploy_spec.rb +++ b/spec/gitploy_spec.rb @@ -167,6 +167,14 @@ push! end end + it 'pushes local changes with force' do + ARGV[1] = '-f' + should_receive(:pretty_run).with("LOCAL", + "git push --force deploy@example.org:/var/apps/.git master:master") + deploy do + push! + end + end it 'remote command from block evals current_user var' do stub(:current_user).and_return('deploy@example.org') should_receive(:pretty_run).with("example.org", "ssh deploy@example.org 'test && deploy@example.org'") From ada0e0ebc067630327a4cd5a6300faf669f907a5 Mon Sep 17 00:00:00 2001 From: Mike Glenn Date: Fri, 1 Jan 2016 15:29:42 -0500 Subject: [PATCH 7/8] improve new relic option handling, update documentation and version --- README.markdown | 46 ++++++++++++++++++++++++- VERSION | 2 +- lib/gitploy.rb | 6 +++- spec/newrelic_deployment_marker_spec.rb | 2 +- 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/README.markdown b/README.markdown index 9905cca..b6e8f14 100644 --- a/README.markdown +++ b/README.markdown @@ -46,17 +46,61 @@ strategy. No hooks, very little behind-the-scenes magic - it just does what you run "cd #{config.path}" run "git reset --hard" run "bundle install --deployment" + echo "Deployment complete. Restarting server!" run "touch tmp/restart.txt" + + # uncomment the line below to send new relic a deployment marker + # newrelic_deployment_marker end end ### Usage $ gem install gitploy - # create config/deploy.rb + # create config/gitploy.rb $ gitploy production setup $ gitploy production + # In case of emergency you can do a git push with the --force option + $ gitploy production --force + +### New Relic Deployment Marker + +Gitploy supports sending [New Relic Deployment Markers](https://docs.newrelic.com/docs/apm/new-relic-apm/maintenance/deployment-notifications). + +In your config/gitploy.rb + + deploy do + push! + remote do + run "cd #{config.path}" + run "git reset --hard" + run "bundle install --deployment" + echo "Deployment complete. Restarting server!" + run "touch tmp/restart.txt" + + # new relic a deployment marker + newrelic_deployment_marker + end + end + +The newrelic_deployment_marker supports the following optional overrides: + + deploy do + push! + remote do + run "cd #{config.path}" + run "git reset --hard" + run "bundle install --deployment" + echo "Deployment complete. Restarting server!" + run "touch tmp/restart.txt" + + # new relic a deployment marker + newrelic_deployment_marker( stage: 'custom_stage_name', user: 'custom_user', revision: 'custom_revision', message: 'custom_message') + end + end + + ### Disclaimer Gitploy is super alpha - don't use it yet, unless you're just that baller. Are you? diff --git a/VERSION b/VERSION index 7179039..abd4105 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.3 +0.2.4 diff --git a/lib/gitploy.rb b/lib/gitploy.rb index 543b13a..b06b3ba 100644 --- a/lib/gitploy.rb +++ b/lib/gitploy.rb @@ -79,7 +79,11 @@ def push! local { run "git push #{force}#{config.user}@#{config.host}:#{config.path}/.git #{config.local_branch}:#{config.remote_branch}" } end - def newrelic_deployment_marker(stage=current_stage, user=current_user, revision=current_revision, message=commit_message) + def newrelic_deployment_marker( options = {} ) + stage = options[:stage] || current_stage + user = options[:user] || current_user + revision = options[:revision] || current_revision + message = options[:message] || commit_message run "bundle exec newrelic deployments -e #{stage} --user=#{user} --revision=#{revision} #{message}" end diff --git a/spec/newrelic_deployment_marker_spec.rb b/spec/newrelic_deployment_marker_spec.rb index 7d5560a..d2fb30f 100644 --- a/spec/newrelic_deployment_marker_spec.rb +++ b/spec/newrelic_deployment_marker_spec.rb @@ -13,7 +13,7 @@ send(:flush_run_queue).should == 'bundle exec newrelic deployments -e test --user=deploy@example.org --revision=d9f8bed add echo command' end it 'allows parameter overrides' do - newrelic_deployment_marker('staging', 'bob@barker.com', 'some_hash', 'test commit') + newrelic_deployment_marker(stage: 'staging', user: 'bob@barker.com', revision: 'some_hash', message: 'test commit') send(:flush_run_queue).should == 'bundle exec newrelic deployments -e staging --user=bob@barker.com --revision=some_hash test commit' end end From 3f6f69d63946e7685274b4c587de7880efff5875 Mon Sep 17 00:00:00 2001 From: Mike Glenn Date: Fri, 1 Jan 2016 15:45:28 -0500 Subject: [PATCH 8/8] add current_stage example to documentation --- README.markdown | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index b6e8f14..771f49d 100644 --- a/README.markdown +++ b/README.markdown @@ -45,7 +45,13 @@ strategy. No hooks, very little behind-the-scenes magic - it just does what you remote do run "cd #{config.path}" run "git reset --hard" - run "bundle install --deployment" + + echo "Bundle Install..."" + run "bundle install --quiet --deployment --without development test" + + echo "Migrating Database..." + run "bundle exec rake db:migrate RAILS_ENV=#{Gitploy.current_stage}" + echo "Deployment complete. Restarting server!" run "touch tmp/restart.txt"