Module: Ci::Deployable

Extended by:
ActiveSupport::Concern
Included in:
Bridge, Build
Defined in:
app/models/concerns/ci/deployable.rb

Instance Method Summary collapse

Instance Method Details

#actual_persisted_environmentObject

If build.persisted_environment is a BatchLoader, we need to remove the method proxy in order to clone into new item here github.com/exAspArk/batch-loader/issues/31



86
87
88
# File 'app/models/concerns/ci/deployable.rb', line 86

def actual_persisted_environment
  persisted_environment.respond_to?(:__sync) ? persisted_environment.__sync : persisted_environment
end

#deployment_job?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'app/models/concerns/ci/deployable.rb', line 120

def deployment_job?
  has_environment_keyword? && environment_action == 'start'
end

#deployment_statusObject

Virtual deployment status depending on the environment status.



44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/concerns/ci/deployable.rb', line 44

def deployment_status
  return unless deployment_job?

  if success?
    return successful_deployment_status
  elsif failed?
    return :failed
  end

  :creating
end

#environment_actionObject



128
129
130
# File 'app/models/concerns/ci/deployable.rb', line 128

def environment_action
  options.fetch(:environment, {}).fetch(:action, 'start') if options
end

#environment_slugObject



144
145
146
# File 'app/models/concerns/ci/deployable.rb', line 144

def environment_slug
  persisted_environment.try(:slug)
end

#environment_statusObject



148
149
150
151
152
153
154
# File 'app/models/concerns/ci/deployable.rb', line 148

def environment_status
  strong_memoize(:environment_status) do
    if has_environment_keyword? && merge_request
      EnvironmentStatus.new(project, persisted_environment, merge_request, pipeline.sha)
    end
  end
end

#environment_tierObject



136
137
138
# File 'app/models/concerns/ci/deployable.rb', line 136

def environment_tier
  environment_tier_from_options || persisted_environment.try(:tier)
end

#environment_tier_from_optionsObject



132
133
134
# File 'app/models/concerns/ci/deployable.rb', line 132

def environment_tier_from_options
  options.dig(:environment, :deployment_tier) if options
end

#environment_urlObject



140
141
142
# File 'app/models/concerns/ci/deployable.rb', line 140

def environment_url
  options&.dig(:environment, :url) || persisted_environment.try(:external_url)
end

#expanded_environment_nameObject



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/concerns/ci/deployable.rb', line 90

def expanded_environment_name
  return unless has_environment_keyword?

  strong_memoize(:expanded_environment_name) do
    # We're using a persisted expanded environment name in order to avoid
    # variable expansion per request.
    if &.expanded_environment_name.present?
      .expanded_environment_name
    else
      ExpandVariables.expand(environment, -> { simple_variables.sort_and_expand_all })
    end
  end
end

#expanded_kubernetes_namespaceObject



104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/concerns/ci/deployable.rb', line 104

def expanded_kubernetes_namespace
  return unless has_environment_keyword?

  namespace = options.dig(:environment, :kubernetes, :namespace)

  if namespace.present? # rubocop:disable Style/GuardClause
    strong_memoize(:expanded_kubernetes_namespace) do
      ExpandVariables.expand(namespace, -> { simple_variables })
    end
  end
end

#has_environment_keyword?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'app/models/concerns/ci/deployable.rb', line 116

def has_environment_keyword?
  environment.present?
end

#on_stopObject



156
157
158
# File 'app/models/concerns/ci/deployable.rb', line 156

def on_stop
  options&.dig(:environment, :on_stop)
end

#outdated_deployment?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
# File 'app/models/concerns/ci/deployable.rb', line 34

def outdated_deployment?
  strong_memoize(:outdated_deployment) do
    deployment_job? &&
      project.ci_forward_deployment_enabled? &&
      (!project.ci_forward_deployment_rollback_allowed? || incomplete?) &&
      deployment&.older_than_last_successful_deployment?
  end
end

#persisted_environmentObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/concerns/ci/deployable.rb', line 64

def persisted_environment
  return unless has_environment_keyword?

  strong_memoize(:persisted_environment) do
    # This code path has caused N+1s in the past, since environments are only indirectly
    # associated to builds and pipelines; see https://gitlab.com/gitlab-org/gitlab/-/issues/326445
    # We therefore batch-load them to prevent dormant N+1s until we found a proper solution.
    BatchLoader.for(expanded_environment_name).batch(key: project_id) do |names, loader, args|
      Environment.where(name: names, project: args[:key]).find_each do |environment|
        loader.call(environment.name, environment)
      end
    end
  end
end

#persisted_environment=(environment) ⇒ Object



79
80
81
# File 'app/models/concerns/ci/deployable.rb', line 79

def persisted_environment=(environment)
  strong_memoize(:persisted_environment) { environment }
end

#stop_action_successful?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'app/models/concerns/ci/deployable.rb', line 160

def stop_action_successful?
  success?
end

#stops_environment?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'app/models/concerns/ci/deployable.rb', line 124

def stops_environment?
  has_environment_keyword? && environment_action == 'stop'
end

#successful_deployment_statusObject



56
57
58
59
60
61
62
# File 'app/models/concerns/ci/deployable.rb', line 56

def successful_deployment_status
  if deployment&.last?
    :last
  else
    :out_of_date
  end
end