debian-mirror-gitlab/app/services/ci/create_pipeline_service.rb

145 lines
5.2 KiB
Ruby
Raw Normal View History

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,
Gitlab::Ci::Pipeline::Chain::Validate::Abilities,
Gitlab::Ci::Pipeline::Chain::Validate::Repository,
2019-12-26 22:10:19 +05:30
Gitlab::Ci::Pipeline::Chain::Config::Content,
Gitlab::Ci::Pipeline::Chain::Config::Process,
Gitlab::Ci::Pipeline::Chain::RemoveUnwantedChatJobs,
2018-03-17 18:26:18 +05:30
Gitlab::Ci::Pipeline::Chain::Skip,
2019-12-26 22:10:19 +05:30
Gitlab::Ci::Pipeline::Chain::EvaluateWorkflowRules,
Gitlab::Ci::Pipeline::Chain::Seed,
2019-03-02 22:35:43 +05:30
Gitlab::Ci::Pipeline::Chain::Limit::Size,
2020-01-01 13:55:28 +05:30
Gitlab::Ci::Pipeline::Chain::Validate::External,
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,
2019-09-04 21:01:54 +05:30
Gitlab::Ci::Pipeline::Chain::Limit::Activity,
Gitlab::Ci::Pipeline::Chain::Limit::JobActivity].freeze
2017-09-10 17:25:29 +05:30
2019-12-04 20:38:33 +05:30
# rubocop: disable Metrics/ParameterLists
2020-03-13 15:44:24 +05:30
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)
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,
2019-12-04 20:38:33 +05:30
external_pull_request: external_pull_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],
2020-03-13 15:44:24 +05:30
bridge: bridge,
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
2020-01-01 13:55:28 +05:30
Ci::ProcessPipelineService
.new(pipeline)
2020-03-13 15:44:24 +05:30
.execute(nil, initial_process: true)
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
2019-12-04 20:38:33 +05:30
# rubocop: enable Metrics/ParameterLists
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?
2019-09-30 21:07:59 +05:30
raise CreateError, pipeline.error_messages
2019-02-15 15:39:39 +05:30
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-12-04 20:38:33 +05:30
# TODO: Introduced by https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/23464
if Feature.enabled?(:ci_support_interruptible_pipelines, project, default_enabled: true)
project.ci_pipelines
.where(ref: pipeline.ref)
2020-03-13 15:44:24 +05:30
.where.not(id: pipeline.same_family_pipeline_ids)
2019-12-04 20:38:33 +05:30
.where.not(sha: project.commit(pipeline.ref).try(:id))
.alive_or_scheduled
.with_only_interruptible_builds
else
project.ci_pipelines
.where(ref: pipeline.ref)
2020-03-13 15:44:24 +05:30
.where.not(id: pipeline.same_family_pipeline_ids)
2019-12-04 20:38:33 +05:30
.where.not(sha: project.commit(pipeline.ref).try(:id))
.created_or_pending
end
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
2019-12-04 20:38:33 +05:30
Ci::CreatePipelineService.prepend_if_ee('EE::Ci::CreatePipelineService')