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
|
|
|
|
2017-10-09 07:07:55 -04:00
|
|
|
SEQUENCE = [Gitlab::Ci::Pipeline::Chain::Build,
|
|
|
|
Gitlab::Ci::Pipeline::Chain::Validate::Abilities,
|
2017-09-26 05:53:50 -04:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Validate::Repository,
|
|
|
|
Gitlab::Ci::Pipeline::Chain::Validate::Config,
|
2017-09-26 03:54:56 -04:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Skip,
|
2018-03-21 05:59:11 -04:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Populate,
|
2017-09-26 03:54:56 -04:00
|
|
|
Gitlab::Ci::Pipeline::Chain::Create].freeze
|
2017-09-25 10:22:00 -04:00
|
|
|
|
2017-09-26 03:54:56 -04:00
|
|
|
def execute(source, ignore_skip_ci: false, save_on_errors: true, trigger_request: nil, schedule: nil, &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],
|
|
|
|
before_sha: params[:before],
|
|
|
|
trigger_request: trigger_request,
|
|
|
|
schedule: schedule,
|
|
|
|
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,
|
|
|
|
current_user: current_user)
|
2017-09-25 10:22:00 -04:00
|
|
|
|
2017-09-26 05:13:40 -04:00
|
|
|
sequence = Gitlab::Ci::Pipeline::Chain::Sequence
|
|
|
|
.new(pipeline, command, SEQUENCE)
|
2017-07-27 11:33:41 -04:00
|
|
|
|
2017-09-26 05:13:40 -04:00
|
|
|
sequence.build! do |pipeline, sequence|
|
2017-09-07 03:58:15 -04:00
|
|
|
schedule_head_pipeline_update
|
2017-07-21 05:49:32 -04:00
|
|
|
|
2017-09-26 05:13:40 -04:00
|
|
|
if sequence.complete?
|
2017-09-26 03:54:56 -04:00
|
|
|
cancel_pending_pipelines if project.auto_cancel_pending_pipelines?
|
|
|
|
pipeline_created_counter.increment(source: source)
|
2017-07-26 05:31:09 -04:00
|
|
|
|
2017-09-26 03:54:56 -04:00
|
|
|
pipeline.process!
|
2017-07-26 05:31:09 -04:00
|
|
|
end
|
2017-07-21 05:49:32 -04:00
|
|
|
end
|
2017-09-07 03:58:15 -04:00
|
|
|
|
|
|
|
pipeline
|
2017-07-21 05:49:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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-02-19 17:17:24 -05:00
|
|
|
def cancel_pending_pipelines
|
2017-03-18 06:04:14 -04:00
|
|
|
Gitlab::OptimisticLocking.retry_lock(auto_cancelable_pipelines) do |cancelables|
|
2017-02-19 17:17:24 -05:00
|
|
|
cancelables.find_each do |cancelable|
|
2017-04-06 09:32:56 -04:00
|
|
|
cancelable.auto_cancel_running(pipeline)
|
2017-02-19 17:17:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-18 06:04:14 -04:00
|
|
|
def auto_cancelable_pipelines
|
|
|
|
project.pipelines
|
|
|
|
.where(ref: pipeline.ref)
|
|
|
|
.where.not(id: pipeline.id)
|
2018-03-21 12:08:56 -04:00
|
|
|
.where.not(sha: project.commit(pipeline.ref).try(:id))
|
2017-03-18 06:04:14 -04:00
|
|
|
.created_or_pending
|
|
|
|
end
|
|
|
|
|
2017-06-07 11:12:23 -04:00
|
|
|
def pipeline_created_counter
|
2017-09-25 10:59:05 -04:00
|
|
|
@pipeline_created_counter ||= Gitlab::Metrics
|
|
|
|
.counter(:pipelines_created_total, "Counter of pipelines created")
|
2017-06-07 11:12:23 -04:00
|
|
|
end
|
2017-09-07 03:58:15 -04:00
|
|
|
|
|
|
|
def schedule_head_pipeline_update
|
|
|
|
related_merge_requests.each do |merge_request|
|
|
|
|
UpdateHeadPipelineForMergeRequestWorker.perform_async(merge_request.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def related_merge_requests
|
2018-03-06 18:07:16 -05:00
|
|
|
pipeline.project.source_of_merge_requests.opened.where(source_branch: pipeline.ref)
|
2017-09-07 03:58:15 -04:00
|
|
|
end
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|
2016-05-12 14:08:18 -04:00
|
|
|
end
|