diff --git a/README.markdown b/README.markdown index 9905cca..771f49d 100644 --- a/README.markdown +++ b/README.markdown @@ -45,18 +45,68 @@ 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" + + # 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/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..abd4105 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.2 +0.2.4 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 diff --git a/lib/gitploy.rb b/lib/gitploy.rb index 8814ce7..b06b3ba 100644 --- a/lib/gitploy.rb +++ b/lib/gitploy.rb @@ -66,12 +66,25 @@ def sudo(cmd) run("sudo #{cmd}") end + def echo(cmd) + run("echo \"#{cmd}\"") + end + def rake(task) run("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( 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 private @@ -117,8 +130,37 @@ def current_branch end end + def current_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) 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_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..828a6d0 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' @@ -100,6 +101,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' @@ -125,7 +131,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 +154,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 +167,42 @@ 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'") + 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 + + 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..d2fb30f --- /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(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 +end \ No newline at end of file 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