2021-12-11 22:18:48 +05:30
|
|
|
# rubocop:disable Naming/FileName
|
2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
2018-03-17 18:26:18 +05:30
|
|
|
module Ci
|
|
|
|
module Pipeline
|
|
|
|
module Chain
|
|
|
|
Command = Struct.new(
|
|
|
|
:source, :project, :current_user,
|
2019-07-07 11:18:12 +05:30
|
|
|
:origin_ref, :checkout_sha, :after_sha, :before_sha, :source_sha, :target_sha,
|
2019-12-04 20:38:33 +05:30
|
|
|
:trigger_request, :schedule, :merge_request, :external_pull_request,
|
2018-03-17 18:26:18 +05:30
|
|
|
:ignore_skip_ci, :save_incompleted,
|
2019-07-07 11:18:12 +05:30
|
|
|
:seeds_block, :variables_attributes, :push_options,
|
2022-01-26 12:08:38 +05:30
|
|
|
:chat_data, :allow_mirror_update, :bridge, :content, :dry_run, :logger,
|
2019-12-26 22:10:19 +05:30
|
|
|
# These attributes are set by Chains during processing:
|
2021-04-29 21:17:54 +05:30
|
|
|
:config_content, :yaml_processor_result, :workflow_rules_result, :pipeline_seed
|
2018-03-17 18:26:18 +05:30
|
|
|
) do
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def initialize(params = {})
|
2018-03-17 18:26:18 +05:30
|
|
|
params.each do |key, value|
|
|
|
|
self[key] = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
alias_method :dry_run?, :dry_run
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def branch_exists?
|
|
|
|
strong_memoize(:is_branch) do
|
2021-08-04 16:29:09 +05:30
|
|
|
branch_ref? && project.repository.branch_exists?(ref)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def tag_exists?
|
|
|
|
strong_memoize(:is_tag) do
|
2021-08-04 16:29:09 +05:30
|
|
|
tag_ref? && project.repository.tag_exists?(ref)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def merge_request_ref_exists?
|
|
|
|
strong_memoize(:merge_request_ref_exists) do
|
|
|
|
MergeRequest.merge_request_ref?(origin_ref) &&
|
|
|
|
project.repository.ref_exists?(origin_ref)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def ref
|
|
|
|
strong_memoize(:ref) do
|
|
|
|
Gitlab::Git.ref_name(origin_ref)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sha
|
|
|
|
strong_memoize(:sha) do
|
|
|
|
project.commit(origin_sha || origin_ref).try(:id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def origin_sha
|
|
|
|
checkout_sha || after_sha
|
|
|
|
end
|
|
|
|
|
|
|
|
def before_sha
|
|
|
|
self[:before_sha] || checkout_sha || Gitlab::Git::BLANK_SHA
|
|
|
|
end
|
|
|
|
|
|
|
|
def protected_ref?
|
|
|
|
strong_memoize(:protected_ref) do
|
2019-01-03 12:48:30 +05:30
|
|
|
project.protected_for?(origin_ref)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ambiguous_ref?
|
|
|
|
strong_memoize(:ambiguous_ref) do
|
|
|
|
project.repository.ambiguous_ref?(origin_ref)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
def parent_pipeline
|
|
|
|
bridge&.parent_pipeline
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
def parent_pipeline_partition_id
|
|
|
|
parent_pipeline.partition_id if creates_child_pipeline?
|
|
|
|
end
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
def creates_child_pipeline?
|
|
|
|
bridge&.triggers_child_pipeline?
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def metrics
|
2021-04-29 21:17:54 +05:30
|
|
|
@metrics ||= ::Gitlab::Ci::Pipeline::Metrics
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
def logger
|
|
|
|
self[:logger] ||= ::Gitlab::Ci::Pipeline::Logger.new(project: project)
|
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
def observe_step_duration(step_class, duration)
|
2022-01-26 12:08:38 +05:30
|
|
|
step = step_class.name.underscore.parameterize(separator: '_')
|
2023-03-04 22:38:38 +05:30
|
|
|
logger.observe("pipeline_step_#{step}_duration_s", duration, once: true)
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
if Feature.enabled?(:ci_pipeline_creation_step_duration_tracking, type: :ops)
|
2021-11-11 11:23:49 +05:30
|
|
|
metrics.pipeline_creation_step_duration_histogram
|
|
|
|
.observe({ step: step_class.name }, duration.seconds)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def observe_creation_duration(duration)
|
2023-03-04 22:38:38 +05:30
|
|
|
logger.observe(:pipeline_creation_duration_s, duration, once: true)
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
metrics.pipeline_creation_duration_histogram
|
2023-03-04 22:38:38 +05:30
|
|
|
.observe({ gitlab: gitlab_org_project?.to_s }, duration.seconds)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def observe_pipeline_size(pipeline)
|
2023-03-04 22:38:38 +05:30
|
|
|
logger.observe(:pipeline_size_count, pipeline.total_size, once: true)
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
metrics.pipeline_size_histogram
|
2023-01-13 00:05:48 +05:30
|
|
|
.observe({ source: pipeline.source.to_s, plan: project.actual_plan_name }, pipeline.total_size)
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
def observe_jobs_count_in_alive_pipelines
|
2022-11-25 23:54:43 +05:30
|
|
|
jobs_count = project.all_pipelines.jobs_count_in_alive_pipelines
|
2022-10-11 01:57:18 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
metrics.active_jobs_histogram
|
2022-10-11 01:57:18 +05:30
|
|
|
.observe({ plan: project.actual_plan_name }, jobs_count)
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def increment_pipeline_failure_reason_counter(reason)
|
|
|
|
metrics.pipeline_failure_reason_counter
|
|
|
|
.increment(reason: (reason || :unknown_failure).to_s)
|
|
|
|
end
|
|
|
|
|
2021-08-04 16:29:09 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
# Verifies that origin_ref is a fully qualified tag reference (refs/tags/<tag-name>)
|
|
|
|
#
|
|
|
|
# Fallbacks to `true` for backward compatibility reasons
|
|
|
|
# if origin_ref is a short ref
|
|
|
|
def tag_ref?
|
|
|
|
return true if full_git_ref_name_unavailable?
|
|
|
|
|
|
|
|
Gitlab::Git.tag_ref?(origin_ref).present?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Verifies that origin_ref is a fully qualified branch reference (refs/heads/<branch-name>)
|
|
|
|
#
|
|
|
|
# Fallbacks to `true` for backward compatibility reasons
|
|
|
|
# if origin_ref is a short ref
|
|
|
|
def branch_ref?
|
|
|
|
return true if full_git_ref_name_unavailable?
|
|
|
|
|
|
|
|
Gitlab::Git.branch_ref?(origin_ref).present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def full_git_ref_name_unavailable?
|
|
|
|
ref == origin_ref
|
|
|
|
end
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
def gitlab_org_project?
|
|
|
|
project.full_path == 'gitlab-org/gitlab'
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
# rubocop:enable Naming/FileName
|