Skip to content

Commit

Permalink
Merge pull request #31 from Taucher2003/30-retries-for-trigger-pipeli…
Browse files Browse the repository at this point in the history
…ne-step

Enable retries for TriggerPipeline step
  • Loading branch information
Taucher2003 authored Dec 12, 2023
2 parents 966602d + c4f8794 commit b6931b6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/action/step/trigger_pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
module GitlabPipelineAction
module Step
class TriggerPipeline < Base
TriggerFailed = Class.new(StandardError)
MAX_TRIGGER_RETRIES = 5

def execute
context.gl_pipeline = context.gitlab_client.create_pipeline(
context.gl_project_id,
context.gl_branch_name,
context.gl_pipeline_variables
)
MAX_TRIGGER_RETRIES.times do |iteration|
context.gl_pipeline = context.gitlab_client.create_pipeline(
context.gl_project_id,
context.gl_branch_name,
context.gl_pipeline_variables
)
puts "Triggered: #{context.gl_pipeline.web_url}"
return # rubocop:disable Lint/NonLocalExitFromIterator -- this is intended
rescue Gitlab::Error::BadRequest
puts "Trigger failed, #{MAX_TRIGGER_RETRIES - iteration - 1} retries remaining"
sleep 1
end

puts "Triggered: #{context.gl_pipeline.web_url}"
raise TriggerFailed, "Failed to trigger pipeline in GitLab #{MAX_TRIGGER_RETRIES} times"
end
end
end
Expand Down
64 changes: 64 additions & 0 deletions spec/action/step/trigger_pipeline_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe GitlabPipelineAction::Step::TriggerPipeline do
subject(:trigger) { instance.execute }

let(:context) { GitlabPipelineAction::Context.new }
let(:instance) { described_class.new(context) }
let(:gitlab_client) { instance_double(Gitlab::Client) }

context 'when triggering fails' do
let(:failing_retries) { 4 }

before do
fake_response = instance_double(HTTParty::Response)
fake_request = instance_double(HTTParty::Request)
allow(fake_response).to receive_messages(headers: { 'content-type' => 'text/plain' }, to_s: '', code: 400,
request: fake_request, body: "'base' Reference not found")

allow(fake_request).to receive_messages(base_uri: '', path: '')

allow(context).to receive_messages(gl_project_id: 1, gl_branch_name: 'glpa/main', gl_pipeline_variables: {},
gitlab_client: gitlab_client)

fake_pipeline = double
allow(fake_pipeline).to receive(:web_url).and_return('')

iteration = 0
allow(gitlab_client).to receive(:create_pipeline) do
iteration += 1
raise Gitlab::Error::BadRequest, fake_response if iteration <= failing_retries
end.and_return(fake_pipeline)
end

it 'retries multiple times', :disable_console do
trigger

expect(gitlab_client).to have_received(:create_pipeline).exactly(5).times
end

it 'logs failures' do
expect { trigger }.to output(
include(
'Trigger failed, 4 retries remaining',
'Trigger failed, 3 retries remaining',
'Trigger failed, 2 retries remaining',
'Trigger failed, 1 retries remaining'
)
).to_stdout
end

context 'when retries are exhausted' do
let(:failing_retries) { 5 }

it 'raises an error', :disable_console do
expect do
trigger
end.to raise_error(GitlabPipelineAction::Step::TriggerPipeline::TriggerFailed,
'Failed to trigger pipeline in GitLab 5 times')
end
end
end
end

0 comments on commit b6931b6

Please sign in to comment.