Skip to content

Commit

Permalink
[GOG-150] Adding pre-condition check for uncached deploy spec (#26)
Browse files Browse the repository at this point in the history
* Adding pre-condition check for uncached deploy spec

* Added commit onto DeploySpec stack

* Added DeploySpec repo

* Added check_deploy_spec stack

* Adding test to make sure trigger_continuous_delivery returns when cached_deploy_spec config is empty

* removing whitespace

* Removed multiline for spec config. Added newline at eof

* Removing deletion of canary tasks from deploy spec test

* Breaking down trigger_continuous_delivery fn. Removed duplicated env declaration

* can_trigger_continuous_delivery

* Further refactored trigger_continuous_delivery

* Making delivery condition fns private
  • Loading branch information
ryanmrodriguez committed Apr 14, 2020
1 parent d3e26d1 commit 8c6ad04
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 21 deletions.
37 changes: 16 additions & 21 deletions app/models/shipit/stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,6 @@ def env
}
end

def env
{
'ENVIRONMENT' => environment,
'LAST_DEPLOYED_SHA' => last_deployed_commit.sha,
'GITHUB_REPO_OWNER' => repository.owner,
'GITHUB_REPO_NAME' => repository.name,
'DEPLOY_URL' => deploy_url,
'BRANCH' => branch,
}
end

def repository
super || build_repository
end
Expand Down Expand Up @@ -182,16 +171,8 @@ def continuous_delivery_delayed!
def trigger_continuous_delivery
commit = next_commit_to_deploy

if !deployable? || deployed_too_recently? || commit.nil? || commit.deployed?
continuous_delivery_resumed!
return
end

if commit.deploy_failed? || (checks? && !EphemeralCommitChecks.new(commit).run.success?) ||
commit.recently_pushed?
continuous_delivery_delayed!
return
end
return continuous_delivery_resumed! if should_resume_continuous_delivery?(commit)
return continuous_delivery_delayed! if should_delay_continuous_delivery?(commit)

begin
trigger_deploy(commit, Shipit.user, env: cached_deploy_spec.default_deploy_env)
Expand Down Expand Up @@ -626,5 +607,19 @@ def emit_merge_status_hooks
def ci_enabled_cache_key
"stacks:#{id}:ci_enabled"
end

def should_delay_continuous_delivery?(commit)
commit.deploy_failed? ||
(checks? && !EphemeralCommitChecks.new(commit).run.success?) ||
commit.recently_pushed? ||
cached_deploy_spec.config.empty?
end

def should_resume_continuous_delivery?(commit)
!deployable? ||
deployed_too_recently? ||
commit.nil? ||
commit.deployed?
end
end
end
14 changes: 14 additions & 0 deletions test/fixtures/shipit/commits.yml
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,17 @@ task_no_commits:
additions: 420
deletions: 342
updated_at: <%= 8.days.ago.to_s(:db) %>

check_deploy_spec_first:
id: 701
sha: 547578b362bf2b4df5903e1c7960929361c3435a
message: "Removing deploy spec"
stack: check_deploy_spec
author: walrus
committer: walrus
authored_at: <%= 4.days.ago.to_s(:db) %>
committed_at: <%= 3.days.ago.to_s(:db) %>
additions: 420
deletions: 342
updated_at: <%= 8.days.ago.to_s(:db) %>

4 changes: 4 additions & 0 deletions test/fixtures/shipit/repositories.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ check_runs:
owner: shopify
name: check_runs

check_deploy_spec:
owner: shopify
name: check_deploy_spec

rails:
owner: rails
name: rails
13 changes: 13 additions & 0 deletions test/fixtures/shipit/stacks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,16 @@ shipit_task_no_commits:
}
}
updated_at: <%= 8.days.ago.to_s(:db) %>

check_deploy_spec:
repository: check_deploy_spec
environment: production
branch: master
ignore_ci: true
merge_queue_enabled: true
tasks_count: 0
undeployed_commits_count: 1
continuous_deployment: true
cached_deploy_spec: "{}"
last_deployed_at: <%= 8.days.ago.to_s(:db) %>
updated_at: <%= 8.days.ago.to_s(:db) %>
15 changes: 15 additions & 0 deletions test/models/stacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,21 @@ def self.deliver(event, stack, payload)
end
end

test "#trigger_continuous_delivery bails out if no DeploySpec has been cached" do
@stack = shipit_stacks(:check_deploy_spec)
config = @stack.cached_deploy_spec.config

assert_predicate @stack, :deployable?
refute_predicate @stack, :deployed_too_recently?
assert_empty(config, "DeploySpec was not empty")

assert_no_enqueued_jobs(only: Shipit::PerformTaskJob) do
assert_no_difference -> { Deploy.count } do
@stack.trigger_continuous_delivery
end
end
end

test "#trigger_continuous_delivery enqueues deployment ref update job" do
@stack = shipit_stacks(:shipit_canaries)
shipit_tasks(:canaries_running).delete
Expand Down

0 comments on commit 8c6ad04

Please sign in to comment.