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

89 lines
2.6 KiB
Ruby
Raw Normal View History

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
2018-03-17 18:26:18 +05:30
SEQUENCE = [Gitlab::Ci::Pipeline::Chain::Build,
Gitlab::Ci::Pipeline::Chain::Validate::Abilities,
Gitlab::Ci::Pipeline::Chain::Validate::Repository,
Gitlab::Ci::Pipeline::Chain::Validate::Config,
Gitlab::Ci::Pipeline::Chain::Skip,
2018-05-09 12:01:36 +05:30
Gitlab::Ci::Pipeline::Chain::Populate,
2018-03-17 18:26:18 +05:30
Gitlab::Ci::Pipeline::Chain::Create].freeze
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
def execute(source, ignore_skip_ci: false, save_on_errors: true, trigger_request: nil, schedule: nil, &block)
@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],
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-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
2018-03-17 18:26:18 +05:30
pipeline.process!
2017-09-10 17:25:29 +05:30
end
2016-06-02 11:05:42 +05:30
end
2018-03-17 18:26:18 +05:30
pipeline
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
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
def auto_cancelable_pipelines
project.pipelines
.where(ref: pipeline.ref)
.where.not(id: pipeline.id)
2018-05-09 12:01:36 +05:30
.where.not(sha: project.commit(pipeline.ref).try(:id))
2017-08-17 22:00:37 +05:30
.created_or_pending
2016-06-02 11:05:42 +05:30
end
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
related_merge_requests.each do |merge_request|
UpdateHeadPipelineForMergeRequestWorker.perform_async(merge_request.id)
2017-09-10 17:25:29 +05:30
end
end
2018-03-17 18:26:18 +05:30
def related_merge_requests
2018-03-27 19:54:05 +05:30
pipeline.project.source_of_merge_requests.opened.where(source_branch: pipeline.ref)
2016-06-02 11:05:42 +05:30
end
end
end