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



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/gitlab/ci/variables/builder.rb', line 78

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, kubernetes_namespace) ⇒ Object



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

def deployment_variables(environment, kubernetes_namespace)
  strong_memoize_with(:deployment_variables, environment, kubernetes_namespace) do
    next [] unless environment

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

#kubeconfig_variables(environment, kubernetes_namespace, token, kubeconfig_yaml) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/gitlab/ci/variables/builder.rb', line 113

def kubeconfig_variables(environment, kubernetes_namespace, token, kubeconfig_yaml)
  # kubernetes_namespace is part of the cache key because the value of KUBECONFIG depends on it.
  # And we don't want to use `kubeconfig_yaml` in the cache key because it can be too large.
  strong_memoize_with(:kubeconfig_variables, environment, token, kubernetes_namespace) do
    template = ::Ci::GenerateKubeconfigService.new(pipeline, token: token, environment: environment).execute
    template.merge_yaml(kubeconfig_yaml) if kubeconfig_yaml.present?

    next [] unless template.valid?

    ::Gitlab::Ci::Variables::Collection.new.tap do |collection|
      collection.append(key: 'KUBECONFIG', value: template.to_yaml, public: false, file: true)
    end
  end
end

#kubernetes_variables(environment:, token:, kubernetes_namespace:) ⇒ Object



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

def kubernetes_variables(environment:, token:, kubernetes_namespace:)
  ::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, kubernetes_namespace))
    collection.concat(kubeconfig_variables(environment, kubernetes_namespace, token, collection['KUBECONFIG']&.value))
  end
end

#release_variablesObject



164
165
166
167
168
# File 'lib/gitlab/ci/variables/builder.rb', line 164

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

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

When adding new variables, consider either adding or commenting out them in the following methods:

  • unprotected_scoped_variables

  • scoped_variables_for_pipeline_seed



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

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_from_job(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

#scoped_variables_for_pipeline_seed(job_attr, environment:, kubernetes_namespace:, user:, trigger_request:) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/gitlab/ci/variables/builder.rb', line 59

def scoped_variables_for_pipeline_seed(job_attr, environment:, kubernetes_namespace:, user:, trigger_request:)
  Gitlab::Ci::Variables::Collection.new.tap do |variables|
    variables.concat(predefined_variables_from_job_attr(job_attr, environment, trigger_request))
    variables.concat(project.predefined_variables)
    variables.concat(pipeline_variables_builder.predefined_variables)
    # job.runner.predefined_variables: No need because it's not available in the Seed step.
    variables.concat(kubernetes_variables_from_attr(environment: environment, kubernetes_namespace: kubernetes_namespace))
    variables.concat(job_attr[:yaml_variables])
    variables.concat(user_variables(user))
    # job.dependency_variables: No need because dependencies are not in the Seed step.
    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:, include_protected_vars: protected_ref?) ) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/gitlab/ci/variables/builder.rb', line 146

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

#secret_instance_variablesObject



139
140
141
142
143
144
# File 'lib/gitlab/ci/variables/builder.rb', line 139

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:, include_protected_vars: protected_ref?) ) ⇒ Object



155
156
157
158
159
160
161
162
# File 'lib/gitlab/ci/variables/builder.rb', line 155

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

#unprotected_scoped_variables(job, expose_project_variables:, expose_group_variables:, environment:, dependencies:) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gitlab/ci/variables/builder.rb', line 40

def unprotected_scoped_variables(job, expose_project_variables:, expose_group_variables:, 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_from_job(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, include_protected_vars: expose_group_variables))
    variables.concat(secret_project_variables(environment: environment, include_protected_vars: expose_project_variables))
    variables.concat(pipeline.variables)
    variables.concat(pipeline_schedule_variables)
    variables.concat(release_variables)
  end
end

#user_variables(user) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/gitlab/ci/variables/builder.rb', line 128

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