2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-09 19:26:13 -04:00
|
|
|
module Ci
|
|
|
|
class CreatePipelineService < BaseService
|
2016-08-11 09:22:35 -04:00
|
|
|
attr_reader :pipeline
|
2016-05-18 14:02:10 -04:00
|
|
|
|
2018-11-30 02:32:30 -05:00
|
|
|
CreateError = Class.new(StandardError)
|
|
|
|
|
2017-10-09 07:07:55 -04:00
|
|
|
SEQUENCE = [Gitlab::Ci::Pipeline::Chain::Build,
|
2019-01-31 12:39:19 -05:00
|
|
|
Gitlab::Ci::Pipeline::Chain::RemoveUnwantedChatJobs,
|
2017-10-09 07:07:55 -04:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Validate::Abilities,
|
2017-09-26 05:53:50 -04:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Validate::Repository,
|
|
|
|
Gitlab::Ci::Pipeline::Chain::Validate::Config,
|
2017-09-26 03:54:56 -04:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Skip,
|
2019-01-31 12:39:19 -05:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Limit::Size,
|
2018-03-21 05:59:11 -04:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Populate,
|
2019-01-31 12:39:19 -05:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Create,
|
|
|
|
Gitlab::Ci::Pipeline::Chain::Limit::Activity].freeze
|
2017-09-25 10:22:00 -04:00
|
|
|
|
2019-01-31 12:39:19 -05:00
|
|
|
def execute(source, ignore_skip_ci: false, save_on_errors: true, trigger_request: nil, schedule: nil, merge_request: nil, **options, &block)
|
2017-10-09 07:07:55 -04:00
|
|
|
@pipeline = Ci::Pipeline.new
|
|
|
|
|
2017-12-08 05:35:36 -05:00
|
|
|
command = Gitlab::Ci::Pipeline::Chain::Command.new(
|
|
|
|
source: source,
|
|
|
|
origin_ref: params[:ref],
|
|
|
|
checkout_sha: params[:checkout_sha],
|
|
|
|
after_sha: params[:after],
|
2019-02-20 01:37:49 -05:00
|
|
|
before_sha: params[:before], # The base SHA of the source branch (i.e merge_request.diff_base_sha).
|
|
|
|
source_sha: params[:source_sha], # The HEAD SHA of the source branch (i.e merge_request.diff_head_sha).
|
|
|
|
target_sha: params[:target_sha], # The HEAD SHA of the target branch.
|
2017-12-08 05:35:36 -05:00
|
|
|
trigger_request: trigger_request,
|
|
|
|
schedule: schedule,
|
2018-12-05 01:57:00 -05:00
|
|
|
merge_request: merge_request,
|
2017-12-08 05:35:36 -05:00
|
|
|
ignore_skip_ci: ignore_skip_ci,
|
|
|
|
save_incompleted: save_on_errors,
|
|
|
|
seeds_block: block,
|
2018-04-18 13:28:34 -04:00
|
|
|
variables_attributes: params[:variables_attributes],
|
2017-12-08 05:35:36 -05:00
|
|
|
project: project,
|
2017-11-28 22:50:29 -05:00
|
|
|
current_user: current_user,
|
2019-04-05 00:19:30 -04:00
|
|
|
push_options: params[:push_options] || {},
|
2019-02-20 16:29:48 -05:00
|
|
|
chat_data: params[:chat_data],
|
2019-02-12 11:46:13 -05:00
|
|
|
**extra_options(options))
|
2017-09-25 10:22:00 -04:00
|
|
|
|
2017-09-26 05:13:40 -04:00
|
|
|
sequence = Gitlab::Ci::Pipeline::Chain::Sequence
|
|
|
|
.new(pipeline, command, SEQUENCE)
|
2017-07-27 11:33:41 -04:00
|
|
|
|
2017-09-26 05:13:40 -04:00
|
|
|
sequence.build! do |pipeline, sequence|
|
2017-09-07 03:58:15 -04:00
|
|
|
schedule_head_pipeline_update
|
2017-07-21 05:49:32 -04:00
|
|
|
|
2017-09-26 05:13:40 -04:00
|
|
|
if sequence.complete?
|
2017-09-26 03:54:56 -04:00
|
|
|
cancel_pending_pipelines if project.auto_cancel_pending_pipelines?
|
|
|
|
pipeline_created_counter.increment(source: source)
|
2017-07-26 05:31:09 -04:00
|
|
|
|
2017-09-26 03:54:56 -04:00
|
|
|
pipeline.process!
|
2017-07-26 05:31:09 -04:00
|
|
|
end
|
2017-07-21 05:49:32 -04:00
|
|
|
end
|
2017-09-07 03:58:15 -04:00
|
|
|
|
2019-03-22 11:51:15 -04:00
|
|
|
# If pipeline is not persisted, try to recover IID
|
|
|
|
pipeline.reset_project_iid unless pipeline.persisted? ||
|
|
|
|
Feature.disabled?(:ci_pipeline_rewind_iid, project, default_enabled: true)
|
|
|
|
|
2017-09-07 03:58:15 -04:00
|
|
|
pipeline
|
2017-07-21 05:49:32 -04:00
|
|
|
end
|
|
|
|
|
2018-11-30 02:32:30 -05:00
|
|
|
def execute!(*args, &block)
|
|
|
|
execute(*args, &block).tap do |pipeline|
|
|
|
|
unless pipeline.persisted?
|
|
|
|
raise CreateError, pipeline.errors.full_messages.join(',')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-21 05:49:32 -04:00
|
|
|
private
|
|
|
|
|
2017-09-07 03:58:15 -04:00
|
|
|
def commit
|
|
|
|
@commit ||= project.commit(origin_sha || origin_ref)
|
|
|
|
end
|
2017-05-25 17:14:40 -04:00
|
|
|
|
2017-09-07 03:58:15 -04:00
|
|
|
def sha
|
|
|
|
commit.try(:id)
|
2017-05-25 17:14:40 -04:00
|
|
|
end
|
|
|
|
|
2017-02-19 17:17:24 -05:00
|
|
|
def cancel_pending_pipelines
|
2017-03-18 06:04:14 -04:00
|
|
|
Gitlab::OptimisticLocking.retry_lock(auto_cancelable_pipelines) do |cancelables|
|
2017-02-19 17:17:24 -05:00
|
|
|
cancelables.find_each do |cancelable|
|
2017-04-06 09:32:56 -04:00
|
|
|
cancelable.auto_cancel_running(pipeline)
|
2017-02-19 17:17:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-03-18 06:04:14 -04:00
|
|
|
def auto_cancelable_pipelines
|
2018-12-05 09:39:15 -05:00
|
|
|
project.ci_pipelines
|
2017-03-18 06:04:14 -04:00
|
|
|
.where(ref: pipeline.ref)
|
|
|
|
.where.not(id: pipeline.id)
|
2018-03-21 12:08:56 -04:00
|
|
|
.where.not(sha: project.commit(pipeline.ref).try(:id))
|
2017-03-18 06:04:14 -04:00
|
|
|
.created_or_pending
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-03-18 06:04:14 -04:00
|
|
|
|
2017-06-07 11:12:23 -04:00
|
|
|
def pipeline_created_counter
|
2017-09-25 10:59:05 -04:00
|
|
|
@pipeline_created_counter ||= Gitlab::Metrics
|
|
|
|
.counter(:pipelines_created_total, "Counter of pipelines created")
|
2017-06-07 11:12:23 -04:00
|
|
|
end
|
2017-09-07 03:58:15 -04:00
|
|
|
|
|
|
|
def schedule_head_pipeline_update
|
2019-05-03 05:18:06 -04:00
|
|
|
pipeline.all_merge_requests.opened.each do |merge_request|
|
2017-09-07 03:58:15 -04:00
|
|
|
UpdateHeadPipelineForMergeRequestWorker.perform_async(merge_request.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-12 11:46:13 -05:00
|
|
|
def extra_options(options = {})
|
|
|
|
# In Ruby 2.4, even when options is empty, f(**options) doesn't work when f
|
|
|
|
# doesn't have any parameters. We reproduce the Ruby 2.5 behavior by
|
2019-02-25 05:42:31 -05:00
|
|
|
# checking explicitly that no arguments are given.
|
2019-02-12 11:46:13 -05:00
|
|
|
raise ArgumentError if options.any?
|
|
|
|
|
2019-02-25 05:42:31 -05:00
|
|
|
{} # overridden in EE
|
2019-01-31 12:39:19 -05:00
|
|
|
end
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|
2016-05-12 14:08:18 -04:00
|
|
|
end
|