Skip to content

Commit c2c77ea

Browse files
committed
Stack: update build_deploy to pass allow_concurrency
Add unit tests
1 parent 0834c0b commit c2c77ea

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Migrate from legacy Rails secrets to credentials (#1326)
44
* Rails secrets were [deprecated in Rails 7.1](https://github.com/rails/rails/pull/48472)
55
* [Guide on credentials](https://guides.rubyonrails.org/security.html#custom-credentials)
6+
* For deployments, `allow_concurrency` defaults to the same value as `force`. If wanted, it can be set separately by passing the intended value for `allow_concurrency` to `build_deploy` method
7+
68

79
# 0.39.0
810

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ For example:
357357
fetch:
358358
curl --silent https://app.example.com/services/ping/version
359359
```
360+
361+
**Note:** Currently, deployments in emergency mode are configured to occur concurrently via [the `build_deploy` method](https://github.com/Shopify/shipit-engine/blob/main/app/models/shipit/stack.rb),
362+
whose `allow_concurrency` keyword argument defaults to `force`, where `force` is true when emergency mode is enabled.
363+
If you'd like to separate these two from one another, override this method as desired in your service.
364+
360365
<h3 id="kubernetes">Kubernetes</h3>
361366

362367
**<code>kubernetes</code>** allows to specify a Kubernetes namespace and context to deploy to.

app/controllers/shipit/api/deploys_controller.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ def index
1111
params do
1212
requires :sha, String, length: { in: 6..40 }
1313
accepts :force, Boolean, default: false
14+
accepts :allow_concurrency, Boolean
1415
accepts :require_ci, Boolean, default: false
1516
accepts :env, Hash, default: {}
1617
end
1718
def create
1819
commit = stack.commits.by_sha(params.sha) || param_error!(:sha, 'Unknown revision')
1920
param_error!(:force, "Can't deploy a locked stack") if !params.force && stack.locked?
2021
param_error!(:require_ci, "Commit is not deployable") if params.require_ci && !commit.deployable?
21-
deploy = stack.trigger_deploy(commit, current_user, env: params.env, force: params.force)
22+
23+
allow_concurrency = params.allow_concurrency.nil? ? params.force : params.allow_concurrency
24+
deploy = stack.trigger_deploy(commit, current_user, env: params.env, force: params.force,
25+
allow_concurrency: allow_concurrency)
2226
render_resource(deploy, status: :accepted)
2327
end
2428
end

app/models/shipit/stack.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,14 @@ def trigger_task(definition_id, user, env: nil, force: false)
150150
task
151151
end
152152

153-
def build_deploy(until_commit, user, env: nil, force: false)
153+
def build_deploy(until_commit, user, env: nil, force: false, allow_concurrency: force)
154154
since_commit = last_deployed_commit.presence || commits.first
155155
deploys.build(
156156
user_id: user.id,
157157
until_commit: until_commit,
158158
since_commit: since_commit,
159159
env: filter_deploy_envs(env&.to_h || {}),
160-
allow_concurrency: force,
160+
allow_concurrency: allow_concurrency,
161161
ignored_safeties: force || !until_commit.deployable?,
162162
max_retries: retries_on_deploy,
163163
)

test/controllers/api/deploys_controller_test.rb

+23
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,29 @@ class DeploysControllerTest < ApiControllerTestCase
120120
assert_response :accepted
121121
assert_json 'status', 'pending'
122122
end
123+
124+
test "#create uses allow_concurrency param when provided" do
125+
@stack.update!(lock_reason: 'Something broken')
126+
127+
assert_difference -> { @stack.deploys.count }, 1 do
128+
post :create, params: { stack_id: @stack.to_param, sha: @commit.sha, force: 'true', allow_concurrency: 'false' }
129+
end
130+
assert_response :accepted
131+
assert_json 'status', 'pending'
132+
refute @stack.deploys.last.allow_concurrency
133+
end
134+
135+
test "#create defaults allow_concurrency to force param when not provided" do
136+
@stack.update!(lock_reason: 'Something broken')
137+
expected_force = true
138+
139+
assert_difference -> { @stack.deploys.count }, 1 do
140+
post :create, params: { stack_id: @stack.to_param, sha: @commit.sha, force: expected_force }
141+
end
142+
assert_response :accepted
143+
assert_json 'status', 'pending'
144+
assert_equal expected_force, @stack.deploys.last.allow_concurrency
145+
end
123146
end
124147
end
125148
end

0 commit comments

Comments
 (0)