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