Commit 204fdcb1 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add build success worker that runs asynchronously

parent fafc5a17
......@@ -76,25 +76,22 @@ module Ci
state_machine :status do
after_transition pending: :running do |build|
build.run_after_commit { BuildHooksWorker.perform_async(id) }
build.run_after_commit do
BuildHooksWorker.perform_async(id)
end
end
after_transition any => [:success, :failed, :canceled] do |build|
build.update_coverage
build.run_after_commit { BuildHooksWorker.perform_async(id) }
build.run_after_commit do
BuildHooksWorker.perform_async(id)
end
end
after_transition any => [:success] do |build|
if build.environment.present?
service = CreateDeploymentService.new(
build.project, build.user,
environment: build.environment,
sha: build.sha,
ref: build.ref,
tag: build.tag,
options: build.options.to_h[:environment],
variables: build.variables)
service.execute(build)
build.run_after_commit do
BuildSuccessWorker.perform_async(id)
end
end
end
......
class BuildSuccessWorker
include Sidekiq::Worker
sidekiq_options queue: :default
def perform(build_id)
Ci::Build.find_by(id: build_id).try do |build|
perform_deloyment(build)
end
end
private
def perform_deloyment(build)
return if build.environment.blank?
service = CreateDeploymentService.new(
build.project, build.user,
environment: build.environment,
sha: build.sha,
ref: build.ref,
tag: build.tag,
options: build.options.to_h[:environment],
variables: build.variables)
service.execute(build)
end
end
require 'spec_helper'
describe BuildSuccessWorker do
describe '#perform' do
context 'when build exists' do
context 'when build belogs to the environment' do
let!(:build) { create(:ci_build, environment: 'production') }
it 'executes deployment service' do
expect_any_instance_of(CreateDeploymentService)
.to receive(:execute)
described_class.new.perform(build.id)
end
end
end
context 'when build does not exist' do
it 'does not raise exception' do
expect { described_class.new.perform(123) }
.not_to raise_error
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment