Commit 7fde7012 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Make it possible to auto retry a failed CI/CD job

parent 9bb7f19d
......@@ -96,6 +96,16 @@ module Ci
BuildSuccessWorker.perform_async(id)
end
end
after_transition any => [:failed] do |build|
build.run_after_commit do
next if build.retries_max.zero?
if build.retries_count < build.retries_max
Ci::Build.retry(build, build.user)
end
end
end
end
def detailed_status(current_user)
......
......@@ -1624,7 +1624,7 @@ describe Ci::Build, :models do
end
end
describe 'State transition: any => [:pending]' do
describe 'state transition: any => [:pending]' do
let(:build) { create(:ci_build, :created) }
it 'queues BuildQueueWorker' do
......@@ -1633,4 +1633,35 @@ describe Ci::Build, :models do
build.enqueue
end
end
describe 'state transition when build fails' do
context 'when build is configured to be retried' do
subject { create(:ci_build, :running, options: { retry: 3 }) }
it 'retries builds and assigns a same user to it' do
expect(described_class).to receive(:retry)
.with(subject, subject.user)
subject.drop!
end
end
context 'when build is not configured to be retried' do
subject { create(:ci_build, :running) }
it 'does not retry build' do
expect(described_class).not_to receive(:retry)
subject.drop!
end
it 'does not count retries when not necessary' do
expect(described_class).not_to receive(:retry)
expect_any_instance_of(described_class)
.not_to receive(:retries_count)
subject.drop!
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