2021-12-11 22:18:48 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
module Variables
|
|
|
|
class Builder
|
|
|
|
include ::Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
def initialize(pipeline)
|
|
|
|
@pipeline = pipeline
|
2022-04-04 11:22:00 +05:30
|
|
|
@instance_variables_builder = Builder::Instance.new
|
|
|
|
@project_variables_builder = Builder::Project.new(project)
|
2022-06-21 17:19:12 +05:30
|
|
|
@group_variables_builder = Builder::Group.new(project&.group)
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def scoped_variables(job, environment:, dependencies:)
|
|
|
|
Gitlab::Ci::Variables::Collection.new.tap do |variables|
|
2022-01-26 12:08:38 +05:30
|
|
|
variables.concat(predefined_variables(job))
|
2022-03-02 08:16:31 +05:30
|
|
|
variables.concat(project.predefined_variables)
|
|
|
|
variables.concat(pipeline.predefined_variables)
|
|
|
|
variables.concat(job.runner.predefined_variables) if job.runnable? && job.runner
|
2022-05-07 20:08:51 +05:30
|
|
|
variables.concat(kubernetes_variables(environment: environment, job: job))
|
2022-03-02 08:16:31 +05:30
|
|
|
variables.concat(job.yaml_variables)
|
|
|
|
variables.concat(user_variables(job.user))
|
|
|
|
variables.concat(job.dependency_variables) if dependencies
|
2022-04-04 11:22:00 +05:30
|
|
|
variables.concat(secret_instance_variables)
|
2022-06-21 17:19:12 +05:30
|
|
|
variables.concat(secret_group_variables(environment: environment))
|
|
|
|
variables.concat(secret_project_variables(environment: environment))
|
2022-03-02 08:16:31 +05:30
|
|
|
variables.concat(pipeline.variables)
|
2022-06-21 17:19:12 +05:30
|
|
|
variables.concat(pipeline_schedule_variables)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def config_variables
|
|
|
|
Gitlab::Ci::Variables::Collection.new.tap do |variables|
|
|
|
|
break variables unless project
|
|
|
|
|
|
|
|
variables.concat(project.predefined_variables)
|
|
|
|
variables.concat(pipeline.predefined_variables)
|
|
|
|
variables.concat(secret_instance_variables)
|
|
|
|
variables.concat(secret_group_variables(environment: nil))
|
|
|
|
variables.concat(secret_project_variables(environment: nil))
|
|
|
|
variables.concat(pipeline.variables)
|
|
|
|
variables.concat(pipeline_schedule_variables)
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
def kubernetes_variables(environment:, job:)
|
2022-03-02 08:16:31 +05:30
|
|
|
::Gitlab::Ci::Variables::Collection.new.tap do |collection|
|
2022-05-07 20:08:51 +05:30
|
|
|
# NOTE: deployment_variables will be removed as part of cleanup for
|
|
|
|
# https://gitlab.com/groups/gitlab-org/configure/-/epics/8
|
|
|
|
# Until then, we need to make both the old and the new KUBECONFIG contexts available
|
|
|
|
collection.concat(deployment_variables(environment: environment, job: job))
|
2022-08-13 15:12:31 +05:30
|
|
|
template = ::Ci::GenerateKubeconfigService.new(pipeline, token: job.try(:token)).execute
|
2022-05-07 20:08:51 +05:30
|
|
|
kubeconfig_yaml = collection['KUBECONFIG']&.value
|
|
|
|
template.merge_yaml(kubeconfig_yaml) if kubeconfig_yaml.present?
|
2022-03-02 08:16:31 +05:30
|
|
|
|
|
|
|
if template.valid?
|
|
|
|
collection.append(key: 'KUBECONFIG', value: template.to_yaml, public: false, file: true)
|
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
def deployment_variables(environment:, job:)
|
|
|
|
return [] unless environment
|
|
|
|
|
|
|
|
project.deployment_variables(
|
|
|
|
environment: environment,
|
|
|
|
kubernetes_namespace: job.expanded_kubernetes_namespace
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_variables(user)
|
|
|
|
Gitlab::Ci::Variables::Collection.new.tap do |variables|
|
|
|
|
break variables if user.blank?
|
|
|
|
|
|
|
|
variables.append(key: 'GITLAB_USER_ID', value: user.id.to_s)
|
|
|
|
variables.append(key: 'GITLAB_USER_EMAIL', value: user.email)
|
|
|
|
variables.append(key: 'GITLAB_USER_LOGIN', value: user.username)
|
|
|
|
variables.append(key: 'GITLAB_USER_NAME', value: user.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
def secret_instance_variables
|
|
|
|
strong_memoize(:secret_instance_variables) do
|
|
|
|
instance_variables_builder
|
|
|
|
.secret_variables(protected_ref: protected_ref?)
|
|
|
|
end
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
def secret_group_variables(environment:)
|
|
|
|
strong_memoize_with(:secret_group_variables, environment) do
|
|
|
|
group_variables_builder
|
|
|
|
.secret_variables(
|
|
|
|
environment: environment,
|
|
|
|
protected_ref: protected_ref?)
|
2022-05-07 20:08:51 +05:30
|
|
|
end
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
def secret_project_variables(environment:)
|
|
|
|
strong_memoize_with(:secret_project_variables, environment) do
|
|
|
|
project_variables_builder
|
|
|
|
.secret_variables(
|
|
|
|
environment: environment,
|
|
|
|
protected_ref: protected_ref?)
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :pipeline
|
2022-04-04 11:22:00 +05:30
|
|
|
attr_reader :instance_variables_builder
|
|
|
|
attr_reader :project_variables_builder
|
2022-05-07 20:08:51 +05:30
|
|
|
attr_reader :group_variables_builder
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
delegate :project, to: :pipeline
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
def predefined_variables(job)
|
|
|
|
Gitlab::Ci::Variables::Collection.new.tap do |variables|
|
|
|
|
variables.append(key: 'CI_JOB_NAME', value: job.name)
|
|
|
|
variables.append(key: 'CI_JOB_STAGE', value: job.stage)
|
|
|
|
variables.append(key: 'CI_JOB_MANUAL', value: 'true') if job.action?
|
|
|
|
variables.append(key: 'CI_PIPELINE_TRIGGERED', value: 'true') if job.trigger_request
|
|
|
|
|
|
|
|
variables.append(key: 'CI_NODE_INDEX', value: job.options[:instance].to_s) if job.options&.include?(:instance)
|
|
|
|
variables.append(key: 'CI_NODE_TOTAL', value: ci_node_total_value(job).to_s)
|
|
|
|
|
|
|
|
# legacy variables
|
|
|
|
variables.append(key: 'CI_BUILD_NAME', value: job.name)
|
|
|
|
variables.append(key: 'CI_BUILD_STAGE', value: job.stage)
|
|
|
|
variables.append(key: 'CI_BUILD_TRIGGERED', value: 'true') if job.trigger_request
|
|
|
|
variables.append(key: 'CI_BUILD_MANUAL', value: 'true') if job.action?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
def pipeline_schedule_variables
|
|
|
|
strong_memoize(:pipeline_schedule_variables) do
|
|
|
|
variables = if pipeline.pipeline_schedule
|
|
|
|
pipeline.pipeline_schedule.job_variables
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2022-05-07 20:08:51 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
Gitlab::Ci::Variables::Collection.new(variables)
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
def ci_node_total_value(job)
|
|
|
|
parallel = job.options&.dig(:parallel)
|
|
|
|
parallel = parallel.dig(:total) if parallel.is_a?(Hash)
|
|
|
|
parallel || 1
|
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
def protected_ref?
|
|
|
|
strong_memoize(:protected_ref) do
|
|
|
|
project.protected_for?(pipeline.jobs_git_ref)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def strong_memoize_with(name, *args)
|
|
|
|
container = strong_memoize(name) { {} }
|
|
|
|
|
|
|
|
if container.key?(args)
|
|
|
|
container[args]
|
|
|
|
else
|
|
|
|
container[args] = yield
|
|
|
|
end
|
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|