Commit 0c063e8f authored by Shinya Maeda's avatar Shinya Maeda

Add a feature flag to try to redirect to latest template

This commit adds a feature flag for measuring impact of
overriding stable templates by the latest templates.
parent 8c9385c2
---
name: redirect_to_latest_template_jobs_browser_performance_testing
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/63144
rollout_issue_url:
milestone: '14.0'
type: development
group: group::pipeline authoring
default_enabled: false
---
name: redirect_to_latest_template_jobs_deploy
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/63144
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/332660
milestone: '14.0'
type: development
group: group::pipeline authoring
default_enabled: false
---
name: redirect_to_latest_template_security_api_fuzzing
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/63144
rollout_issue_url:
milestone: '14.0'
type: development
group: group::pipeline authoring
default_enabled: false
---
name: redirect_to_latest_template_security_dast
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/63144
rollout_issue_url:
milestone: '14.0'
type: development
group: group::pipeline authoring
default_enabled: false
---
name: redirect_to_latest_template_terraform
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/63144
rollout_issue_url:
milestone: '14.0'
type: development
group: group::pipeline authoring
default_enabled: false
---
name: redirect_to_latest_template_verify_browser_performance
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/63144
rollout_issue_url:
milestone: '14.0'
type: development
group: group::pipeline authoring
default_enabled: false
......@@ -307,6 +307,22 @@ include:
- remote: https://gitlab.com/gitlab-org/gitlab/-/raw/v13.0.1-ee/lib/gitlab/ci/templates/Jobs/Deploy.gitlab-ci.yml
```
### Use a feature flag to rollout the latest template
At a GitLab major version update, we should renew [stable templates](#stable-version) by the corresponding [latest templates](#latest-version),
but in some cases, you'd want to gauge the impact at first, so that we can be
more confident that the change wouldn't require revert/rollback on production.
To do so, you can use the feature flag, named `redirect_to_latest_template_<name>`.
For example, you can redirect to the latest template in 25% of projects on `gitlab.com`, by the following chatops command:
```shell
/chatops run feature set redirect_to_latest_template_jobs_deploy 25 --actors
```
Once you're confident on the latest template, you should renew the stable template by the latest content,
remove the latest template and remove the corresponding feature flag.
### Further reading
There is an [open issue](https://gitlab.com/gitlab-org/gitlab/-/issues/17716) about
......
......@@ -6,6 +6,10 @@ module EE
module GitlabCiYmlTemplate
extend ActiveSupport::Concern
EE_TEMPLATES_WITH_LATEST_VERSION = {
'Verify/Browser-Performance' => true
}.freeze
class_methods do
extend ::Gitlab::Utils::Override
......@@ -26,6 +30,12 @@ module EE
'Security' => 'Security'
}
end
override :templates_with_latest_version
def templates_with_latest_version
@templates_with_latest_version ||=
super.merge(EE_TEMPLATES_WITH_LATEST_VERSION)
end
end
end
end
......
......@@ -11,6 +11,14 @@ RSpec.describe "CI YML Templates" do
Gitlab::Template::GitlabCiYmlTemplate.all.map(&:full_name)
end
before do
stub_feature_flags(
redirect_to_latest_template_terraform: false,
redirect_to_latest_template_security_dast: false,
redirect_to_latest_template_security_api_fuzzing: false,
redirect_to_latest_template_jobs_browser_performance_testing: false)
end
with_them do
let(:content) do
if template_name == 'Security/DAST-API.gitlab-ci.yml'
......
......@@ -34,6 +34,7 @@ RSpec.describe 'Jobs/Browser-Performance-Testing.gitlab-ci.yml' do
before do
stub_ci_pipeline_yaml_file(template)
stub_feature_flags(redirect_to_latest_template_jobs_browser_performance_testing: false)
allow_next_instance_of(Ci::BuildScheduleWorker) do |worker|
allow(worker).to receive(:perform).and_return(true)
......
......@@ -30,6 +30,7 @@ RSpec.describe 'Verify/Browser-Performance.gitlab-ci.yml' do
before do
stub_ci_pipeline_yaml_file(template)
stub_feature_flags(redirect_to_latest_template_verify_browser_performance: false)
allow_next_instance_of(Ci::BuildScheduleWorker) do |worker|
allow(worker).to receive(:perform).and_return(true)
end
......
......@@ -6,7 +6,7 @@ module Gitlab
module External
module File
class Template < Base
attr_reader :location, :project
attr_reader :location
SUFFIX = '.gitlab-ci.yml'
......@@ -41,7 +41,7 @@ module Gitlab
end
def fetch_template_content
Gitlab::Template::GitlabCiYmlTemplate.find(template_name, project)&.content
Gitlab::Template::GitlabCiYmlTemplate.find(template_name, context.project)&.content
end
end
end
......
......@@ -5,6 +5,14 @@ module Gitlab
class GitlabCiYmlTemplate < BaseTemplate
BASE_EXCLUDED_PATTERNS = [%r{\.latest\.}].freeze
TEMPLATES_WITH_LATEST_VERSION = {
'Jobs/Deploy' => true,
'Jobs/Browser-Performance-Testing' => true,
'Security/API-Fuzzing' => true,
'Security/DAST' => true,
'Terraform' => true
}.freeze
def description
"# This file is a template, and might need editing before it works on your project."
end
......@@ -54,6 +62,30 @@ module Gitlab
excluded_patterns: self.excluded_patterns
)
end
def find(key, project = nil)
if try_redirect_to_latest?(key, project)
key += '.latest'
end
super(key, project)
end
private
# To gauge the impact of the latest template,
# you can redirect the stable template to the latest template by enabling the feature flag.
# See https://docs.gitlab.com/ee/development/cicd/templates.html#versioning for more information.
def try_redirect_to_latest?(key, project)
return false unless templates_with_latest_version[key]
flag_name = "redirect_to_latest_template_#{key.underscore.tr('/', '_')}"
::Feature.enabled?(flag_name, project, default_enabled: :yaml)
end
def templates_with_latest_version
TEMPLATES_WITH_LATEST_VERSION
end
end
end
end
......
......@@ -20,6 +20,13 @@ RSpec.describe 'CI YML Templates' do
all_templates - excluded_templates
end
before do
stub_feature_flags(
redirect_to_latest_template_terraform: false,
redirect_to_latest_template_security_api_fuzzing: false,
redirect_to_latest_template_security_dast: false)
end
with_them do
let(:content) do
if template_name == 'Security/DAST-API.gitlab-ci.yml'
......
......@@ -21,6 +21,54 @@ RSpec.describe Gitlab::Template::GitlabCiYmlTemplate do
end
end
describe '.find' do
let_it_be(:project) { create(:project) }
described_class::TEMPLATES_WITH_LATEST_VERSION.keys.each do |key|
it "finds the latest template for #{key}" do
result = described_class.find(key, project)
expect(result.full_name).to eq("#{key}.latest.gitlab-ci.yml")
expect(result.content).to be_present
end
context 'when `redirect_to_latest_template` feature flag is disabled' do
before do
stub_feature_flags("redirect_to_latest_template_#{key.underscore.tr('/', '_')}".to_sym => false)
end
it "finds the stable template for #{key}" do
result = described_class.find(key, project)
expect(result.full_name).to eq("#{key}.gitlab-ci.yml")
expect(result.content).to be_present
end
end
context 'when `redirect_to_latest_template` feature flag is enabled on the project' do
before do
stub_feature_flags("redirect_to_latest_template_#{key.underscore.tr('/', '_')}".to_sym => project)
end
it "finds the latest template for #{key}" do
result = described_class.find(key, project)
expect(result.full_name).to eq("#{key}.latest.gitlab-ci.yml")
expect(result.content).to be_present
end
end
context 'when `redirect_to_latest_template` feature flag is enabled on the other project' do
before do
stub_feature_flags("redirect_to_latest_template_#{key.underscore.tr('/', '_')}".to_sym => create(:project))
end
it "finds the stable template for #{key}" do
result = described_class.find(key, project)
expect(result.full_name).to eq("#{key}.gitlab-ci.yml")
expect(result.content).to be_present
end
end
end
end
describe '#content' do
it 'loads the full file' do
gitignore = subject.new(Rails.root.join('lib/gitlab/ci/templates/Ruby.gitlab-ci.yml'))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment