2019-12-21 20:55:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class Config
|
|
|
|
module External
|
|
|
|
class Context
|
2021-12-11 22:18:48 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
TimeoutError = Class.new(StandardError)
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
MAX_INCLUDES = 150
|
|
|
|
TEMP_MAX_INCLUDES = 100 # For logging; to be removed in https://gitlab.com/gitlab-org/gitlab/-/issues/367150
|
2022-08-13 15:12:31 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
include ::Gitlab::Utils::StrongMemoize
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
attr_reader :project, :sha, :user, :parent_pipeline, :variables, :pipeline_config
|
2022-08-13 15:12:31 +05:30
|
|
|
attr_reader :expandset, :execution_deadline, :logger, :max_includes
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
delegate :instrument, to: :logger
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
def initialize(
|
|
|
|
project: nil, sha: nil, user: nil, parent_pipeline: nil, variables: nil,
|
2023-05-27 22:25:52 +05:30
|
|
|
pipeline_config: nil, logger: nil
|
2022-08-13 15:12:31 +05:30
|
|
|
)
|
2019-12-21 20:55:43 +05:30
|
|
|
@project = project
|
|
|
|
@sha = sha
|
|
|
|
@user = user
|
2020-04-08 14:13:33 +05:30
|
|
|
@parent_pipeline = parent_pipeline
|
2022-01-26 12:08:38 +05:30
|
|
|
@variables = variables || Ci::Variables::Collection.new
|
2023-05-27 22:25:52 +05:30
|
|
|
@pipeline_config = pipeline_config
|
|
|
|
@expandset = []
|
2019-12-21 20:55:43 +05:30
|
|
|
@execution_deadline = 0
|
2022-01-26 12:08:38 +05:30
|
|
|
@logger = logger || Gitlab::Ci::Pipeline::Logger.new(project: project)
|
2023-05-27 22:25:52 +05:30
|
|
|
@max_includes = MAX_INCLUDES
|
2019-12-21 20:55:43 +05:30
|
|
|
yield self if block_given?
|
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
def top_level_worktree_paths
|
|
|
|
strong_memoize(:top_level_worktree_paths) do
|
|
|
|
project.repository.tree(sha).blobs.map(&:path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_worktree_paths
|
|
|
|
strong_memoize(:all_worktree_paths) do
|
|
|
|
project.repository.ls_files(sha)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
def variables_hash
|
|
|
|
strong_memoize(:variables_hash) do
|
|
|
|
variables.to_hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
def mutate(attrs = {})
|
|
|
|
self.class.new(**attrs) do |ctx|
|
|
|
|
ctx.expandset = expandset
|
|
|
|
ctx.execution_deadline = execution_deadline
|
2022-01-26 12:08:38 +05:30
|
|
|
ctx.logger = logger
|
2022-08-13 15:12:31 +05:30
|
|
|
ctx.max_includes = max_includes
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_deadline(timeout_seconds)
|
|
|
|
@execution_deadline = current_monotonic_time + timeout_seconds.to_f
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_execution_time!
|
|
|
|
raise TimeoutError if execution_expired?
|
|
|
|
end
|
|
|
|
|
|
|
|
def sentry_payload
|
|
|
|
{
|
|
|
|
user: user.inspect,
|
|
|
|
project: project.inspect
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
def mask_variables_from(string)
|
|
|
|
variables.reduce(string.dup) do |str, variable|
|
2022-04-01 21:47:47 +05:30
|
|
|
if variable[:masked]
|
2022-06-21 17:19:12 +05:30
|
|
|
Gitlab::Ci::MaskSecret.mask!(str, variable[:value])
|
2022-04-01 21:47:47 +05:30
|
|
|
else
|
2022-06-21 17:19:12 +05:30
|
|
|
str
|
2022-04-01 21:47:47 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
def includes
|
|
|
|
expandset.map(&:metadata)
|
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
# Some Ci::ProjectConfig sources prepend the config content with an "internal" `include`, which becomes
|
|
|
|
# the first included file. When running a pipeline, we pass pipeline_config into the context of the first
|
|
|
|
# included file, which we use in this method to determine if the file is an "internal" one.
|
|
|
|
def internal_include?
|
|
|
|
!!pipeline_config&.internal_include_prepended?
|
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
protected
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
attr_writer :expandset, :execution_deadline, :logger, :max_includes
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def current_monotonic_time
|
|
|
|
Gitlab::Metrics::System.monotonic_time
|
|
|
|
end
|
|
|
|
|
|
|
|
def execution_expired?
|
2020-10-24 23:57:45 +05:30
|
|
|
return false if execution_deadline == 0
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
current_monotonic_time > execution_deadline
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|