Skip to content

Commit b7b1ae9

Browse files
balasankarcRobert Marshall
authored andcommitted
Refactor deploy rake task
- Drop auto-deploy pipeline code sections that are now handled by the Delivery team's co-ordinated pipelines - Remove code relevant to Ubuntu 16.04 whose support was dropped some time ago - Improves the deploy related rspec tests - Exit deploy code earlier when possible similar to how logic gates are handled in other rake tasks Signed-off-by: Balasankar "Balu" C <[email protected]>
1 parent 8766891 commit b7b1ae9

File tree

4 files changed

+179
-33
lines changed

4 files changed

+179
-33
lines changed

lib/gitlab/build/info.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
module Build
1010
class Info
1111
DEPLOYER_OS_MAPPING = {
12-
'AUTO_DEPLOY_ENVIRONMENT' => 'ubuntu-xenial',
1312
'PATCH_DEPLOY_ENVIRONMENT' => 'ubuntu-bionic',
1413
'RELEASE_DEPLOY_ENVIRONMENT' => 'ubuntu-focal',
1514
}.freeze
@@ -36,9 +35,7 @@ def log_level
3635
end
3736

3837
def deploy_env_key
39-
if Build::Check.is_auto_deploy_tag?
40-
'AUTO_DEPLOY_ENVIRONMENT'
41-
elsif Build::Check.is_rc_tag?
38+
if Build::Check.is_rc_tag?
4239
'PATCH_DEPLOY_ENVIRONMENT'
4340
elsif Build::Check.is_latest_stable_tag?
4441
'RELEASE_DEPLOY_ENVIRONMENT'
@@ -52,7 +49,7 @@ def deploy_env
5249

5350
env = Gitlab::Util.get_env(key)
5451

55-
abort "Unable to determine which environment to deploy too, #{key} is empty" unless env
52+
abort "Unable to determine which environment to deploy to, #{key} is empty" unless env
5653

5754
puts "Ready to send trigger for environment(s): #{env}"
5855

lib/gitlab/tasks/gitlab_com.rake

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
require_relative '../ohai_helper'
12
require_relative '../deployer_helper.rb'
23
require_relative "../util.rb"
4+
require_relative "../build/check"
35

46
namespace :gitlab_com do
57
desc 'Tasks related to gitlab.com.'
@@ -8,35 +10,38 @@ namespace :gitlab_com do
810

911
unless Build::Info::Package.name == "gitlab-ee"
1012
puts "#{Build::Info::Package.name} is not an ee package, not doing anything."
11-
exit
13+
next
1214
end
1315

14-
deploy_env = Build::Info.deploy_env
15-
operating_systems = Build::Info::Package.file_list.map { |path| path.split("/")[1] }.uniq
16-
17-
unless operating_systems.include?(Build::Info::DEPLOYER_OS_MAPPING[Build::Info.deploy_env_key])
18-
puts "Deployment to #{deploy_env} not to be triggered from this build (#{operating_systems.join(',')})."
19-
exit
16+
if Build::Check.is_auto_deploy?
17+
puts 'Auto-deploys are handled in release-tools, exiting...'
18+
next
2019
end
2120

21+
deploy_env = Build::Info.deploy_env
22+
2223
if deploy_env.nil?
2324
puts 'Unable to determine which environment to deploy to, exiting...'
24-
exit
25+
next
26+
elsif deploy_env == 'gprd'
27+
# We do not support auto-deployments or triggered deployments
28+
# directly to production from the omnibus pipeline, this check is here
29+
# for safety
30+
raise NotImplementedError, "Environment #{deploy_env} is not supported"
2531
end
2632

2733
trigger_token = Gitlab::Util.get_env('DEPLOYER_TRIGGER_TOKEN')
28-
trigger_ref = Build::Check.is_auto_deploy? && Build::Check.ci_commit_tag? ? Gitlab::Util.get_env('CI_COMMIT_TAG') : :master
29-
30-
if Build::Check.is_auto_deploy?
31-
puts 'Auto-deploys are handled in release-tools, exiting...'
32-
exit
34+
# DEPLOYER_TRIGGER_REF to be set to trigger pipelines against a reference
35+
# other than `master` in the deployer project
36+
trigger_ref = Gitlab::Util.get_env('DEPLOYER_TRIGGER_REF') || :master
37+
38+
current_os = OhaiHelper.fetch_os_with_codename[0..1].join("-")
39+
os_for_deployment = Build::Info::DEPLOYER_OS_MAPPING[Build::Info.deploy_env_key]
40+
if current_os != os_for_deployment
41+
puts "Deployment to #{deploy_env} not to be triggered from this build (#{current_os})."
42+
next
3343
end
3444

35-
# We do not support auto-deployments or triggered deployments
36-
# directly to production from the omnibus pipeline, this check is here
37-
# for safety
38-
raise NotImplementedError, "Environment #{deploy_env} is not supported" if deploy_env == 'gprd'
39-
4045
deployer_helper = DeployerHelper.new(trigger_token, deploy_env, trigger_ref)
4146
url = deployer_helper.trigger_deploy
4247
puts "Deployer build triggered at #{url} on #{trigger_ref} for the #{deploy_env} environment"

spec/lib/gitlab/build/info_spec.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,10 @@
1111

1212
describe '.deploy_env' do
1313
before do
14-
allow(ENV).to receive(:[]).with('AUTO_DEPLOY_ENVIRONMENT').and_return('ad')
1514
allow(ENV).to receive(:[]).with('PATCH_DEPLOY_ENVIRONMENT').and_return('patch')
1615
allow(ENV).to receive(:[]).with('RELEASE_DEPLOY_ENVIRONMENT').and_return('r')
1716
end
1817

19-
context 'on auto-deploy tag' do
20-
before do
21-
allow(Build::Check).to receive(:is_auto_deploy_tag?).and_return(true)
22-
end
23-
it 'returns the auto-deploy environment' do
24-
expect(described_class.deploy_env).to eq('ad')
25-
end
26-
end
27-
2818
context 'on RC tag' do
2919
before do
3020
allow(Build::Check).to receive(:is_auto_deploy_tag?).and_return(false)
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe 'gitlab_com', type: :rake do
4+
before(:all) do
5+
Rake.application.rake_require 'gitlab/tasks/gitlab_com'
6+
end
7+
8+
describe 'gitlab_com:deployer' do
9+
before do
10+
Rake::Task['gitlab_com:deployer'].reenable
11+
12+
allow(ENV).to receive(:[]).and_call_original
13+
stub_env_var('PATCH_DEPLOY_ENVIRONMENT', 'patch-environment')
14+
stub_env_var('RELEASE_DEPLOY_ENVIRONMENT', 'release-environment')
15+
allow(DeployerHelper).to receive(:new).and_return(double(trigger_deploy: 'dummy-url'))
16+
end
17+
18+
context 'when DEPLOYER_TRIGGER_TOKEN is not set' do
19+
before do
20+
stub_env_var('DEPLOYER_TRIGGER_TOKEN', nil)
21+
end
22+
23+
it 'prints warning' do
24+
expect { Rake::Task['gitlab_com:deployer'].invoke }.to raise_error(SystemExit, "This task requires DEPLOYER_TRIGGER_TOKEN to be set")
25+
end
26+
end
27+
28+
context 'when DEPLOYER_TRIGGER_TOKEN is set' do
29+
before do
30+
stub_env_var('DEPLOYER_TRIGGER_TOKEN', 'dummy-token')
31+
end
32+
33+
context 'when building Community Edition (CE)' do
34+
before do
35+
stub_is_ee(false)
36+
end
37+
38+
it 'prints warning' do
39+
expect { Rake::Task['gitlab_com:deployer'].invoke }.to output(/gitlab-ce is not an ee package, not doing anything./).to_stdout
40+
end
41+
end
42+
43+
context 'when building Enterprise Edition (EE)' do
44+
before do
45+
stub_is_ee(true)
46+
end
47+
48+
context 'with the auto-deploy tag' do
49+
before do
50+
allow(OhaiHelper).to receive(:fetch_os_with_codename).and_return(%w[ubuntu bionic])
51+
allow(Build::Check).to receive(:is_auto_deploy?).and_return(true)
52+
end
53+
54+
it 'shows a warning' do
55+
expect { Rake::Task['gitlab_com:deployer'].invoke }.to output(/Auto-deploys are handled in release-tools, exiting.../).to_stdout
56+
end
57+
end
58+
59+
context 'when running on Ubuntu 18.04' do
60+
before do
61+
allow(OhaiHelper).to receive(:fetch_os_with_codename).and_return(%w[ubuntu bionic])
62+
end
63+
64+
context 'with a release candidate (RC) tag' do
65+
before do
66+
allow(Build::Check).to receive(:is_rc_tag?).and_return(true)
67+
end
68+
69+
it 'triggers deployment to specified environment' do
70+
expect(DeployerHelper).to receive(:new).with('dummy-token', 'patch-environment', :master)
71+
72+
Rake::Task['gitlab_com:deployer'].invoke
73+
end
74+
end
75+
76+
context 'with a stable tag' do
77+
before do
78+
allow(Build::Check).to receive(:is_rc_tag?).and_return(false)
79+
allow(Build::Check).to receive(:is_latest_stable_tag?).and_return(true)
80+
end
81+
82+
it 'does not trigger deployment' do
83+
expect(DeployerHelper).not_to receive(:new)
84+
85+
Rake::Task['gitlab_com:deployer'].invoke
86+
end
87+
end
88+
end
89+
90+
context 'when running on Ubuntu 20.04' do
91+
before do
92+
allow(OhaiHelper).to receive(:fetch_os_with_codename).and_return(%w[ubuntu focal])
93+
end
94+
95+
context 'with a release candidate (RC) tag' do
96+
before do
97+
allow(Build::Check).to receive(:is_rc_tag?).and_return(true)
98+
end
99+
100+
it 'does not trigger deployment' do
101+
expect(DeployerHelper).not_to receive(:new)
102+
103+
Rake::Task['gitlab_com:deployer'].invoke
104+
end
105+
end
106+
107+
context 'with a stable tag' do
108+
before do
109+
allow(Build::Check).to receive(:is_rc_tag?).and_return(false)
110+
allow(Build::Check).to receive(:is_latest_stable_tag?).and_return(true)
111+
end
112+
113+
it 'triggers deployment to specified environment' do
114+
expect(DeployerHelper).to receive(:new).with('dummy-token', 'release-environment', :master)
115+
116+
Rake::Task['gitlab_com:deployer'].invoke
117+
end
118+
end
119+
end
120+
121+
context 'running on any other operating system' do
122+
before do
123+
allow(OhaiHelper).to receive(:fetch_os_with_codename).and_return(%w[debian buster])
124+
end
125+
126+
context 'with a release candidate (RC) tag' do
127+
before do
128+
allow(Build::Check).to receive(:is_rc_tag?).and_return(true)
129+
end
130+
131+
it 'does not trigger deployment' do
132+
expect(DeployerHelper).not_to receive(:new)
133+
134+
Rake::Task['gitlab_com:deployer'].invoke
135+
end
136+
end
137+
138+
context 'with a stable tag' do
139+
before do
140+
allow(Build::Check).to receive(:is_rc_tag?).and_return(false)
141+
allow(Build::Check).to receive(:is_latest_stable_tag?).and_return(true)
142+
end
143+
144+
it 'does not trigger deployment' do
145+
expect(DeployerHelper).not_to receive(:new)
146+
147+
Rake::Task['gitlab_com:deployer'].invoke
148+
end
149+
end
150+
end
151+
end
152+
end
153+
end
154+
end

0 commit comments

Comments
 (0)