Skip to content

Commit

Permalink
add rake deploy command
Browse files Browse the repository at this point in the history
  • Loading branch information
doodzik committed May 16, 2019
1 parent e3d982a commit 027ca68
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 3 deletions.
1 change: 1 addition & 0 deletions Rakefile
Expand Up @@ -22,3 +22,4 @@ Rake::TestTask.new(:test) do |t|
end

task default: :test

58 changes: 58 additions & 0 deletions app/models/shipit/command_line_user.rb
@@ -0,0 +1,58 @@
module Shipit
class CommandLineUser
def present?
false
end

def email
'command_line@example.com'
end

def login
'command_line'
end

def name
'CommandLine'
end

def avatar_url
'https://github.com/images/error/octocat_happy.gif'
end

def id
end

def github_id
end

def logged_in?
false
end

def authorized?
Shipit.authentication_disabled?
end

def stacks_contributed_to
[]
end

def avatar_uri
User::DEFAULT_AVATAR.dup
end

def created_at
Time.at(0).utc
end
alias_method :updated_at, :created_at

def read_attribute_for_serialization(attr)
public_send(attr)
end

def github_api
Shipit.github.api
end
end
end
16 changes: 13 additions & 3 deletions app/models/shipit/stack.rb
Expand Up @@ -124,10 +124,11 @@ def build_deploy(until_commit, user, env: nil, force: false)
)
end

def trigger_deploy(*args)
deploy = build_deploy(*args)
def trigger_deploy(*args, **kwargs)
run_now = kwargs.delete(:run_now)
deploy = build_deploy(*args, **kwargs)
deploy.save!
deploy.enqueue
run_now ? deploy.run_now! : deploy.enqueue
continuous_delivery_resumed!
deploy
end
Expand Down Expand Up @@ -401,6 +402,15 @@ def to_param
[repo_owner, repo_name, environment].join('/')
end

def self.run_deploy_in_foreground(stack:, revision:)
stack = Shipit::Stack.from_param!(stack)
until_commit = stack.commits.where(sha: revision).limit(1)
env = stack.cached_deploy_spec.default_deploy_env
current_user = Shipit::CommandLineUser.new

stack.trigger_deploy(until_commit, current_user, env: env, force: true, run_now: true)
end

def self.from_param!(param)
repo_owner, repo_name, environment = param.split('/')
where(
Expand Down
5 changes: 5 additions & 0 deletions app/models/shipit/task.rb
Expand Up @@ -176,6 +176,11 @@ def enqueue
PerformTaskJob.perform_later(self)
end

def run_now!
raise "only persisted jobs can be run" unless persisted?
PerformTaskJob.perform_now(self)
end

def write(text)
chunks.create!(text: text)
end
Expand Down
4 changes: 4 additions & 0 deletions app/serializers/shipit/command_line_user_serializer.rb
@@ -0,0 +1,4 @@
module Shipit
class CommandLineUserSerializer < UserSerializer
end
end
27 changes: 27 additions & 0 deletions lib/tasks/shipit.rake
@@ -0,0 +1,27 @@
namespace :shipit do
desc "Deploy from a running instance. "
task deploy: :environment do
begin
stack = ENV['stack']
revision = ENV['revision']

raise ArgumentError.new('The first argument has to be a stack, e.g. shopify/shipit/production') if stack.nil?
raise ArgumentError.new('The second argument has to be a revision') if revision.nil?

module Shipit
class Task
def write(text)
p text
chunks.create!(text: text)
end
end
end

Shipit::Stack.run_deploy_in_foreground(stack: stack, revision: revision)
rescue ArgumentError
p "Use this command as follows:"
p "bundle exec rake shipit:deploy stack='shopify/shipit/production' revision='593b1f07cec6c30df3f62d9e63b31dc0ff444098'"
raise
end
end
end
10 changes: 10 additions & 0 deletions test/models/deploys_test.rb
Expand Up @@ -23,6 +23,16 @@ def setup
assert_raise(RuntimeError) { Deploy.new.enqueue }
end

test "run_now! when not persisted" do
assert_raise(RuntimeError) { Deploy.new.run_now! }
end

test "run_now! runs in foreground" do
PerformTaskJob.any_instance.expects(:perform).once

@deploy.run_now!
end

test "working_directory" do
assert_equal File.join(@deploy.stack.deploys_path, @deploy.id.to_s), @deploy.working_directory
end
Expand Down
19 changes: 19 additions & 0 deletions test/models/stacks_test.rb
Expand Up @@ -133,6 +133,15 @@ def setup
assert_instance_of Deploy, deploy
end

test "#trigger_deploy doesn't enqueues a deploy job when run_now is provided" do
@stack.deploys.destroy_all
Deploy.any_instance.expects(:run_now!).once

last_commit = shipit_commits(:third)
deploy = @stack.trigger_deploy(last_commit, AnonymousUser.new, run_now: true)
assert_instance_of Deploy, deploy
end

test "#trigger_deploy marks the deploy as `ignored_safeties` if the commit wasn't deployable" do
last_commit = shipit_commits(:fifth)
refute_predicate last_commit, :deployable?
Expand Down Expand Up @@ -263,6 +272,16 @@ def setup
assert @stack.active_task?
end

test ".run_deploy_in_foreground triggers a deploy" do
stack = Stack.create!(repo_owner: 'foo', repo_name: 'bar', environment: 'production')
commit = shipit_commits(:first)
stack.commits << commit

Stack.any_instance.expects(:trigger_deploy).with(anything, anything, env: {}, force: true, run_now: true)

Stack.run_deploy_in_foreground(stack: stack.to_param, revision: commit.sha)
end

test "#active_task? is memoized" do
assert_queries(1) do
10.times { @stack.active_task? }
Expand Down

0 comments on commit 027ca68

Please sign in to comment.