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,
|
2021-06-08 01:23:25 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::Validate::SecurityOrchestrationPolicy,
|
2021-10-27 15:23:28 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::Skip,
|
2019-12-26 22:10:19 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::Config::Content,
|
|
|
|
Gitlab::Ci::Pipeline::Chain::Config::Process,
|
2021-09-04 01:27:46 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::Validate::AfterConfig,
|
2019-12-26 22:10:19 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::RemoveUnwantedChatJobs,
|
2021-01-29 00:20:46 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::SeedBlock,
|
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,
|
2021-02-22 17:27:13 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::Limit::Deployments,
|
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,
|
2020-10-24 23:57:45 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::StopDryRun,
|
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,
|
2020-10-24 23:57:45 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::Limit::JobActivity,
|
|
|
|
Gitlab::Ci::Pipeline::Chain::CancelPendingPipelines,
|
|
|
|
Gitlab::Ci::Pipeline::Chain::Metrics,
|
2021-03-08 18:12:59 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::TemplateUsage,
|
2020-10-24 23:57:45 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::Pipeline::Process].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,
|
2021-01-03 14:25:43 +05:30
|
|
|
**extra_options(**options))
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
# Ensure we never persist the pipeline when dry_run: true
|
|
|
|
@pipeline.readonly! if command.dry_run?
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
Gitlab::Ci::Pipeline::Chain::Sequence
|
|
|
|
.new(pipeline, command, SEQUENCE)
|
|
|
|
.build!
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
if pipeline.persisted?
|
|
|
|
schedule_head_pipeline_update
|
|
|
|
create_namespace_onboarding_action
|
2021-10-27 15:23:28 +05:30
|
|
|
else
|
|
|
|
# If pipeline is not persisted, try to recover IID
|
|
|
|
pipeline.reset_project_iid
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
if error_message = pipeline.full_error_messages.presence || pipeline.failure_reason.presence
|
|
|
|
ServiceResponse.error(message: error_message, payload: pipeline)
|
|
|
|
else
|
|
|
|
ServiceResponse.success(payload: pipeline)
|
|
|
|
end
|
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)
|
2021-04-29 21:17:54 +05:30
|
|
|
source = args[0]
|
|
|
|
params = Hash(args[1])
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
execute(source, **params, &block).tap do |response|
|
|
|
|
unless response.payload.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
|
|
|
|
|
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
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
def create_namespace_onboarding_action
|
|
|
|
Namespaces::OnboardingPipelineCreatedWorker.perform_async(project.namespace_id)
|
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
def extra_options(content: nil, dry_run: false)
|
|
|
|
{ content: content, dry_run: dry_run }
|
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
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
Ci::CreatePipelineService.prepend_mod_with('Ci::CreatePipelineService')
|