Skip to content

Commit 9ff9301

Browse files
twk3balasankarc
andcommitted
Merge branch 'split-info-git' into 'master'
Split Git related information to own class See merge request https://gitlab.com/gitlab-org/omnibus-gitlab/-/merge_requests/7008 Merged-by: DJ Mountney <[email protected]> Approved-by: Andrew Patterson <[email protected]> Approved-by: DJ Mountney <[email protected]> Co-authored-by: Balasankar "Balu" C <[email protected]>
2 parents f39fae6 + 258569b commit 9ff9301

File tree

13 files changed

+540
-373
lines changed

13 files changed

+540
-373
lines changed

lib/gitlab/build/check.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require_relative "info.rb"
2+
require_relative "info/git"
23
require_relative "../util.rb"
34

45
module Build
@@ -46,7 +47,7 @@ def is_auto_deploy?
4647
end
4748

4849
def is_auto_deploy_tag?
49-
AUTO_DEPLOY_TAG_REGEX.match?(Build::Info.current_git_tag)
50+
AUTO_DEPLOY_TAG_REGEX.match?(Build::Info::Git.tag_name)
5051
end
5152

5253
def is_auto_deploy_branch?
@@ -59,19 +60,19 @@ def is_patch_release?
5960
end
6061

6162
def is_rc_tag?
62-
Build::Info.current_git_tag.include?("+rc")
63+
Build::Info::Git.tag_name&.include?("+rc")
6364
end
6465

6566
def ci_commit_tag?
6667
Gitlab::Util.get_env('CI_COMMIT_TAG')
6768
end
6869

6970
def is_latest_stable_tag?
70-
match_tag?(Info.latest_stable_tag)
71+
match_tag?(Info::Git.latest_stable_tag)
7172
end
7273

7374
def is_latest_tag?
74-
match_tag?(Info.latest_tag)
75+
match_tag?(Info::Git.latest_tag)
7576
end
7677

7778
def is_nightly?
@@ -91,11 +92,11 @@ def on_regular_tag?
9192
end
9293

9394
def on_stable_branch?
94-
Build::Info.branch_name&.match?(/^\d+-\d+-stable$/)
95+
Build::Info::Git.branch_name&.match?(/^\d+-\d+-stable$/)
9596
end
9697

9798
def on_regular_branch?
98-
Build::Info.branch_name && !on_stable_branch?
99+
Build::Info::Git.branch_name && !on_stable_branch?
99100
end
100101
end
101102
end

lib/gitlab/build/facts.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require_relative 'info/git'
2+
13
module Build
24
class Facts
35
class << self
@@ -12,7 +14,7 @@ def generate_tag_files
1214
:latest_stable_tag,
1315
:latest_tag
1416
].each do |fact|
15-
content = Build::Info.send(fact) # rubocop:disable GitlabSecurity/PublicSend
17+
content = Build::Info::Git.send(fact) # rubocop:disable GitlabSecurity/PublicSend
1618
File.write("build_facts/#{fact}", content) unless content.nil?
1719
end
1820
end
@@ -74,7 +76,7 @@ def qa_trigger_vars
7476
QA_IMAGE=#{Build::Info.qa_image}
7577
QA_TESTS=#{Gitlab::Util.get_env('QA_TESTS')}
7678
ALLURE_JOB_NAME=#{allure_job_name}-#{Build::Info.edition}
77-
GITLAB_SEMVER_VERSION=#{Build::Info.latest_stable_tag.tr('+', '-')}
79+
GITLAB_SEMVER_VERSION=#{Build::Info::Git.latest_stable_tag.tr('+', '-')}
7880
RAT_REFERENCE_ARCHITECTURE=#{Gitlab::Util.get_env('RAT_REFERENCE_ARCHITECTURE') || 'omnibus-gitlab-mrs'}
7981
RAT_FIPS_REFERENCE_ARCHITECTURE=#{Gitlab::Util.get_env('RAT_FIPS_REFERENCE_ARCHITECTURE') || 'omnibus-gitlab-mrs-fips-ubuntu'}
8082
RAT_PACKAGE_URL=#{Gitlab::Util.get_env('PACKAGE_URL') || Build::Info::CI.triggered_package_download_url(fips: false)}

lib/gitlab/build/info.rb

Lines changed: 3 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'omnibus'
22

3+
require_relative 'info/git'
34
require_relative '../build_iteration'
45
require_relative "../util.rb"
56
require_relative './info/ci'
@@ -16,13 +17,6 @@ class Info
1617
PACKAGE_GLOB = "pkg/**/*.{deb,rpm}".freeze
1718

1819
class << self
19-
def fetch_fact_from_file(fact)
20-
return unless File.exist?("build_facts/#{fact}")
21-
22-
content = File.read("build_facts/#{fact}").strip
23-
return content unless content.empty?
24-
end
25-
2620
def package
2721
return "gitlab-fips" if Check.use_system_ssl?
2822
return "gitlab-ee" if Check.is_ee?
@@ -46,86 +40,20 @@ def semver_version
4640
Omnibus.load_configuration('omnibus.rb')
4741
Omnibus::BuildVersion.semver
4842
else
49-
latest_git_tag = Info.latest_tag.strip
43+
latest_git_tag = Info::Git.latest_tag.strip
5044
latest_version = latest_git_tag && !latest_git_tag.empty? ? latest_git_tag[0, latest_git_tag.match("[+]").begin(0)] : '0.0.1'
51-
commit_sha = Build::Info.commit_sha
45+
commit_sha = Build::Info::Git.commit_sha
5246
ver_tag = "#{latest_version}+" + (Build::Check.is_nightly? ? "rnightly" : "rfbranch")
5347
ver_tag += ".fips" if Build::Check.use_system_ssl?
5448
[ver_tag, Gitlab::Util.get_env('CI_PIPELINE_ID'), commit_sha].compact.join('.')
5549
end
5650
end
5751

58-
def branch_name
59-
Gitlab::Util.get_env('CI_COMMIT_BRANCH')
60-
end
61-
62-
def commit_sha
63-
commit_sha_raw = Gitlab::Util.get_env('CI_COMMIT_SHA') || `git rev-parse HEAD`.strip
64-
commit_sha_raw[0, 8]
65-
end
66-
6752
def release_version
6853
semver = Info.semver_version
6954
"#{semver}-#{Gitlab::BuildIteration.new.build_iteration}"
7055
end
7156

72-
def sorted_tags_for_edition
73-
`git -c versionsort.prereleaseSuffix=rc tag -l '#{Info.tag_match_pattern}' --sort=-v:refname`.split("\n")
74-
end
75-
76-
# TODO, merge latest_tag with latest_stable_tag
77-
# TODO, add tests, needs a repo clone
78-
def latest_tag
79-
unless (fact_from_file = fetch_fact_from_file(__method__)).nil?
80-
return fact_from_file
81-
end
82-
83-
tags = sorted_tags_for_edition
84-
85-
return if tags.empty?
86-
87-
version = branch_name.delete_suffix('-stable').tr('-', '.') if Build::Check.on_stable_branch?
88-
output = tags.find { |t| t.start_with?(version) } if version
89-
90-
# If no tags corresponding to the stable branch version was found, we
91-
# fall back to the latest available tag
92-
output || tags.first
93-
end
94-
95-
def latest_stable_tag(level: 1)
96-
unless (fact_from_file = fetch_fact_from_file(__method__)).nil?
97-
return fact_from_file
98-
end
99-
100-
# Exclude RC tags so that we only have stable tags.
101-
stable_tags = sorted_tags_for_edition.reject { |t| t.include?('rc') }
102-
103-
return if stable_tags.empty?
104-
105-
version = branch_name.delete_suffix('-stable').tr('-', '.') if Build::Check.on_stable_branch?
106-
107-
results = stable_tags.select { |t| t.start_with?(version) } if version
108-
109-
# If no tags corresponding to the stable branch version was found, we
110-
# fall back to the latest available stable tag
111-
output = if results.nil? || results.empty?
112-
stable_tags
113-
else
114-
results
115-
end
116-
117-
# Level decides tag at which position you want. Level one gives you
118-
# latest stable tag, two gives you the one just before it and so on.
119-
# Since arrays start from 0, we subtract 1 from the specified level to
120-
# get the index. If the specified level is more than the number of
121-
# tags, we return the last tag.
122-
if level >= output.length
123-
output.last
124-
else
125-
output[level - 1]
126-
end
127-
end
128-
12957
def docker_tag
13058
Gitlab::Util.get_env('IMAGE_TAG') || Info.release_version.tr('+', '-')
13159
end
@@ -162,12 +90,6 @@ def gitlab_rails_ref(prepend_version: true)
16290
Gitlab::Version.new('gitlab-rails').print(prepend_version)
16391
end
16492

165-
def previous_version
166-
# Get the second latest git tag
167-
previous_tag = Info.latest_stable_tag(level: 2)
168-
previous_tag.tr("+", "-")
169-
end
170-
17193
def gitlab_rails_project_path
17294
if Gitlab::Util.get_env('CI_SERVER_HOST') == 'dev.gitlab.org'
17395
package == "gitlab-ee" ? 'gitlab/gitlab-ee' : 'gitlab/gitlabhq'
@@ -243,12 +165,6 @@ def ami_deb_package_download_url(arch: 'amd64')
243165
"https://#{Info.release_bucket}.#{Info.release_bucket_s3_endpoint}/#{folder}/#{Info.package}_#{package_filename_url_safe}_#{arch}.deb"
244166
end
245167

246-
def tag_match_pattern
247-
return '*[+.]ee.*' if Check.is_ee?
248-
249-
'*[+.]ce.*'
250-
end
251-
252168
def release_file_contents
253169
repo = Gitlab::Util.get_env('PACKAGECLOUD_REPO') # Target repository
254170

@@ -269,10 +185,6 @@ def release_file_contents
269185
contents.join
270186
end
271187

272-
def current_git_tag
273-
`git describe --exact-match 2>/dev/null`.chomp
274-
end
275-
276188
def image_reference
277189
"#{Build::GitlabImage.gitlab_registry_image_address}:#{Info.docker_tag}"
278190
end

lib/gitlab/build/info/git.rb

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
require_relative '../../util'
2+
require_relative '../check'
3+
require_relative 'ci'
4+
5+
module Build
6+
class Info
7+
class Git
8+
class << self
9+
def branch_name
10+
# If on CI, branch name from `CI_COMMIT_BRANCH` wins
11+
result = Build::Info::CI.branch_name
12+
13+
return result if result
14+
15+
# If not on CI, attempt to detect branch name
16+
head_reference = Gitlab::Util.shellout_stdout('git rev-parse --abbrev-ref HEAD')
17+
18+
# On tags, the shell command will return `HEAD`. If that is not the
19+
# case, we are on a branch and can return the output we received.
20+
return head_reference unless head_reference == "HEAD"
21+
end
22+
23+
def tag_name
24+
Build::Info::CI.tag_name || Gitlab::Util.shellout_stdout('git describe --tags --exact-match')
25+
rescue Gitlab::Util::ShellOutExecutionError => e
26+
return nil if /fatal: no tag exactly matches/.match?(e.stderr)
27+
28+
raise "#{e.message}\nSTDOUT: #{e.stdout}\nSTDERR: #{e.stderr}"
29+
end
30+
31+
def commit_sha
32+
commit_sha_raw = Gitlab::Util.get_env('CI_COMMIT_SHA') || Gitlab::Util.shellout_stdout('git rev-parse HEAD')
33+
34+
commit_sha_raw[0, 8]
35+
end
36+
37+
# TODO, merge latest_tag with latest_stable_tag
38+
# TODO, add tests, needs a repo clone
39+
def latest_tag
40+
unless (fact_from_file = Gitlab::Util.fetch_fact_from_file(__method__)).nil?
41+
return fact_from_file
42+
end
43+
44+
tags = sorted_tags_for_edition
45+
46+
return if tags.empty?
47+
48+
version = branch_name.delete_suffix('-stable').tr('-', '.') if Build::Check.on_stable_branch?
49+
output = tags.find { |t| t.start_with?(version) } if version
50+
51+
# If no tags corresponding to the stable branch version was found, we
52+
# fall back to the latest available tag
53+
output || tags.first
54+
end
55+
56+
def latest_stable_tag(level: 1)
57+
unless (fact_from_file = Gitlab::Util.fetch_fact_from_file(__method__)).nil?
58+
return fact_from_file
59+
end
60+
61+
# Exclude RC tags so that we only have stable tags.
62+
stable_tags = sorted_tags_for_edition.reject { |t| t.include?('rc') }
63+
64+
return if stable_tags.empty?
65+
66+
version = branch_name.delete_suffix('-stable').tr('-', '.') if Build::Check.on_stable_branch?
67+
68+
results = stable_tags.select { |t| t.start_with?(version) } if version
69+
70+
# If no tags corresponding to the stable branch version was found, we
71+
# fall back to the latest available stable tag
72+
output = if results.nil? || results.empty?
73+
stable_tags
74+
else
75+
results
76+
end
77+
78+
# Level decides tag at which position you want. Level one gives you
79+
# latest stable tag, two gives you the one just before it and so on.
80+
# Since arrays start from 0, we subtract 1 from the specified level to
81+
# get the index. If the specified level is more than the number of
82+
# tags, we return the last tag.
83+
if level >= output.length
84+
output.last
85+
else
86+
output[level - 1]
87+
end
88+
end
89+
90+
private
91+
92+
def sorted_tags_for_edition
93+
Gitlab::Util.shellout_stdout("git -c versionsort.prereleaseSuffix=rc tag -l '#{tag_match_pattern}' --sort=-v:refname")&.split("\n")
94+
end
95+
96+
def tag_match_pattern
97+
return '*[+.]ee.*' if Build::Check.is_ee?
98+
99+
'*[+.]ce.*'
100+
end
101+
end
102+
end
103+
end
104+
end

lib/gitlab/tasks/build.rake

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ require 'fileutils'
22
require_relative "../build.rb"
33
require_relative "../build/check.rb"
44
require_relative "../build/info.rb"
5+
require_relative "../build/info/git"
56
require_relative '../build/facts'
67
require_relative "../gcloud_helper.rb"
78
require_relative "../ohai_helper.rb"
@@ -27,12 +28,12 @@ namespace :build do
2728
namespace :docker do
2829
desc 'Show latest available tag. Includes unstable releases.'
2930
task :latest_tag do
30-
puts Build::Info.latest_tag
31+
puts Build::Info::Git.latest_tag
3132
end
3233

3334
desc 'Show latest stable tag.'
3435
task :latest_stable_tag do
35-
puts Build::Info.latest_stable_tag
36+
puts Build::Info::Git.latest_stable_tag
3637
end
3738
end
3839

lib/gitlab/tasks/qa.rake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ require_relative '../docker_operations'
33
require_relative '../build/qa'
44
require_relative '../build/check'
55
require_relative '../build/info'
6+
require_relative "../build/info/git"
67
require_relative '../build/gitlab_image'
78
require_relative '../build/qa_image'
89
require_relative '../build/rat'
@@ -64,7 +65,7 @@ namespace :qa do
6465
task :staging do
6566
Gitlab::Util.section('qa:push:staging') do
6667
Build::QAImage.tag_and_push_to_gitlab_registry(Build::Info.gitlab_version)
67-
Build::QAImage.tag_and_push_to_gitlab_registry(Build::Info.commit_sha)
68+
Build::QAImage.tag_and_push_to_gitlab_registry(Build::Info::Git.commit_sha)
6869
end
6970
end
7071

0 commit comments

Comments
 (0)