summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShinya Maeda <shinya@gitlab.com>2019-01-25 14:57:12 +0900
committerShinya Maeda <shinya@gitlab.com>2019-01-25 14:57:12 +0900
commita6555ff504c5388e6732a652ad78fdd4d52528ae (patch)
treec2fb371f62e6984094c58343150592c6e609fa42
parent07941f7f2232f2442185d53576d877c5b65093a7 (diff)
downloadgitlab-ce-optimize-persisted-environment-in-ci-builds.tar.gz
Improve performance of `Ci::Build#persisted_environment`optimize-persisted-environment-in-ci-builds
-rw-r--r--app/models/ci/build.rb13
1 files changed, 11 insertions, 2 deletions
diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb
index cfdb3c0d719..7ca34251a22 100644
--- a/app/models/ci/build.rb
+++ b/app/models/ci/build.rb
@@ -48,13 +48,22 @@ module Ci
delegate :trigger_short_token, to: :trigger_request, allow_nil: true
##
- # The "environment" field for builds is a String, and is the unexpanded name!
+ # Since Gitlab 11.5, deployments records started being created right after
+ # `ci_builds` creation. We can look up a relevant `environment` through
+ # `deployment` relation today. This is much more efficient than expanding
+ # environment name with variables.
#
+ # However, we have to still expand environment name if it's a stop action,
+ # because `deployment` persists information for start action only.
+ #
+ # We will follow up this by persisting expanded name in build metadata or
+ # persisting stop action in database.
def persisted_environment
return unless has_environment?
strong_memoize(:persisted_environment) do
- Environment.find_by(name: expanded_environment_name, project: project)
+ deployment&.environment ||
+ Environment.find_by(name: expanded_environment_name, project: project)
end
end