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

150 lines
6.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,
2020-04-08 14:13:33 +05:30
Gitlab::Ci::Pipeline::Chain::Build::Associations,
2018-03-17 18:26:18 +05:30
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
2020-07-28 23:09:34 +05:30
# Create a new pipeline in the specified project.
#
# @param [Symbol] source What event (Ci::Pipeline.sources) triggers the pipeline
# creation.
# @param [Boolean] ignore_skip_ci Whether skipping a pipeline creation when `[skip ci]` comment
# is present in the commit body
# @param [Boolean] save_on_errors Whether persisting an invalid pipeline when it encounters an
# error during creation (e.g. invalid yaml)
# @param [Ci::TriggerRequest] trigger_request The pipeline trigger triggers the pipeline creation.
# @param [Ci::PipelineSchedule] schedule The pipeline schedule triggers the pipeline creation.
# @param [MergeRequest] merge_request The merge request triggers the pipeline creation.
# @param [ExternalPullRequest] external_pull_request The external pull request triggers the pipeline creation.
# @param [Ci::Bridge] bridge The bridge job that triggers the downstream pipeline creation.
# @param [String] content The content of .gitlab-ci.yml to override the default config
# contents (e.g. .gitlab-ci.yml in repostiry). Mainly used for
# generating a dangling pipeline.
#
# @return [Ci::Pipeline] The created Ci::Pipeline object.
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?
2020-07-28 23:09:34 +05:30
raise CreateError, pipeline.full_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
2020-05-24 23:13:21 +05:30
project.ci_pipelines
.where(ref: pipeline.ref)
.where.not(id: pipeline.same_family_pipeline_ids)
.where.not(sha: project.commit(pipeline.ref).try(:id))
.alive_or_scheduled
.with_only_interruptible_builds
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
2020-07-28 23:09:34 +05:30
def extra_options(content: nil)
{ content: content }
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')