diff --git a/lib/gitlab/build/info.rb b/lib/gitlab/build/info.rb index 482e2a253920aa85d2f291b408b96280e6dabd4f_bGliL2dpdGxhYi9idWlsZC9pbmZvLnJi..16aac4bbe58f6e1d46cd21caaa76ae2e097deb74_bGliL2dpdGxhYi9idWlsZC9pbmZvLnJi 100644 --- a/lib/gitlab/build/info.rb +++ b/lib/gitlab/build/info.rb @@ -9,7 +9,6 @@ module Build class Info DEPLOYER_OS_MAPPING = { - 'AUTO_DEPLOY_ENVIRONMENT' => 'ubuntu-xenial', 'PATCH_DEPLOY_ENVIRONMENT' => 'ubuntu-bionic', 'RELEASE_DEPLOY_ENVIRONMENT' => 'ubuntu-focal', }.freeze @@ -36,9 +35,7 @@ end def deploy_env_key - if Build::Check.is_auto_deploy_tag? - 'AUTO_DEPLOY_ENVIRONMENT' - elsif Build::Check.is_rc_tag? + if Build::Check.is_rc_tag? 'PATCH_DEPLOY_ENVIRONMENT' elsif Build::Check.is_latest_stable_tag? 'RELEASE_DEPLOY_ENVIRONMENT' @@ -52,7 +49,7 @@ env = Gitlab::Util.get_env(key) - abort "Unable to determine which environment to deploy too, #{key} is empty" unless env + abort "Unable to determine which environment to deploy to, #{key} is empty" unless env puts "Ready to send trigger for environment(s): #{env}" diff --git a/lib/gitlab/tasks/gitlab_com.rake b/lib/gitlab/tasks/gitlab_com.rake index 482e2a253920aa85d2f291b408b96280e6dabd4f_bGliL2dpdGxhYi90YXNrcy9naXRsYWJfY29tLnJha2U=..16aac4bbe58f6e1d46cd21caaa76ae2e097deb74_bGliL2dpdGxhYi90YXNrcy9naXRsYWJfY29tLnJha2U= 100644 --- a/lib/gitlab/tasks/gitlab_com.rake +++ b/lib/gitlab/tasks/gitlab_com.rake @@ -1,2 +1,3 @@ +require_relative '../ohai_helper' require_relative '../deployer_helper.rb' require_relative "../util.rb" @@ -1,5 +2,6 @@ require_relative '../deployer_helper.rb' require_relative "../util.rb" +require_relative "../build/check" namespace :gitlab_com do desc 'Tasks related to gitlab.com.' @@ -8,7 +10,12 @@ unless Build::Info::Package.name == "gitlab-ee" puts "#{Build::Info::Package.name} is not an ee package, not doing anything." - exit + next + end + + if Build::Check.is_auto_deploy? + puts 'Auto-deploys are handled in release-tools, exiting...' + next end deploy_env = Build::Info.deploy_env @@ -12,12 +19,6 @@ end deploy_env = Build::Info.deploy_env - operating_systems = Build::Info::Package.file_list.map { |path| path.split("/")[1] }.uniq - - unless operating_systems.include?(Build::Info::DEPLOYER_OS_MAPPING[Build::Info.deploy_env_key]) - puts "Deployment to #{deploy_env} not to be triggered from this build (#{operating_systems.join(',')})." - exit - end if deploy_env.nil? puts 'Unable to determine which environment to deploy to, exiting...' @@ -21,7 +22,12 @@ if deploy_env.nil? puts 'Unable to determine which environment to deploy to, exiting...' - exit + next + elsif deploy_env == 'gprd' + # We do not support auto-deployments or triggered deployments + # directly to production from the omnibus pipeline, this check is here + # for safety + raise NotImplementedError, "Environment #{deploy_env} is not supported" end trigger_token = Gitlab::Util.get_env('DEPLOYER_TRIGGER_TOKEN') @@ -25,5 +31,7 @@ end trigger_token = Gitlab::Util.get_env('DEPLOYER_TRIGGER_TOKEN') - trigger_ref = Build::Check.is_auto_deploy? && Build::Check.ci_commit_tag? ? Gitlab::Util.get_env('CI_COMMIT_TAG') : :master + # DEPLOYER_TRIGGER_REF to be set to trigger pipelines against a reference + # other than `master` in the deployer project + trigger_ref = Gitlab::Util.get_env('DEPLOYER_TRIGGER_REF') || :master @@ -29,6 +37,8 @@ - if Build::Check.is_auto_deploy? - puts 'Auto-deploys are handled in release-tools, exiting...' - exit + current_os = OhaiHelper.fetch_os_with_codename[0..1].join("-") + os_for_deployment = Build::Info::DEPLOYER_OS_MAPPING[Build::Info.deploy_env_key] + if current_os != os_for_deployment + puts "Deployment to #{deploy_env} not to be triggered from this build (#{current_os})." + next end @@ -33,10 +43,5 @@ end - # We do not support auto-deployments or triggered deployments - # directly to production from the omnibus pipeline, this check is here - # for safety - raise NotImplementedError, "Environment #{deploy_env} is not supported" if deploy_env == 'gprd' - deployer_helper = DeployerHelper.new(trigger_token, deploy_env, trigger_ref) url = deployer_helper.trigger_deploy puts "Deployer build triggered at #{url} on #{trigger_ref} for the #{deploy_env} environment" diff --git a/spec/lib/gitlab/build/info_spec.rb b/spec/lib/gitlab/build/info_spec.rb index 482e2a253920aa85d2f291b408b96280e6dabd4f_c3BlYy9saWIvZ2l0bGFiL2J1aWxkL2luZm9fc3BlYy5yYg==..16aac4bbe58f6e1d46cd21caaa76ae2e097deb74_c3BlYy9saWIvZ2l0bGFiL2J1aWxkL2luZm9fc3BlYy5yYg== 100644 --- a/spec/lib/gitlab/build/info_spec.rb +++ b/spec/lib/gitlab/build/info_spec.rb @@ -11,8 +11,7 @@ describe '.deploy_env' do before do - allow(ENV).to receive(:[]).with('AUTO_DEPLOY_ENVIRONMENT').and_return('ad') allow(ENV).to receive(:[]).with('PATCH_DEPLOY_ENVIRONMENT').and_return('patch') allow(ENV).to receive(:[]).with('RELEASE_DEPLOY_ENVIRONMENT').and_return('r') end @@ -15,16 +14,7 @@ allow(ENV).to receive(:[]).with('PATCH_DEPLOY_ENVIRONMENT').and_return('patch') allow(ENV).to receive(:[]).with('RELEASE_DEPLOY_ENVIRONMENT').and_return('r') end - context 'on auto-deploy tag' do - before do - allow(Build::Check).to receive(:is_auto_deploy_tag?).and_return(true) - end - it 'returns the auto-deploy environment' do - expect(described_class.deploy_env).to eq('ad') - end - end - context 'on RC tag' do before do allow(Build::Check).to receive(:is_auto_deploy_tag?).and_return(false) diff --git a/spec/lib/gitlab/tasks/gitlab_com_spec.rb b/spec/lib/gitlab/tasks/gitlab_com_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..16aac4bbe58f6e1d46cd21caaa76ae2e097deb74_c3BlYy9saWIvZ2l0bGFiL3Rhc2tzL2dpdGxhYl9jb21fc3BlYy5yYg== --- /dev/null +++ b/spec/lib/gitlab/tasks/gitlab_com_spec.rb @@ -0,0 +1,154 @@ +require 'spec_helper' + +RSpec.describe 'gitlab_com', type: :rake do + before(:all) do + Rake.application.rake_require 'gitlab/tasks/gitlab_com' + end + + describe 'gitlab_com:deployer' do + before do + Rake::Task['gitlab_com:deployer'].reenable + + allow(ENV).to receive(:[]).and_call_original + stub_env_var('PATCH_DEPLOY_ENVIRONMENT', 'patch-environment') + stub_env_var('RELEASE_DEPLOY_ENVIRONMENT', 'release-environment') + allow(DeployerHelper).to receive(:new).and_return(double(trigger_deploy: 'dummy-url')) + end + + context 'when DEPLOYER_TRIGGER_TOKEN is not set' do + before do + stub_env_var('DEPLOYER_TRIGGER_TOKEN', nil) + end + + it 'prints warning' do + expect { Rake::Task['gitlab_com:deployer'].invoke }.to raise_error(SystemExit, "This task requires DEPLOYER_TRIGGER_TOKEN to be set") + end + end + + context 'when DEPLOYER_TRIGGER_TOKEN is set' do + before do + stub_env_var('DEPLOYER_TRIGGER_TOKEN', 'dummy-token') + end + + context 'when building Community Edition (CE)' do + before do + stub_is_ee(false) + end + + it 'prints warning' do + expect { Rake::Task['gitlab_com:deployer'].invoke }.to output(/gitlab-ce is not an ee package, not doing anything./).to_stdout + end + end + + context 'when building Enterprise Edition (EE)' do + before do + stub_is_ee(true) + end + + context 'with the auto-deploy tag' do + before do + allow(OhaiHelper).to receive(:fetch_os_with_codename).and_return(%w[ubuntu bionic]) + allow(Build::Check).to receive(:is_auto_deploy?).and_return(true) + end + + it 'shows a warning' do + expect { Rake::Task['gitlab_com:deployer'].invoke }.to output(/Auto-deploys are handled in release-tools, exiting.../).to_stdout + end + end + + context 'when running on Ubuntu 18.04' do + before do + allow(OhaiHelper).to receive(:fetch_os_with_codename).and_return(%w[ubuntu bionic]) + end + + context 'with a release candidate (RC) tag' do + before do + allow(Build::Check).to receive(:is_rc_tag?).and_return(true) + end + + it 'triggers deployment to specified environment' do + expect(DeployerHelper).to receive(:new).with('dummy-token', 'patch-environment', :master) + + Rake::Task['gitlab_com:deployer'].invoke + end + end + + context 'with a stable tag' do + before do + allow(Build::Check).to receive(:is_rc_tag?).and_return(false) + allow(Build::Check).to receive(:is_latest_stable_tag?).and_return(true) + end + + it 'does not trigger deployment' do + expect(DeployerHelper).not_to receive(:new) + + Rake::Task['gitlab_com:deployer'].invoke + end + end + end + + context 'when running on Ubuntu 20.04' do + before do + allow(OhaiHelper).to receive(:fetch_os_with_codename).and_return(%w[ubuntu focal]) + end + + context 'with a release candidate (RC) tag' do + before do + allow(Build::Check).to receive(:is_rc_tag?).and_return(true) + end + + it 'does not trigger deployment' do + expect(DeployerHelper).not_to receive(:new) + + Rake::Task['gitlab_com:deployer'].invoke + end + end + + context 'with a stable tag' do + before do + allow(Build::Check).to receive(:is_rc_tag?).and_return(false) + allow(Build::Check).to receive(:is_latest_stable_tag?).and_return(true) + end + + it 'triggers deployment to specified environment' do + expect(DeployerHelper).to receive(:new).with('dummy-token', 'release-environment', :master) + + Rake::Task['gitlab_com:deployer'].invoke + end + end + end + + context 'running on any other operating system' do + before do + allow(OhaiHelper).to receive(:fetch_os_with_codename).and_return(%w[debian buster]) + end + + context 'with a release candidate (RC) tag' do + before do + allow(Build::Check).to receive(:is_rc_tag?).and_return(true) + end + + it 'does not trigger deployment' do + expect(DeployerHelper).not_to receive(:new) + + Rake::Task['gitlab_com:deployer'].invoke + end + end + + context 'with a stable tag' do + before do + allow(Build::Check).to receive(:is_rc_tag?).and_return(false) + allow(Build::Check).to receive(:is_latest_stable_tag?).and_return(true) + end + + it 'does not trigger deployment' do + expect(DeployerHelper).not_to receive(:new) + + Rake::Task['gitlab_com:deployer'].invoke + end + end + end + end + end + end +end