2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
module Ci
|
|
|
|
class CreatePipelineService < BaseService
|
2016-09-13 17:45:13 +05:30
|
|
|
attr_reader :pipeline
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
CreateError = Class.new(StandardError)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
SEQUENCE = [Gitlab::Ci::Pipeline::Chain::Build,
|
2019-03-02 22:35:43 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::RemoveUnwantedChatJobs,
|
2018-03-17 18:26:18 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::Validate::Abilities,
|
|
|
|
Gitlab::Ci::Pipeline::Chain::Validate::Repository,
|
|
|
|
Gitlab::Ci::Pipeline::Chain::Validate::Config,
|
|
|
|
Gitlab::Ci::Pipeline::Chain::Skip,
|
2019-03-02 22:35:43 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::Limit::Size,
|
2018-05-09 12:01:36 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::Populate,
|
2019-03-02 22:35:43 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::Create,
|
|
|
|
Gitlab::Ci::Pipeline::Chain::Limit::Activity].freeze
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
def execute(source, ignore_skip_ci: false, save_on_errors: true, trigger_request: nil, schedule: nil, merge_request: nil, **options, &block)
|
2018-03-17 18:26:18 +05:30
|
|
|
@pipeline = Ci::Pipeline.new
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
command = Gitlab::Ci::Pipeline::Chain::Command.new(
|
|
|
|
source: source,
|
|
|
|
origin_ref: params[:ref],
|
|
|
|
checkout_sha: params[:checkout_sha],
|
|
|
|
after_sha: params[:after],
|
2019-07-07 11:18:12 +05:30
|
|
|
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.
|
2018-03-17 18:26:18 +05:30
|
|
|
trigger_request: trigger_request,
|
|
|
|
schedule: schedule,
|
2019-02-15 15:39:39 +05:30
|
|
|
merge_request: merge_request,
|
2018-03-17 18:26:18 +05:30
|
|
|
ignore_skip_ci: ignore_skip_ci,
|
|
|
|
save_incompleted: save_on_errors,
|
|
|
|
seeds_block: block,
|
2018-10-15 14:42:47 +05:30
|
|
|
variables_attributes: params[:variables_attributes],
|
2018-03-17 18:26:18 +05:30
|
|
|
project: project,
|
2019-02-15 15:39:39 +05:30
|
|
|
current_user: current_user,
|
2019-07-07 11:18:12 +05:30
|
|
|
push_options: params[:push_options] || {},
|
|
|
|
chat_data: params[:chat_data],
|
2019-03-02 22:35:43 +05:30
|
|
|
**extra_options(options))
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
sequence = Gitlab::Ci::Pipeline::Chain::Sequence
|
|
|
|
.new(pipeline, command, SEQUENCE)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
sequence.build! do |pipeline, sequence|
|
|
|
|
schedule_head_pipeline_update
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
if sequence.complete?
|
|
|
|
cancel_pending_pipelines if project.auto_cancel_pending_pipelines?
|
|
|
|
pipeline_created_counter.increment(source: source)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
pipeline.process!
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
# 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)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
pipeline
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def execute!(*args, &block)
|
|
|
|
execute(*args, &block).tap do |pipeline|
|
|
|
|
unless pipeline.persisted?
|
|
|
|
raise CreateError, pipeline.errors.full_messages.join(',')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
private
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def commit
|
|
|
|
@commit ||= project.commit(origin_sha || origin_ref)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def sha
|
|
|
|
commit.try(:id)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def cancel_pending_pipelines
|
|
|
|
Gitlab::OptimisticLocking.retry_lock(auto_cancelable_pipelines) do |cancelables|
|
|
|
|
cancelables.find_each do |cancelable|
|
|
|
|
cancelable.auto_cancel_running(pipeline)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
def auto_cancelable_pipelines
|
2019-02-15 15:39:39 +05:30
|
|
|
project.ci_pipelines
|
2017-08-17 22:00:37 +05:30
|
|
|
.where(ref: pipeline.ref)
|
|
|
|
.where.not(id: pipeline.id)
|
2018-05-09 12:01:36 +05:30
|
|
|
.where.not(sha: project.commit(pipeline.ref).try(:id))
|
2017-08-17 22:00:37 +05:30
|
|
|
.created_or_pending
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def pipeline_created_counter
|
|
|
|
@pipeline_created_counter ||= Gitlab::Metrics
|
|
|
|
.counter(:pipelines_created_total, "Counter of pipelines created")
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def schedule_head_pipeline_update
|
2019-07-31 22:56:46 +05:30
|
|
|
pipeline.all_merge_requests.opened.each do |merge_request|
|
2018-03-17 18:26:18 +05:30
|
|
|
UpdateHeadPipelineForMergeRequestWorker.perform_async(merge_request.id)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
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-07-07 11:18:12 +05:30
|
|
|
# checking explicitly that no arguments are given.
|
2019-03-02 22:35:43 +05:30
|
|
|
raise ArgumentError if options.any?
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
{} # overridden in EE
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|