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,
|
2020-08-12 11:10:02 -04:00
|
|
|
Gitlab::Ci::Pipeline::Chain::StopDryRun,
|
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,
|
2020-08-12 11:10:02 -04:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Limit::JobActivity,
|
|
|
|
Gitlab::Ci::Pipeline::Chain::CancelPendingPipelines,
|
|
|
|
Gitlab::Ci::Pipeline::Chain::Metrics,
|
|
|
|
Gitlab::Ci::Pipeline::Chain::Pipeline::Process].freeze
|
2017-09-25 10:22:00 -04:00
|
|
|
|
2020-06-25 11:08:37 -04:00
|
|
|
# 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-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
|
|
|
|
2020-08-12 11:10:02 -04:00
|
|
|
# Ensure we never persist the pipeline when dry_run: true
|
|
|
|
@pipeline.readonly! if command.dry_run?
|
2017-07-27 11:33:41 -04:00
|
|
|
|
2020-08-12 11:10:02 -04:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Sequence
|
|
|
|
.new(pipeline, command, SEQUENCE)
|
|
|
|
.build!
|
2017-07-21 05:49:32 -04:00
|
|
|
|
2020-08-12 11:10:02 -04:00
|
|
|
schedule_head_pipeline_update if pipeline.persisted?
|
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?
|
2020-06-29 08:09:20 -04:00
|
|
|
raise CreateError, pipeline.full_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-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
|
|
|
|
|
2020-08-12 11:10:02 -04:00
|
|
|
def extra_options(content: nil, dry_run: false)
|
|
|
|
{ content: content, dry_run: dry_run }
|
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')
|