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,
|
2020-03-02 10:08:01 -05:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Build::Associations,
|
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,
|
2019-11-18 13:06:53 -05:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Config::Content,
|
|
|
|
Gitlab::Ci::Pipeline::Chain::Config::Process,
|
|
|
|
Gitlab::Ci::Pipeline::Chain::RemoveUnwantedChatJobs,
|
2017-09-26 03:54:56 -04:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Skip,
|
2019-11-17 16:06:14 -05:00
|
|
|
Gitlab::Ci::Pipeline::Chain::EvaluateWorkflowRules,
|
2019-11-18 13:06:53 -05:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Seed,
|
2019-01-31 12:39:19 -05:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Limit::Size,
|
2019-12-14 04:07:51 -05:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Validate::External,
|
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,
|
2019-08-19 10:29:53 -04:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Limit::Activity,
|
|
|
|
Gitlab::Ci::Pipeline::Chain::Limit::JobActivity].freeze
|
2017-09-25 10:22:00 -04:00
|
|
|
|
2019-08-09 05:40:45 -04:00
|
|
|
# rubocop: disable Metrics/ParameterLists
|
2020-01-13 07:08:04 -05:00
|
|
|
def execute(source, ignore_skip_ci: false, save_on_errors: true, trigger_request: nil, schedule: nil, merge_request: nil, external_pull_request: nil, bridge: 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,
|
2019-08-09 05:40:45 -04:00
|
|
|
external_pull_request: external_pull_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],
|
2020-01-13 07:08:04 -05:00
|
|
|
bridge: bridge,
|
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
|
|
|
|
2019-11-19 07:06:00 -05:00
|
|
|
Ci::ProcessPipelineService
|
|
|
|
.new(pipeline)
|
2020-02-14 07:09:03 -05:00
|
|
|
.execute(nil, initial_process: true)
|
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
|
2019-08-09 05:40:45 -04:00
|
|
|
# rubocop: enable Metrics/ParameterLists
|
2017-07-21 05:49:32 -04:00
|
|
|
|
2018-11-30 02:32:30 -05:00
|
|
|
def execute!(*args, &block)
|
|
|
|
execute(*args, &block).tap do |pipeline|
|
|
|
|
unless pipeline.persisted?
|
2019-06-26 08:29:35 -04:00
|
|
|
raise CreateError, pipeline.error_messages
|
2018-11-30 02:32:30 -05:00
|
|
|
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
|
2019-09-18 10:02:45 -04:00
|
|
|
# TODO: Introduced by https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/23464
|
2019-09-17 10:16:34 -04:00
|
|
|
if Feature.enabled?(:ci_support_interruptible_pipelines, project, default_enabled: true)
|
2019-09-05 10:50:39 -04:00
|
|
|
project.ci_pipelines
|
|
|
|
.where(ref: pipeline.ref)
|
2020-01-13 07:08:04 -05:00
|
|
|
.where.not(id: pipeline.same_family_pipeline_ids)
|
2019-09-05 10:50:39 -04:00
|
|
|
.where.not(sha: project.commit(pipeline.ref).try(:id))
|
|
|
|
.alive_or_scheduled
|
2019-09-11 18:09:42 -04:00
|
|
|
.with_only_interruptible_builds
|
2019-09-05 10:50:39 -04:00
|
|
|
else
|
|
|
|
project.ci_pipelines
|
|
|
|
.where(ref: pipeline.ref)
|
2020-01-13 07:08:04 -05:00
|
|
|
.where.not(id: pipeline.same_family_pipeline_ids)
|
2019-09-05 10:50:39 -04:00
|
|
|
.where.not(sha: project.commit(pipeline.ref).try(:id))
|
|
|
|
.created_or_pending
|
|
|
|
end
|
2017-03-18 06:04:14 -04:00
|
|
|
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
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Ci::CreatePipelineService.prepend_if_ee('EE::Ci::CreatePipelineService')
|