Skip to content

Commit 853b0bb

Browse files
Robert Marshallbalasankarc
andcommitted
Merge branch 'split-info-gitlab-rails' into 'master'
Split GitLab Rails related information to own class See merge request https://gitlab.com/gitlab-org/omnibus-gitlab/-/merge_requests/7010 Merged-by: Robert Marshall <[email protected]> Approved-by: Clemens Beck <[email protected]> Approved-by: Robert Marshall <[email protected]> Reviewed-by: Robert Marshall <[email protected]> Reviewed-by: Clemens Beck <[email protected]> Co-authored-by: Balasankar "Balu" C <[email protected]>
2 parents f1984e1 + 727229e commit 853b0bb

File tree

9 files changed

+275
-255
lines changed

9 files changed

+275
-255
lines changed

config/software/git.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
end
9090

9191
env['GIT_REPO_URL'] = git_repo_url if git_repo_url
92-
elsif sm_version_override_git_repo_url && Regexp.new(sm_version_override_git_repo_url).match?(Build::Info.gitlab_version)
92+
elsif sm_version_override_git_repo_url && Regexp.new(sm_version_override_git_repo_url).match?(Build::Info::Components::GitLabRails.version)
9393
env['GIT_REPO_URL'] = git_repo_url if git_repo_url
9494
end
9595

lib/gitlab/build/info.rb

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,62 +19,8 @@ def docker_tag
1919
Gitlab::Util.get_env('IMAGE_TAG') || Build::Info::Package.release_version.tr('+', '-')
2020
end
2121

22-
def gitlab_version
23-
# Get the branch/version/commit of GitLab CE/EE repo against which package
24-
# is built. If GITLAB_VERSION variable is specified, as in triggered builds,
25-
# we use that. Else, we use the value in VERSION file.
26-
27-
if Gitlab::Util.get_env('GITLAB_VERSION').nil? || Gitlab::Util.get_env('GITLAB_VERSION').empty?
28-
File.read('VERSION').strip
29-
else
30-
Gitlab::Util.get_env('GITLAB_VERSION')
31-
end
32-
end
33-
34-
def gitlab_version_slug
35-
gitlab_version.downcase
36-
.gsub(/[^a-z0-9]/, '-')[0..62]
37-
.gsub(/(\A-+|-+\z)/, '')
38-
end
39-
40-
def gitlab_rails_ref(prepend_version: true)
41-
# Returns the immutable git ref of GitLab rails being used.
42-
#
43-
# 1. In feature branch pipelines, generate-facts job will create
44-
# version fact files which will contain the commit SHA of GitLab
45-
# rails. This will be used by `Gitlab::Version` class and will be
46-
# presented as version of `gitlab-rails` software component.
47-
# 2. In stable branch and tag pipelines, these version fact files will
48-
# not be created. However, in such cases, VERSION file will be
49-
# anyway pointing to immutable references (git tags), and hence we
50-
# can directly use it.
51-
Gitlab::Version.new('gitlab-rails').print(prepend_version)
52-
end
53-
54-
def gitlab_rails_project_path
55-
if Gitlab::Util.get_env('CI_SERVER_HOST') == 'dev.gitlab.org'
56-
Build::Info::Package.name == "gitlab-ee" ? 'gitlab/gitlab-ee' : 'gitlab/gitlabhq'
57-
else
58-
namespace = Gitlab::Version.security_channel? ? "gitlab-org/security" : "gitlab-org"
59-
project = Build::Info::Package.name == "gitlab-ee" ? 'gitlab' : 'gitlab-foss'
60-
61-
"#{namespace}/#{project}"
62-
end
63-
end
64-
65-
def gitlab_rails_repo
66-
gitlab_rails =
67-
if Build::Info::Package.name == "gitlab-ce"
68-
"gitlab-rails"
69-
else
70-
"gitlab-rails-ee"
71-
end
72-
73-
Gitlab::Version.new(gitlab_rails).remote
74-
end
75-
7622
def qa_image
77-
Gitlab::Util.get_env('QA_IMAGE') || "#{Gitlab::Util.get_env('CI_REGISTRY')}/#{gitlab_rails_project_path}/#{Build::Info::Package.name}-qa:#{gitlab_rails_ref(prepend_version: false)}"
23+
Gitlab::Util.get_env('QA_IMAGE') || "#{Gitlab::Util.get_env('CI_REGISTRY')}/#{Build::Info::Components::GitLabRails.project_path}/#{Build::Info::Package.name}-qa:#{Build::Info::Components::GitLabRails.ref(prepend_version: false)}"
7824
end
7925

8026
def release_bucket

lib/gitlab/build/info/components.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require_relative '../../util'
2+
require_relative '../check'
3+
4+
module Build
5+
class Info
6+
class Components
7+
class GitLabRails
8+
class << self
9+
def name
10+
Build::Info::Package.name == "gitlab-ce" ? "gitlab-rails" : "gitlab-rails-ee"
11+
end
12+
13+
def version
14+
# Get the branch/version/commit of GitLab CE/EE repo against which package
15+
# is built. If GITLAB_VERSION variable is specified, as in triggered builds,
16+
# we use that. Else, we use the value in VERSION file.
17+
18+
if Gitlab::Util.get_env('GITLAB_VERSION').nil? || Gitlab::Util.get_env('GITLAB_VERSION').empty?
19+
File.read('VERSION').strip
20+
else
21+
Gitlab::Util.get_env('GITLAB_VERSION')
22+
end
23+
end
24+
25+
def version_slug
26+
version.downcase
27+
.gsub(/[^a-z0-9]/, '-')[0..62]
28+
.gsub(/(\A-+|-+\z)/, '')
29+
end
30+
31+
def ref(prepend_version: true)
32+
# Returns the immutable git ref of GitLab rails being used.
33+
#
34+
# 1. In feature branch pipelines, generate-facts job will create
35+
# version fact files which will contain the commit SHA of GitLab
36+
# rails. This will be used by `Gitlab::Version` class and will be
37+
# presented as version of `gitlab-rails` software component.
38+
# 2. In stable branch and tag pipelines, these version fact files will
39+
# not be created. However, in such cases, VERSION file will be
40+
# anyway pointing to immutable references (git tags), and hence we
41+
# can directly use it.
42+
Gitlab::Version.new(name).print(prepend_version)
43+
end
44+
45+
def project_path
46+
if Gitlab::Util.get_env('CI_SERVER_HOST') == 'dev.gitlab.org'
47+
Build::Info::Package.name == "gitlab-ee" ? 'gitlab/gitlab-ee' : 'gitlab/gitlabhq'
48+
else
49+
namespace = Gitlab::Version.security_channel? ? "gitlab-org/security" : "gitlab-org"
50+
project = Build::Info::Package.name == "gitlab-ee" ? 'gitlab' : 'gitlab-foss'
51+
52+
"#{namespace}/#{project}"
53+
end
54+
end
55+
56+
def repo
57+
Gitlab::Version.new(name).remote
58+
end
59+
end
60+
end
61+
end
62+
end
63+
end

lib/gitlab/build/qa.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ def self.get_gitlab_repo
2121

2222
def self.clone_gitlab_rails
2323
system(*%W[rm -rf #{repo_path}])
24-
system(*%W[git clone #{Build::Info.gitlab_rails_repo} #{repo_path}])
24+
system(*%W[git clone #{Build::Info::Components::GitLabRails.repo} #{repo_path}])
2525
end
2626

2727
def self.checkout_gitlab_rails
2828
# Checking out the cloned repo to the specific commit (well, without doing
2929
# a to-and-fro `cd`).
30-
version = Build::Info.gitlab_rails_ref
30+
version = Build::Info::Components::GitLabRails.ref
3131

3232
puts "Building from #{Build::Info::Package.name} commit #{version}"
3333

lib/gitlab/tasks/qa.rake

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ require_relative '../build/qa'
44
require_relative '../build/check'
55
require_relative '../build/info'
66
require_relative "../build/info/git"
7+
require_relative "../build/info/components"
78
require_relative '../build/gitlab_image'
89
require_relative '../build/qa_image'
910
require_relative '../build/rat'
@@ -37,10 +38,10 @@ namespace :qa do
3738
desc "Copy stable version of gitlab-{ce,ee}-qa to the Omnibus registry and Docker Hub"
3839
task :stable do
3940
Gitlab::Util.section('qa:copy:stable') do
40-
# Using `Build::Info.gitlab_version` allows to have
41+
# Using `Build::Info::Components::GitLabRails.version` allows to have
4142
# gitlab/gitlab-{ce,ee}-qa:X.Y.Z-{ce,ee} without the build number, as
4243
# opposed to using something like `Build::Info::Package.release_version`.
43-
Build::QAImage.copy_image_to_dockerhub(Build::Info.gitlab_version)
44+
Build::QAImage.copy_image_to_dockerhub(Build::Info::Components::GitLabRails.version)
4445
end
4546
end
4647

@@ -64,7 +65,7 @@ namespace :qa do
6465
desc "Push unstable version of gitlab-{ce,ee}-qa to the GitLab registry"
6566
task :staging do
6667
Gitlab::Util.section('qa:push:staging') do
67-
Build::QAImage.tag_and_push_to_gitlab_registry(Build::Info.gitlab_version)
68+
Build::QAImage.tag_and_push_to_gitlab_registry(Build::Info::Components::GitLabRails.version)
6869
Build::QAImage.tag_and_push_to_gitlab_registry(Build::Info::Git.commit_sha)
6970
end
7071
end
@@ -73,8 +74,8 @@ namespace :qa do
7374
task :stable do
7475
Gitlab::Util.section('qa:push:stable') do
7576
# Allows to have gitlab/gitlab-{ce,ee}-qa:10.2.0-ee without the build number
76-
Build::QAImage.tag_and_push_to_gitlab_registry(Build::Info.gitlab_version)
77-
Build::QAImage.tag_and_push_to_dockerhub(Build::Info.gitlab_version, initial_tag: 'latest')
77+
Build::QAImage.tag_and_push_to_gitlab_registry(Build::Info::Components::GitLabRails.version)
78+
Build::QAImage.tag_and_push_to_dockerhub(Build::Info::Components::GitLabRails.version, initial_tag: 'latest')
7879
end
7980
end
8081

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
require 'spec_helper'
2+
require 'gitlab/build/info/components'
3+
4+
RSpec.describe Build::Info::Components::GitLabRails do
5+
before do
6+
stub_default_package_version
7+
stub_env_var('GITLAB_ALTERNATIVE_REPO', nil)
8+
stub_env_var('ALTERNATIVE_PRIVATE_TOKEN', nil)
9+
end
10+
11+
describe '.version' do
12+
describe 'GITLAB_VERSION variable specified' do
13+
it 'returns passed value' do
14+
allow(ENV).to receive(:[]).with("GITLAB_VERSION").and_return("9.0.0")
15+
expect(described_class.version).to eq('9.0.0')
16+
end
17+
end
18+
19+
describe 'GITLAB_VERSION variable not specified' do
20+
it 'returns content of VERSION' do
21+
allow(File).to receive(:read).with("VERSION").and_return("8.5.6")
22+
expect(described_class.version).to eq('8.5.6')
23+
end
24+
end
25+
end
26+
27+
describe '.ref' do
28+
context 'with prepend_version true' do
29+
context 'when on tags and stable branches' do
30+
# On stable branches and tags, generate-facts will not populate version facts
31+
# So, the content of the VERSION file will be used as-is.
32+
it 'returns tag with v prefix' do
33+
allow(File).to receive(:exist?).with(/gitlab-rails_version/).and_return(false)
34+
allow(File).to receive(:read).with(/VERSION/).and_return('15.7.0')
35+
expect(described_class.ref).to eq('v15.7.0')
36+
end
37+
end
38+
39+
context 'when on feature branches' do
40+
it 'returns commit SHA without any prefix' do
41+
allow(File).to receive(:exist?).with(/gitlab-rails_version/).and_return(true)
42+
allow(File).to receive(:read).with(/gitlab-rails_version/).and_return('arandomcommit')
43+
expect(described_class.ref).to eq('arandomcommit')
44+
end
45+
end
46+
end
47+
48+
context 'with prepend_version false' do
49+
context 'when on tags and stable branches' do
50+
# On stable branches and tags, generate-facts will not populate version facts
51+
# So, whatever is on VERSION file, will be used.
52+
it 'returns tag without v prefix' do
53+
allow(File).to receive(:exist?).with(/gitlab-rails_version/).and_return(false)
54+
allow(File).to receive(:read).with(/VERSION/).and_return('15.7.0')
55+
expect(described_class.ref(prepend_version: false)).to eq('15.7.0')
56+
end
57+
end
58+
59+
context 'when on feature branches' do
60+
it 'returns commit SHA without any prefix' do
61+
allow(File).to receive(:exist?).with(/gitlab-rails_version/).and_return(true)
62+
allow(File).to receive(:read).with(/gitlab-rails_version/).and_return('arandomcommit')
63+
expect(described_class.ref(prepend_version: false)).to eq('arandomcommit')
64+
end
65+
end
66+
end
67+
end
68+
69+
describe '.repo' do
70+
context 'when alternative sources channel selected' do
71+
before do
72+
allow(::Gitlab::Version).to receive(:sources_channel).and_return('alternative')
73+
end
74+
75+
it 'returns public mirror for GitLab CE' do
76+
allow(Build::Info::Package).to receive(:name).and_return("gitlab-ce")
77+
expect(described_class.repo).to eq("https://gitlab.com/gitlab-org/gitlab-foss.git")
78+
end
79+
80+
it 'returns public mirror for GitLab EE' do
81+
allow(Build::Info::Package).to receive(:name).and_return("gitlab-ee")
82+
expect(described_class.repo).to eq("https://gitlab.com/gitlab-org/gitlab.git")
83+
end
84+
end
85+
86+
context 'when default sources channel' do
87+
before do
88+
allow(::Gitlab::Version).to receive(:sources_channel).and_return('remote')
89+
end
90+
91+
it 'returns dev repo for GitLab CE' do
92+
allow(Build::Info::Package).to receive(:name).and_return("gitlab-ce")
93+
expect(described_class.repo).to eq("[email protected]:gitlab/gitlabhq.git")
94+
end
95+
96+
it 'returns dev repo for GitLab EE' do
97+
allow(Build::Info::Package).to receive(:name).and_return("gitlab-ee")
98+
expect(described_class.repo).to eq("[email protected]:gitlab/gitlab-ee.git")
99+
end
100+
end
101+
102+
context 'when security sources channel selected' do
103+
before do
104+
allow(::Gitlab::Version).to receive(:sources_channel).and_return('security')
105+
stub_env_var('CI_JOB_TOKEN', 'CJT')
106+
end
107+
108+
it 'returns security mirror for GitLab CE with attached credential' do
109+
allow(Build::Info::Package).to receive(:name).and_return("gitlab-ce")
110+
expect(described_class.repo).to eq("https://gitlab-ci-token:[email protected]/gitlab-org/security/gitlab-foss.git")
111+
end
112+
it 'returns security mirror for GitLab EE with attached credential' do
113+
allow(Build::Info::Package).to receive(:name).and_return("gitlab-ee")
114+
expect(described_class.repo).to eq("https://gitlab-ci-token:[email protected]/gitlab-org/security/gitlab.git")
115+
end
116+
end
117+
end
118+
119+
describe '.project_path' do
120+
context 'when building CE' do
121+
before do
122+
stub_is_ee(false)
123+
end
124+
125+
context 'when on the build mirror' do
126+
before do
127+
stub_env_var('CI_SERVER_HOST', 'dev.gitlab.org')
128+
stub_env_var('SECURITY_SOURCES', '')
129+
end
130+
131+
it 'returns correct path for GitLab rails project' do
132+
expect(described_class.project_path).to eq("gitlab/gitlabhq")
133+
end
134+
end
135+
136+
context 'when on running on the canonical project or QA mirror' do
137+
before do
138+
stub_env_var('CI_SERVER_HOST', 'gitlab.com')
139+
stub_env_var('SECURITY_SOURCES', '')
140+
end
141+
142+
it 'returns correct path for GitLab rails project' do
143+
expect(described_class.project_path).to eq("gitlab-org/gitlab-foss")
144+
end
145+
end
146+
147+
context 'when running on the security mirror' do
148+
before do
149+
stub_env_var('CI_SERVER_HOST', 'gitlab.com')
150+
stub_env_var('SECURITY_SOURCES', 'true')
151+
end
152+
153+
it 'returns correct path for GitLab rails project' do
154+
expect(described_class.project_path).to eq("gitlab-org/security/gitlab-foss")
155+
end
156+
end
157+
end
158+
159+
context 'when building EE' do
160+
before do
161+
stub_is_ee(true)
162+
end
163+
164+
context 'when running on the build mirror' do
165+
before do
166+
stub_env_var('CI_SERVER_HOST', 'dev.gitlab.org')
167+
stub_env_var('SECURITY_SOURCES', '')
168+
end
169+
170+
it 'returns correct path for GitLab rails project' do
171+
expect(described_class.project_path).to eq("gitlab/gitlab-ee")
172+
end
173+
end
174+
175+
context 'when running on the canonical project or QA mirror' do
176+
before do
177+
stub_env_var('CI_SERVER_HOST', 'gitlab.com')
178+
stub_env_var('SECURITY_SOURCES', '')
179+
end
180+
181+
it 'returns correct path for GitLab rails project' do
182+
expect(described_class.project_path).to eq("gitlab-org/gitlab")
183+
end
184+
end
185+
186+
context 'when running on the security mirror' do
187+
before do
188+
stub_env_var('CI_SERVER_HOST', 'gitlab.com')
189+
stub_env_var('SECURITY_SOURCES', 'true')
190+
end
191+
192+
it 'returns correct path for GitLab rails project' do
193+
expect(described_class.project_path).to eq("gitlab-org/security/gitlab")
194+
end
195+
end
196+
end
197+
end
198+
end

0 commit comments

Comments
 (0)