Skip to content

Commit ad07773

Browse files
authored
Merge pull request #895 from Shopify/rake_deploy
Add rake deploy command
2 parents 6170a31 + 2209890 commit ad07773

File tree

7 files changed

+136
-3
lines changed

7 files changed

+136
-3
lines changed
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
module Shipit
2+
class CommandLineUser
3+
def present?
4+
false
5+
end
6+
7+
def email
8+
9+
end
10+
11+
def login
12+
'command_line'
13+
end
14+
15+
def name
16+
'CommandLine'
17+
end
18+
19+
def avatar_url
20+
'https://github.com/images/error/octocat_happy.gif'
21+
end
22+
23+
def id
24+
end
25+
26+
def github_id
27+
end
28+
29+
def logged_in?
30+
false
31+
end
32+
33+
def authorized?
34+
Shipit.authentication_disabled?
35+
end
36+
37+
def stacks_contributed_to
38+
[]
39+
end
40+
41+
def avatar_uri
42+
User::DEFAULT_AVATAR.dup
43+
end
44+
45+
def created_at
46+
Time.at(0).utc
47+
end
48+
alias_method :updated_at, :created_at
49+
50+
def read_attribute_for_serialization(attr)
51+
public_send(attr)
52+
end
53+
54+
def github_api
55+
Shipit.github.api
56+
end
57+
end
58+
end

app/models/shipit/stack.rb

+13-3
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,11 @@ def build_deploy(until_commit, user, env: nil, force: false)
124124
)
125125
end
126126

127-
def trigger_deploy(*args)
128-
deploy = build_deploy(*args)
127+
def trigger_deploy(*args, **kwargs)
128+
run_now = kwargs.delete(:run_now)
129+
deploy = build_deploy(*args, **kwargs)
129130
deploy.save!
130-
deploy.enqueue
131+
run_now ? deploy.run_now! : deploy.enqueue
131132
continuous_delivery_resumed!
132133
deploy
133134
end
@@ -401,6 +402,15 @@ def to_param
401402
[repo_owner, repo_name, environment].join('/')
402403
end
403404

405+
def self.run_deploy_in_foreground(stack:, revision:)
406+
stack = Shipit::Stack.from_param!(stack)
407+
until_commit = stack.commits.where(sha: revision).limit(1).first
408+
env = stack.cached_deploy_spec.default_deploy_env
409+
current_user = Shipit::CommandLineUser.new
410+
411+
stack.trigger_deploy(until_commit, current_user, env: env, force: true, run_now: true)
412+
end
413+
404414
def self.from_param!(param)
405415
repo_owner, repo_name, environment = param.split('/')
406416
where(

app/models/shipit/task.rb

+5
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ def enqueue
176176
PerformTaskJob.perform_later(self)
177177
end
178178

179+
def run_now!
180+
raise "only persisted jobs can be run" unless persisted?
181+
PerformTaskJob.perform_now(self)
182+
end
183+
179184
def write(text)
180185
chunks.create!(text: text)
181186
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Shipit
2+
class CommandLineUserSerializer < UserSerializer
3+
end
4+
end

lib/tasks/shipit.rake

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace :shipit do
2+
desc "Deploy from a running instance. "
3+
task deploy: :environment do
4+
begin
5+
stack = ENV['stack']
6+
revision = ENV['revision']
7+
8+
raise ArgumentError.new('The first argument has to be a stack, e.g. shopify/shipit/production') if stack.nil?
9+
raise ArgumentError.new('The second argument has to be a revision') if revision.nil?
10+
11+
module Shipit
12+
class Task
13+
def write(text)
14+
p text
15+
chunks.create!(text: text)
16+
end
17+
end
18+
end
19+
20+
Shipit::Stack.run_deploy_in_foreground(stack: stack, revision: revision)
21+
rescue ArgumentError
22+
p "Use this command as follows:"
23+
p "bundle exec rake shipit:deploy stack='shopify/shipit/production' revision='$SHA'"
24+
raise
25+
end
26+
end
27+
end

test/models/deploys_test.rb

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ def setup
2323
assert_raise(RuntimeError) { Deploy.new.enqueue }
2424
end
2525

26+
test "run_now! when not persisted" do
27+
assert_raise(RuntimeError) { Deploy.new.run_now! }
28+
end
29+
30+
test "run_now! runs in foreground" do
31+
PerformTaskJob.any_instance.expects(:perform).once
32+
33+
@deploy.run_now!
34+
end
35+
2636
test "working_directory" do
2737
assert_equal File.join(@deploy.stack.deploys_path, @deploy.id.to_s), @deploy.working_directory
2838
end

test/models/stacks_test.rb

+19
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ def setup
133133
assert_instance_of Deploy, deploy
134134
end
135135

136+
test "#trigger_deploy doesn't enqueues a deploy job when run_now is provided" do
137+
@stack.deploys.destroy_all
138+
Deploy.any_instance.expects(:run_now!).once
139+
140+
last_commit = shipit_commits(:third)
141+
deploy = @stack.trigger_deploy(last_commit, AnonymousUser.new, run_now: true)
142+
assert_instance_of Deploy, deploy
143+
end
144+
136145
test "#trigger_deploy marks the deploy as `ignored_safeties` if the commit wasn't deployable" do
137146
last_commit = shipit_commits(:fifth)
138147
refute_predicate last_commit, :deployable?
@@ -263,6 +272,16 @@ def setup
263272
assert @stack.active_task?
264273
end
265274

275+
test ".run_deploy_in_foreground triggers a deploy" do
276+
stack = Stack.create!(repo_owner: 'foo', repo_name: 'bar', environment: 'production')
277+
commit = shipit_commits(:first)
278+
stack.commits << commit
279+
280+
Stack.any_instance.expects(:trigger_deploy).with(anything, anything, env: {}, force: true, run_now: true)
281+
282+
Stack.run_deploy_in_foreground(stack: stack.to_param, revision: commit.sha)
283+
end
284+
266285
test "#active_task? is memoized" do
267286
assert_queries(1) do
268287
10.times { @stack.active_task? }

0 commit comments

Comments
 (0)