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-05-24 09:13:51 -04:00
|
|
|
def execute(source, ignore_skip_ci: false, save_on_errors: true, trigger_request: nil, schedule: nil)
|
2016-08-11 09:22:35 -04:00
|
|
|
@pipeline = Ci::Pipeline.new(
|
2017-05-24 09:13:51 -04:00
|
|
|
source: source,
|
2016-08-11 09:22:35 -04:00
|
|
|
project: project,
|
|
|
|
ref: ref,
|
|
|
|
sha: sha,
|
|
|
|
before_sha: before_sha,
|
|
|
|
tag: tag?,
|
|
|
|
trigger_requests: Array(trigger_request),
|
2017-05-07 18:35:56 -04:00
|
|
|
user: current_user,
|
|
|
|
pipeline_schedule: schedule
|
2016-08-11 09:22:35 -04:00
|
|
|
)
|
|
|
|
|
2017-07-27 11:33:41 -04:00
|
|
|
result = validate(current_user || trigger_request.trigger.owner,
|
|
|
|
ignore_skip_ci: ignore_skip_ci,
|
|
|
|
save_on_errors: save_on_errors)
|
2017-07-26 09:58:31 -04:00
|
|
|
|
2017-07-27 11:33:41 -04:00
|
|
|
return result if result
|
|
|
|
|
|
|
|
begin
|
2017-07-26 05:31:09 -04:00
|
|
|
Ci::Pipeline.transaction do
|
|
|
|
pipeline.save!
|
2017-07-21 05:49:32 -04:00
|
|
|
|
2017-07-26 05:31:09 -04:00
|
|
|
yield(pipeline) if block_given?
|
|
|
|
|
|
|
|
Ci::CreatePipelineStagesService
|
|
|
|
.new(project, current_user)
|
|
|
|
.execute(pipeline)
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
return error("Failed to persist the pipeline: #{e}")
|
2017-07-21 05:49:32 -04:00
|
|
|
end
|
|
|
|
|
2017-07-26 05:31:09 -04:00
|
|
|
update_merge_requests_head_pipeline
|
|
|
|
|
2017-07-21 05:49:32 -04:00
|
|
|
cancel_pending_pipelines if project.auto_cancel_pending_pipelines?
|
|
|
|
|
|
|
|
pipeline_created_counter.increment(source: source)
|
|
|
|
|
|
|
|
pipeline.tap(&:process!)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def validate(triggering_user, ignore_skip_ci:, save_on_errors:)
|
2016-08-11 09:22:35 -04:00
|
|
|
unless project.builds_enabled?
|
2017-07-27 11:33:41 -04:00
|
|
|
return error('Pipeline is disabled')
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
|
|
|
|
2017-07-19 04:42:47 -04:00
|
|
|
unless allowed_to_trigger_pipeline?(triggering_user)
|
|
|
|
if can?(triggering_user, :create_pipeline, project)
|
2017-07-27 11:33:41 -04:00
|
|
|
return error("Insufficient permissions for protected ref '#{ref}'")
|
2017-07-19 04:42:47 -04:00
|
|
|
else
|
2017-07-27 11:33:41 -04:00
|
|
|
return error('Insufficient permissions to create a new pipeline')
|
2017-07-19 04:42:47 -04:00
|
|
|
end
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|
|
|
|
|
2017-07-19 13:31:20 -04:00
|
|
|
unless branch? || tag?
|
2017-07-27 11:33:41 -04:00
|
|
|
return error('Reference not found')
|
2017-07-19 13:31:20 -04:00
|
|
|
end
|
|
|
|
|
2016-08-11 09:22:35 -04:00
|
|
|
unless commit
|
2017-07-27 11:33:41 -04:00
|
|
|
return error('Commit not found')
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|
|
|
|
|
2016-06-02 06:39:15 -04:00
|
|
|
unless pipeline.config_processor
|
2016-08-11 09:22:35 -04:00
|
|
|
unless pipeline.ci_yaml_file
|
2017-07-27 11:33:41 -04:00
|
|
|
return error("Missing #{pipeline.ci_yaml_file_path} file")
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
2017-07-27 11:33:41 -04:00
|
|
|
return error(pipeline.yaml_errors, save: save_on_errors)
|
2016-06-02 06:39:15 -04:00
|
|
|
end
|
2016-05-14 20:47:16 -04:00
|
|
|
|
2016-08-11 09:22:35 -04:00
|
|
|
if !ignore_skip_ci && skip_ci?
|
2016-08-11 14:54:02 -04:00
|
|
|
pipeline.skip if save_on_errors
|
2017-07-27 02:52:55 -04:00
|
|
|
raise InsufficientConditionError, pipeline
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
2016-05-09 19:26:13 -04:00
|
|
|
|
2017-06-02 09:05:14 -04:00
|
|
|
unless pipeline.has_stage_seeds?
|
2017-07-27 11:33:41 -04:00
|
|
|
return error('No stages / jobs for this pipeline.')
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-19 04:42:47 -04:00
|
|
|
def allowed_to_trigger_pipeline?(triggering_user)
|
2017-07-06 05:37:27 -04:00
|
|
|
if triggering_user
|
|
|
|
allowed_to_create?(triggering_user)
|
|
|
|
else # legacy triggers don't have a corresponding user
|
2017-07-03 17:20:44 -04:00
|
|
|
!project.protected_for?(ref)
|
2017-07-06 05:37:27 -04:00
|
|
|
end
|
2017-07-03 17:20:44 -04:00
|
|
|
end
|
|
|
|
|
2017-07-05 09:55:35 -04:00
|
|
|
def allowed_to_create?(triggering_user)
|
|
|
|
access = Gitlab::UserAccess.new(triggering_user, project: project)
|
|
|
|
|
2017-07-19 04:42:47 -04:00
|
|
|
can?(triggering_user, :create_pipeline, project) &&
|
2017-07-05 09:55:35 -04:00
|
|
|
if branch?
|
2017-07-18 09:56:28 -04:00
|
|
|
access.can_update_branch?(ref)
|
2017-07-05 09:55:35 -04:00
|
|
|
elsif tag?
|
|
|
|
access.can_create_tag?(ref)
|
|
|
|
else
|
2017-07-19 13:31:20 -04:00
|
|
|
true # Allow it for now and we'll reject when we check ref existence
|
2017-07-05 09:55:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-25 17:14:40 -04:00
|
|
|
def update_merge_requests_head_pipeline
|
2017-06-01 14:31:52 -04:00
|
|
|
return unless pipeline.latest?
|
2017-05-25 17:14:40 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
MergeRequest.where(source_project: @pipeline.project, source_branch: @pipeline.ref)
|
|
|
|
.update_all(head_pipeline_id: @pipeline.id)
|
2017-05-25 17:14:40 -04:00
|
|
|
end
|
|
|
|
|
2016-08-11 09:22:35 -04:00
|
|
|
def skip_ci?
|
2017-02-10 05:32:59 -05:00
|
|
|
return false unless pipeline.git_commit_message
|
|
|
|
pipeline.git_commit_message =~ /\[(ci[ _-]skip|skip[ _-]ci)\]/i
|
2016-05-09 19:26:13 -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)
|
|
|
|
.where.not(sha: project.repository.sha_from_ref(pipeline.ref))
|
|
|
|
.created_or_pending
|
|
|
|
end
|
|
|
|
|
2016-05-09 19:26:13 -04:00
|
|
|
def commit
|
2016-08-11 09:22:35 -04:00
|
|
|
@commit ||= project.commit(origin_sha || origin_ref)
|
|
|
|
end
|
|
|
|
|
|
|
|
def sha
|
|
|
|
commit.try(:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def before_sha
|
|
|
|
params[:checkout_sha] || params[:before] || Gitlab::Git::BLANK_SHA
|
|
|
|
end
|
|
|
|
|
|
|
|
def origin_sha
|
|
|
|
params[:checkout_sha] || params[:after]
|
|
|
|
end
|
|
|
|
|
|
|
|
def origin_ref
|
|
|
|
params[:ref]
|
|
|
|
end
|
|
|
|
|
|
|
|
def branch?
|
2017-06-05 12:56:38 -04:00
|
|
|
return @is_branch if defined?(@is_branch)
|
|
|
|
|
|
|
|
@is_branch =
|
|
|
|
project.repository.ref_exists?(Gitlab::Git::BRANCH_REF_PREFIX + ref)
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tag?
|
2017-06-05 12:56:38 -04:00
|
|
|
return @is_tag if defined?(@is_tag)
|
|
|
|
|
|
|
|
@is_tag =
|
|
|
|
project.repository.ref_exists?(Gitlab::Git::TAG_REF_PREFIX + ref)
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def ref
|
2017-07-05 09:55:35 -04:00
|
|
|
@ref ||= Gitlab::Git.ref_name(origin_ref)
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid_sha?
|
|
|
|
origin_sha && origin_sha != Gitlab::Git::BLANK_SHA
|
|
|
|
end
|
|
|
|
|
|
|
|
def error(message, save: false)
|
|
|
|
pipeline.errors.add(:base, message)
|
2016-08-11 14:54:02 -04:00
|
|
|
pipeline.drop if save
|
2016-08-11 09:22:35 -04:00
|
|
|
pipeline
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|
2017-06-07 11:12:23 -04:00
|
|
|
|
|
|
|
def pipeline_created_counter
|
2017-07-19 03:43:57 -04:00
|
|
|
@pipeline_created_counter ||= Gitlab::Metrics.counter(:pipelines_created_total, "Counter of pipelines created")
|
2017-06-07 11:12:23 -04:00
|
|
|
end
|
2016-05-09 19:26:13 -04:00
|
|
|
end
|
2016-05-12 14:08:18 -04:00
|
|
|
end
|