diff options
-rw-r--r-- | lib/tasks/build_site.rake | 2 | ||||
-rw-r--r-- | lib/tasks/task_helpers.rb | 73 | ||||
-rw-r--r-- | spec/lib/task_helpers_spec.rb | 14 |
3 files changed, 52 insertions, 37 deletions
diff --git a/lib/tasks/build_site.rake b/lib/tasks/build_site.rake index 9d32bbf94..7b1fdadf6 100644 --- a/lib/tasks/build_site.rake +++ b/lib/tasks/build_site.rake @@ -21,7 +21,7 @@ task :clone_repositories do && branch == ENV['CI_DEFAULT_BRANCH'] \ && ENV["CI_PIPELINE_SOURCE"] == 'pipeline' - puts "\n#{TaskHelpers::COLOR_CODE_GREEN}INFO: Cloning #{product['repo']}..#{TaskHelpers::COLOR_CODE_RESET}" + puts "\n#{TaskHelpers::COLOR_CODE_GREEN}INFO: Cloning branch '#{branch}' of #{product['repo']}..#{TaskHelpers::COLOR_CODE_RESET}" # # Handle the cases where we land on a runner that already ran a docs build, diff --git a/lib/tasks/task_helpers.rb b/lib/tasks/task_helpers.rb index bf5e54ac3..fd64afe64 100644 --- a/lib/tasks/task_helpers.rb +++ b/lib/tasks/task_helpers.rb @@ -28,38 +28,32 @@ class TaskHelpers end def retrieve_branch(slug) - # If CI_COMMIT_REF_NAME is not defined (run locally), set it to the default branch. - if ENV["CI_COMMIT_REF_NAME"].nil? - default_branch(products[slug].fetch('repo')) - - # If we're on a gitlab-docs stable branch according to the regex, catch the - # version and assign the product stable branches correctly. - elsif version = ENV["CI_COMMIT_REF_NAME"].match(VERSION_FORMAT) + # + # If we're NOT on a gitlab-docs stable branch, fetch the BRANCH_* environment + # variable, and if not assigned, set to the default branch. + # + return ENV.fetch("BRANCH_#{slug.upcase}", default_branch(products[slug].fetch('repo'))) if stable_branch_name.nil? - case slug - # EE has different branch name scheme - when 'ee' - "#{version[:major]}-#{version[:minor]}-stable-ee" + # + # Check the project slug to determine the branch name + # + case slug - when 'omnibus', 'runner' - "#{version[:major]}-#{version[:minor]}-stable" + when 'ee' + "#{stable_branch_name}-ee" - # Charts don't use the same version scheme as GitLab, we need to - # deduct their version from the GitLab equivalent one. - when 'charts' - chart = chart_version(ENV.fetch('CI_COMMIT_REF_NAME', nil)).match(VERSION_FORMAT) - "#{chart[:major]}-#{chart[:minor]}-stable" + when 'omnibus', 'runner' + stable_branch_name - # If the upstream product doesn't follow a stable branch scheme, set the - # branch to the default - else - default_branch(products[slug].fetch('repo')) - end + # Charts don't use the same version scheme as GitLab, we need to + # deduct their version from the GitLab equivalent one. + when 'charts' + charts_stable_branch - # If we're NOT on a gitlab-docs stable branch, fetch the BRANCH_* environment - # variable, and if not assigned, set to the default branch. + # If the upstream product doesn't follow a stable branch scheme, set the + # branch to the default else - ENV.fetch("BRANCH_#{slug.upcase}", default_branch(products[slug].fetch('repo'))) + default_branch(products[slug].fetch('repo')) end end @@ -72,6 +66,27 @@ class TaskHelpers end # + # If we're on a gitlab-docs stable branch (or targeting one) according to the + # regex, catch the version and return the branch name. + # For example, 15-8-stable. + # + # 1. Skip if CI_COMMIT_REF_NAME is not defined (run outside the CI environment). + # 2. If CI_COMMIT_REF_NAME matches the version format it means we're on a + # stable branch. Return the version format of that branch. + # 3. If CI_MERGE_REQUEST_TARGET_BRANCH_NAME is defined and its value matches + # the version format, return that value. + # + def stable_branch_name + @stable_branch_name ||= begin + ref_name = ENV["CI_COMMIT_REF_NAME"]&.match(VERSION_FORMAT) + return "#{ref_name[:major]}-#{ref_name[:minor]}-stable" if ref_name + + mr_name = ENV["CI_MERGE_REQUEST_TARGET_BRANCH_NAME"]&.match(VERSION_FORMAT) + "#{mr_name[:major]}-#{mr_name[:minor]}-stable" if mr_name + end + end + + # # The charts versions do not follow the same GitLab major number, BUT # they do follow a pattern https://docs.gitlab.com/charts/installation/version_mappings.html: # @@ -81,8 +96,8 @@ class TaskHelpers # This means we can deduct the charts version from the GitLab version, since # the major charts version is always 9 versions behind its GitLab counterpart. # - def chart_version(gitlab_version) - major, minor = gitlab_version.split('.') + def charts_stable_branch + major, minor = stable_branch_name.split('-') # Assume major charts version is nine less than major GitLab version. # If this breaks and the version isn't found, it might be because they @@ -90,7 +105,7 @@ class TaskHelpers # about it. major = major.to_i - 9 - "#{major}.#{minor}" + "#{major}-#{minor}-stable" end def default_branch(repo) diff --git a/spec/lib/task_helpers_spec.rb b/spec/lib/task_helpers_spec.rb index 2a5ada7e2..3ccd87a60 100644 --- a/spec/lib/task_helpers_spec.rb +++ b/spec/lib/task_helpers_spec.rb @@ -13,16 +13,16 @@ describe TaskHelpers do end end - describe '#chart_version' do - let(:gitlab_version) { nil } + describe '#charts_stable_branch' do + subject(:charts_stable_branch) { described_class.new.charts_stable_branch } - subject(:chart_version) { described_class.new.chart_version(gitlab_version) } + context 'when GitLab version is 15-0-stable' do + ENV['CI_COMMIT_REF_NAME'] = '15.0' - context 'when GitLab version is 15' do - let(:gitlab_version) { '15.0' } + let(:stable_branch_name) { '15-0-stable' } - it 'returns charts version 6.0' do - expect(chart_version).to eq('6.0') + it 'returns charts branch 6-0-stable' do + expect(charts_stable_branch).to eq('6-0-stable') end end end |