Module: Ci::Contextable

Included in:
Bridge, Build
Defined in:
app/models/concerns/ci/contextable.rb

Overview

This module implements methods that provide context in form of essential CI/CD variables that can be used by a build / bridge job.

Instance Method Summary collapse

Instance Method Details

#deployment_variables(environment:) ⇒ Object


83
84
85
86
87
88
89
90
# File 'app/models/concerns/ci/contextable.rb', line 83

def deployment_variables(environment:)
  return [] unless environment

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

#predefined_variablesObject


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/concerns/ci/contextable.rb', line 65

def predefined_variables
  Gitlab::Ci::Variables::Collection.new.tap do |variables|
    variables.append(key: 'CI_JOB_NAME', value: name)
    variables.append(key: 'CI_JOB_STAGE', value: stage)
    variables.append(key: 'CI_JOB_MANUAL', value: 'true') if action?
    variables.append(key: 'CI_PIPELINE_TRIGGERED', value: 'true') if trigger_request

    variables.append(key: 'CI_NODE_INDEX', value: self.options[:instance].to_s) if self.options&.include?(:instance)
    variables.append(key: 'CI_NODE_TOTAL', value: ci_node_total_value.to_s)

    # legacy variables
    variables.append(key: 'CI_BUILD_NAME', value: name)
    variables.append(key: 'CI_BUILD_STAGE', value: stage)
    variables.append(key: 'CI_BUILD_TRIGGERED', value: 'true') if trigger_request
    variables.append(key: 'CI_BUILD_MANUAL', value: 'true') if action?
  end
end

#scoped_variables(environment: expanded_environment_name, dependencies: true) ⇒ Object

Variables in the environment name scope.


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/concerns/ci/contextable.rb', line 12

def scoped_variables(environment: expanded_environment_name, dependencies: true)
  Gitlab::Ci::Variables::Collection.new.tap do |variables|
    variables.concat(predefined_variables)
    variables.concat(project.predefined_variables)
    variables.concat(pipeline.predefined_variables)
    variables.concat(runner.predefined_variables) if runnable? && runner
    variables.concat(deployment_variables(environment: environment))
    variables.concat(yaml_variables)
    variables.concat(user_variables)
    variables.concat(dependency_variables) if dependencies
    variables.concat(secret_instance_variables)
    variables.concat(secret_group_variables)
    variables.concat(secret_project_variables(environment: environment))
    variables.concat(trigger_request.user_variables) if trigger_request
    variables.concat(pipeline.variables)
    variables.concat(pipeline.pipeline_schedule.job_variables) if pipeline.pipeline_schedule
  end
end

#scoped_variables_hashObject

Regular Ruby hash of scoped variables, without duplicates that are possible to be present in an array of hashes returned from `variables`.


35
36
37
# File 'app/models/concerns/ci/contextable.rb', line 35

def scoped_variables_hash
  scoped_variables.to_hash
end

#secret_group_variablesObject


96
97
98
99
100
# File 'app/models/concerns/ci/contextable.rb', line 96

def secret_group_variables
  return [] unless project.group

  project.group.ci_variables_for(git_ref, project)
end

#secret_instance_variablesObject


92
93
94
# File 'app/models/concerns/ci/contextable.rb', line 92

def secret_instance_variables
  project.ci_instance_variables_for(ref: git_ref)
end

#secret_project_variables(environment: persisted_environment) ⇒ Object


102
103
104
# File 'app/models/concerns/ci/contextable.rb', line 102

def secret_project_variables(environment: persisted_environment)
  project.ci_variables_for(ref: git_ref, environment: environment)
end

#simple_variablesObject

Variables that do not depend on the environment name.


42
43
44
45
46
# File 'app/models/concerns/ci/contextable.rb', line 42

def simple_variables
  strong_memoize(:simple_variables) do
    scoped_variables(environment: nil).to_runner_variables
  end
end

#simple_variables_without_dependenciesObject


48
49
50
51
52
# File 'app/models/concerns/ci/contextable.rb', line 48

def simple_variables_without_dependencies
  strong_memoize(:variables_without_dependencies) do
    scoped_variables(environment: nil, dependencies: false).to_runner_variables
  end
end

#user_variablesObject


54
55
56
57
58
59
60
61
62
63
# File 'app/models/concerns/ci/contextable.rb', line 54

def user_variables
  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