Skip to content

Heroku deploy #70

Closed
wants to merge 11 commits into from
View
34 lib/kumade/deployer.rb
@@ -1,5 +1,5 @@
require "rake"
-require 'cocaine'
+require "cocaine"
module Kumade
class Deployer < Base
@@ -14,49 +14,23 @@ def initialize
end
def deploy
- begin
- ensure_heroku_remote_exists
- pre_deploy
- heroku.sync
- heroku.migrate_database
- rescue
- ensure
- post_deploy
- end
- end
-
- def pre_deploy
+ heroku.pre_deploy
ensure_clean_git
package_assets
sync_origin
+ heroku.deploy
end
def package_assets
- @packager.run
+ packager.run
end
def sync_origin
git.push(@branch)
end
- def post_deploy
- heroku.delete_deploy_branch
- end
-
def ensure_clean_git
git.ensure_clean_git
end
-
- def ensure_heroku_remote_exists
- if git.remote_exists?(Kumade.configuration.environment)
- if git.heroku_remote?
- success("#{Kumade.configuration.environment} is a Heroku remote")
- else
- error(%{Cannot deploy: "#{Kumade.configuration.environment}" remote does not point to Heroku})
- end
- else
- error(%{Cannot deploy: "#{Kumade.configuration.environment}" remote does not exist})
- end
- end
end
end
View
1 lib/kumade/git.rb
@@ -15,6 +15,7 @@ def self.environments
end
def push(branch, remote = 'origin', force = false)
+ return unless remote_exists?(remote)
command = ["git push"]
command << "-f" if force
command << remote
View
32 lib/kumade/heroku.rb
@@ -11,6 +11,38 @@ def initialize
@branch = @git.current_branch
end
+ def pre_deploy
+ ensure_heroku_remote_exists
+ end
+
+ def deploy
+ begin
+ sync
+ migrate_database
+ rescue Kumade::DeploymentError
+ raise
+ rescue
+ ensure
+ post_deploy
+ end
+ end
+
+ def post_deploy
+ delete_deploy_branch
+ end
+
+ def ensure_heroku_remote_exists
+ if git.remote_exists?(Kumade.configuration.environment)
+ if git.heroku_remote?
+ success("#{Kumade.configuration.environment} is a Heroku remote")
+ else
+ error(%{Cannot deploy: "#{Kumade.configuration.environment}" remote does not point to Heroku})
+ end
+ else
+ error(%{Cannot deploy: "#{Kumade.configuration.environment}" remote does not exist})
+ end
+ end
+
def sync
git.create(DEPLOY_BRANCH)
git.push("#{DEPLOY_BRANCH}:master", Kumade.configuration.environment, true)
View
96 spec/kumade/deployer_spec.rb
@@ -1,38 +1,12 @@
require 'spec_helper'
-describe Kumade::Deployer, "#pre_deploy" do
- let(:git) { subject.git }
-
- it "calls the correct methods" do
- git.expects(:ensure_clean_git)
- subject.expects(:package_assets)
- git.expects(:push).with(subject.git.current_branch)
-
- subject.pre_deploy
- end
-end
-
describe Kumade::Deployer, "#deploy" do
- let(:remote_name) { 'staging' }
-
- before do
- STDOUT.stubs(:puts)
- force_add_heroku_remote(remote_name)
- end
-
it "calls the correct methods" do
- subject.expects(:pre_deploy)
- subject.heroku.expects(:sync)
- subject.heroku.expects(:migrate_database)
- subject.expects(:post_deploy)
-
- subject.deploy
- end
-
- it "calls post_deploy if deploy fails" do
- subject.git.stubs(:heroku_remote?).raises(RuntimeError)
-
- subject.expects(:post_deploy)
+ subject.heroku.expects(:pre_deploy)
+ subject.expects(:ensure_clean_git)
+ subject.expects(:package_assets)
+ subject.expects(:sync_origin)
+ subject.heroku.expects(:deploy)
subject.deploy
end
@@ -59,66 +33,14 @@
end
end
-describe Kumade::Deployer, "#ensure_heroku_remote_exists" do
- let(:environment) { 'staging' }
-
- before do
- force_add_heroku_remote(environment)
- Kumade.configuration.environment = environment
- end
-
- context "when the remote points to Heroku" do
- before { STDOUT.stubs(:puts) }
-
- it "does not print an error" do
- subject.ensure_heroku_remote_exists
-
- STDOUT.should have_received(:puts).with(regexp_matches(/==> !/)).never
- end
-
- it "prints a success message" do
- subject.ensure_heroku_remote_exists
-
- STDOUT.should have_received(:puts).with(regexp_matches(/#{environment} is a Heroku remote/))
- end
- end
-
- context "when the remote does not exist" do
- before do
- remove_remote(environment)
- STDOUT.stubs(:puts)
- end
-
- it "prints an error" do
- lambda { subject.ensure_heroku_remote_exists }.should raise_error(Kumade::DeploymentError)
-
- STDOUT.should have_received(:puts).with(regexp_matches(/Cannot deploy: "#{environment}" remote does not exist/))
- end
- end
-
- context "when the remote does not point to Heroku" do
- let(:bad_environment) { 'bad' }
-
- before do
- `git remote add #{bad_environment} blerg@example.com`
- STDOUT.stubs(:puts)
- Kumade.configuration.environment = bad_environment
- end
-
- it "prints an error" do
- lambda { subject.ensure_heroku_remote_exists }.should raise_error(Kumade::DeploymentError)
-
- STDOUT.should have_received(:puts).with(regexp_matches(/Cannot deploy: "#{bad_environment}" remote does not point to Heroku/))
- end
- end
-end
-
describe Kumade::Deployer, "packaging" do
- let(:git) { stub("git", :current_branch => "awesome", :delete => true) }
- let(:packager) { stub("packager", :run => true) }
+ let(:git) { stub_everything("git") }
+ let(:heroku) { stub_everything("heroku") }
+ let(:packager) { stub_everything("packager") }
before do
Kumade::Git.stubs(:new => git)
+ Kumade::Heroku.stubs(:new => heroku)
Kumade::Packager.stubs(:new => packager)
end
View
51 spec/kumade/git_spec.rb
@@ -43,28 +43,43 @@
describe Kumade::Git, "#push" do
let(:branch) { 'branch' }
let(:remote) { 'my-remote' }
- let(:command_line) { stub("Kumade::CommandLine instance", :run_or_error => true) }
- before do
- Kumade::CommandLine.stubs(:new => command_line)
- subject.stubs(:success)
- end
+ context "when the remote exists" do
+ let(:command_line) { stub("Kumade::CommandLine instance", :run_or_error => true) }
- it "pushes to the correct remote" do
- subject.push(branch, remote)
- Kumade::CommandLine.should have_received(:new).with("git push #{remote} #{branch}")
- command_line.should have_received(:run_or_error).once
- end
+ before do
+ Kumade::CommandLine.stubs(:new => command_line)
+ subject.stubs(:success)
+ subject.stubs(:remote_exists? => true)
+ end
- it "can force push" do
- subject.push(branch, remote, true)
- Kumade::CommandLine.should have_received(:new).with("git push -f #{remote} #{branch}")
- command_line.should have_received(:run_or_error).once
- end
+ it "pushes to the correct remote" do
+ subject.push(branch, remote)
+ Kumade::CommandLine.should have_received(:new).with("git push #{remote} #{branch}")
+ command_line.should have_received(:run_or_error).once
+ end
- it "prints a success message" do
- subject.push(branch, remote)
- subject.should have_received(:success).with("Pushed #{branch} -> #{remote}")
+ it "can force push" do
+ subject.push(branch, remote, true)
+ Kumade::CommandLine.should have_received(:new).with("git push -f #{remote} #{branch}")
+ command_line.should have_received(:run_or_error).once
+ end
+
+ it "prints a success message" do
+ subject.push(branch, remote)
+ subject.should have_received(:success).with("Pushed #{branch} -> #{remote}")
+ end
+ end
+
+ context "when the remote does not exist" do
+ before do
+ subject.stubs(:remote_exists? => false)
+ end
+
+ it "returns silently" do
+ subject.push(branch)
+ Kumade::CommandLine.should have_received(:new).never
+ end
end
end
View
90 spec/kumade/heroku_spec.rb
@@ -6,6 +6,96 @@
it { should == "deploy" }
end
+describe Kumade::Heroku, "#pre_deploy" do
+ it "ensures heroku remote exists" do
+ subject.expects(:ensure_heroku_remote_exists)
+ subject.pre_deploy
+ end
+end
+
+describe Kumade::Heroku, "#deploy" do
+ it "sync's and migrate's" do
+ subject.expects(:sync)
+ subject.expects(:migrate_database)
+ subject.expects(:post_deploy)
+ subject.deploy
+ end
+
+ it "calls post_deploy if the deploy fails" do
+ subject.stubs(:sync).raises(RuntimeError)
+ subject.expects(:post_deploy)
+ subject.deploy
+ end
+
+ it "reraises Kumade::DeploymentError's" do
+ subject.stubs(:sync).raises(Kumade::DeploymentError)
+ subject.expects(:post_deploy)
+
+ expect { subject.deploy }.to raise_error(Kumade::DeploymentError)
+ end
+end
+
+describe Kumade::Heroku, "#post_deploy" do
+ it "deletes the deploy branch" do
+ subject.expects(:delete_deploy_branch)
+ subject.post_deploy
+ end
+end
+
+describe Kumade::Heroku, "#ensure_heroku_remote_exists" do
+ let(:environment) { 'staging' }
+
+ before do
+ force_add_heroku_remote(environment)
+ Kumade.configuration.environment = environment
+ end
+
+ context "when the remote points to Heroku" do
+ before { STDOUT.stubs(:puts) }
+
+ it "does not print an error" do
+ subject.ensure_heroku_remote_exists
+
+ STDOUT.should have_received(:puts).with(regexp_matches(/==> !/)).never
+ end
+
+ it "prints a success message" do
+ subject.ensure_heroku_remote_exists
+
+ STDOUT.should have_received(:puts).with(regexp_matches(/#{environment} is a Heroku remote/))
+ end
+ end
+
+ context "when the remote does not exist" do
+ before do
+ remove_remote(environment)
+ STDOUT.stubs(:puts)
+ end
+
+ it "prints an error" do
+ lambda { subject.ensure_heroku_remote_exists }.should raise_error(Kumade::DeploymentError)
+
+ STDOUT.should have_received(:puts).with(regexp_matches(/Cannot deploy: "#{environment}" remote does not exist/))
+ end
+ end
+
+ context "when the remote does not point to Heroku" do
+ let(:bad_environment) { 'bad' }
+
+ before do
+ `git remote add #{bad_environment} blerg@example.com`
+ STDOUT.stubs(:puts)
+ Kumade.configuration.environment = bad_environment
+ end
+
+ it "prints an error" do
+ lambda { subject.ensure_heroku_remote_exists }.should raise_error(Kumade::DeploymentError)
+
+ STDOUT.should have_received(:puts).with(regexp_matches(/Cannot deploy: "#{bad_environment}" remote does not point to Heroku/))
+ end
+ end
+end
+
describe Kumade::Heroku, "#sync" do
let(:environment) { 'staging' }
Something went wrong with that request. Please try again.