Skip to content

Commit

Permalink
Merge pull request #1334 from Shopify/update-deploy-api
Browse files Browse the repository at this point in the history
Update `build_deploy` with `allow_concurrency` keyword argument
  • Loading branch information
kartiki975 authored Mar 20, 2024
2 parents 0834c0b + c2c77ea commit 261b3ad
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Migrate from legacy Rails secrets to credentials (#1326)
* Rails secrets were [deprecated in Rails 7.1](https://github.com/rails/rails/pull/48472)
* [Guide on credentials](https://guides.rubyonrails.org/security.html#custom-credentials)
* 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


# 0.39.0

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ For example:
fetch:
curl --silent https://app.example.com/services/ping/version
```

**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),
whose `allow_concurrency` keyword argument defaults to `force`, where `force` is true when emergency mode is enabled.
If you'd like to separate these two from one another, override this method as desired in your service.

<h3 id="kubernetes">Kubernetes</h3>

**<code>kubernetes</code>** allows to specify a Kubernetes namespace and context to deploy to.
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/shipit/api/deploys_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ def index
params do
requires :sha, String, length: { in: 6..40 }
accepts :force, Boolean, default: false
accepts :allow_concurrency, Boolean
accepts :require_ci, Boolean, default: false
accepts :env, Hash, default: {}
end
def create
commit = stack.commits.by_sha(params.sha) || param_error!(:sha, 'Unknown revision')
param_error!(:force, "Can't deploy a locked stack") if !params.force && stack.locked?
param_error!(:require_ci, "Commit is not deployable") if params.require_ci && !commit.deployable?
deploy = stack.trigger_deploy(commit, current_user, env: params.env, force: params.force)

allow_concurrency = params.allow_concurrency.nil? ? params.force : params.allow_concurrency
deploy = stack.trigger_deploy(commit, current_user, env: params.env, force: params.force,
allow_concurrency: allow_concurrency)
render_resource(deploy, status: :accepted)
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/shipit/stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ def trigger_task(definition_id, user, env: nil, force: false)
task
end

def build_deploy(until_commit, user, env: nil, force: false)
def build_deploy(until_commit, user, env: nil, force: false, allow_concurrency: force)
since_commit = last_deployed_commit.presence || commits.first
deploys.build(
user_id: user.id,
until_commit: until_commit,
since_commit: since_commit,
env: filter_deploy_envs(env&.to_h || {}),
allow_concurrency: force,
allow_concurrency: allow_concurrency,
ignored_safeties: force || !until_commit.deployable?,
max_retries: retries_on_deploy,
)
Expand Down
23 changes: 23 additions & 0 deletions test/controllers/api/deploys_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,29 @@ class DeploysControllerTest < ApiControllerTestCase
assert_response :accepted
assert_json 'status', 'pending'
end

test "#create uses allow_concurrency param when provided" do
@stack.update!(lock_reason: 'Something broken')

assert_difference -> { @stack.deploys.count }, 1 do
post :create, params: { stack_id: @stack.to_param, sha: @commit.sha, force: 'true', allow_concurrency: 'false' }
end
assert_response :accepted
assert_json 'status', 'pending'
refute @stack.deploys.last.allow_concurrency
end

test "#create defaults allow_concurrency to force param when not provided" do
@stack.update!(lock_reason: 'Something broken')
expected_force = true

assert_difference -> { @stack.deploys.count }, 1 do
post :create, params: { stack_id: @stack.to_param, sha: @commit.sha, force: expected_force }
end
assert_response :accepted
assert_json 'status', 'pending'
assert_equal expected_force, @stack.deploys.last.allow_concurrency
end
end
end
end

0 comments on commit 261b3ad

Please sign in to comment.