Class: Gitlab::Ci::Variables::Builder

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/ci/variables/builder.rb,
lib/gitlab/ci/variables/builder/group.rb,
lib/gitlab/ci/variables/builder/project.rb,
lib/gitlab/ci/variables/builder/release.rb,
lib/gitlab/ci/variables/builder/instance.rb,
lib/gitlab/ci/variables/builder/pipeline.rb

Defined Under Namespace

Classes: Group, Instance, Pipeline, Project, Release

Instance Method Summary collapse

Constructor Details

#initialize(pipeline) ⇒ Builder

Returns a new instance of Builder.



9
10
11
12
13
14
15
16
# File 'lib/gitlab/ci/variables/builder.rb', line 9

def initialize(pipeline)
  @pipeline = pipeline
  @pipeline_variables_builder = Builder::Pipeline.new(pipeline)
  @instance_variables_builder = Builder::Instance.new
  @project_variables_builder = Builder::Project.new(project)
  @group_variables_builder = Builder::Group.new(project&.group)
  @release_variables_builder = Builder::Release.new(release)
end

Instance Method Details

#config_variablesObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gitlab/ci/variables/builder.rb', line 37

def config_variables
  Gitlab::Ci::Variables::Collection.new.tap do |variables|
    break variables unless project

    variables.concat(project.predefined_variables)
    variables.concat(pipeline_variables_builder.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)
  end
end

#deployment_variables(environment:, job:) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/gitlab/ci/variables/builder.rb', line 67

def deployment_variables(environment:, job:)
  return [] unless environment

  project.deployment_variables(
    environment: environment,
    kubernetes_namespace: job.expanded_kubernetes_namespace
  )
end

#kubernetes_variables(environment:, job:) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gitlab/ci/variables/builder.rb', line 51

def kubernetes_variables(environment:, job:)
  ::Gitlab::Ci::Variables::Collection.new.tap do |collection|
    # 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))
    template = ::Ci::GenerateKubeconfigService.new(pipeline, token: job.try(:token), environment: environment).execute
    kubeconfig_yaml = collection['KUBECONFIG']&.value
    template.merge_yaml(kubeconfig_yaml) if kubeconfig_yaml.present?

    if template.valid?
      collection.append(key: 'KUBECONFIG', value: template.to_yaml, public: false, file: true)
    end
  end
end

#release_variablesObject



112
113
114
115
116
# File 'lib/gitlab/ci/variables/builder.rb', line 112

def release_variables
  strong_memoize(:release_variables) do
    release_variables_builder.variables
  end
end

#scoped_variables(job, environment:, dependencies:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gitlab/ci/variables/builder.rb', line 18

def scoped_variables(job, environment:, dependencies:)
  Gitlab::Ci::Variables::Collection.new.tap do |variables|
    variables.concat(predefined_variables(job, environment))
    variables.concat(project.predefined_variables)
    variables.concat(pipeline_variables_builder.predefined_variables)
    variables.concat(job.runner.predefined_variables) if job.runnable? && job.runner
    variables.concat(kubernetes_variables(environment: environment, job: job))
    variables.concat(job.yaml_variables)
    variables.concat(user_variables(job.user))
    variables.concat(job.dependency_variables) if dependencies
    variables.concat(secret_instance_variables)
    variables.concat(secret_group_variables(environment: environment))
    variables.concat(secret_project_variables(environment: environment))
    variables.concat(pipeline.variables)
    variables.concat(pipeline_schedule_variables)
    variables.concat(release_variables)
  end
end

#secret_group_variables(environment:) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/gitlab/ci/variables/builder.rb', line 94

def secret_group_variables(environment:)
  strong_memoize_with(:secret_group_variables, environment) do
    group_variables_builder
      .secret_variables(
        environment: environment,
        protected_ref: protected_ref?)
  end
end

#secret_instance_variablesObject



87
88
89
90
91
92
# File 'lib/gitlab/ci/variables/builder.rb', line 87

def secret_instance_variables
  strong_memoize(:secret_instance_variables) do
    instance_variables_builder
      .secret_variables(protected_ref: protected_ref?)
  end
end

#secret_project_variables(environment:) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/gitlab/ci/variables/builder.rb', line 103

def secret_project_variables(environment:)
  strong_memoize_with(:secret_project_variables, environment) do
    project_variables_builder
      .secret_variables(
        environment: environment,
        protected_ref: protected_ref?)
  end
end

#user_variables(user) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/gitlab/ci/variables/builder.rb', line 76

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