summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2019-02-05 21:29:00 +0000
committerStan Hu <stanhu@gmail.com>2019-02-05 21:29:00 +0000
commit07d137a93afc43bfdc95f821a0f32bf07ff87e34 (patch)
tree1cc0ad5ea3abde80dbe8ba2709a57f30e50af456
parent91b1e9dc77eea57535e1f43c6f32d60d0ee34217 (diff)
parentbd2ebeda84e4a36d58713c7d9314ad44ff6d89ed (diff)
downloadgitlab-ce-07d137a93afc43bfdc95f821a0f32bf07ff87e34.tar.gz
Merge branch 'use-deployment-relation-to-fetch-environment-ce' into 'master'
Backport: Optimize slow pipelines.js response See merge request gitlab-org/gitlab-ce!24890
-rw-r--r--app/models/ci/build.rb14
-rw-r--r--changelogs/unreleased/use-deployment-relation-to-fetch-environment-ce.yml5
-rw-r--r--spec/models/ci/build_spec.rb20
3 files changed, 37 insertions, 2 deletions
diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb
index 84010e40ef4..6b2b7e77180 100644
--- a/app/models/ci/build.rb
+++ b/app/models/ci/build.rb
@@ -48,13 +48,23 @@ 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.
+ # (See more https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/22380)
#
+ # 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
diff --git a/changelogs/unreleased/use-deployment-relation-to-fetch-environment-ce.yml b/changelogs/unreleased/use-deployment-relation-to-fetch-environment-ce.yml
new file mode 100644
index 00000000000..1ec276b4abc
--- /dev/null
+++ b/changelogs/unreleased/use-deployment-relation-to-fetch-environment-ce.yml
@@ -0,0 +1,5 @@
+---
+title: Use deployment relation to get an environment name
+merge_request: 24890
+author:
+type: performance
diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb
index 8a1bbb26e57..47865e4d08f 100644
--- a/spec/models/ci/build_spec.rb
+++ b/spec/models/ci/build_spec.rb
@@ -1844,6 +1844,26 @@ describe Ci::Build do
context 'when there is no environment' do
it { is_expected.to be_nil }
end
+
+ context 'when build has a start environment' do
+ let(:build) { create(:ci_build, :deploy_to_production, pipeline: pipeline) }
+
+ it 'does not expand environment name' do
+ expect(build).not_to receive(:expanded_environment_name)
+
+ subject
+ end
+ end
+
+ context 'when build has a stop environment' do
+ let(:build) { create(:ci_build, :stop_review_app, pipeline: pipeline) }
+
+ it 'expands environment name' do
+ expect(build).to receive(:expanded_environment_name)
+
+ subject
+ end
+ end
end
describe '#play' do