diff --git a/changelogs/unreleased/require-all-templates-to-include-default-stages.yml b/changelogs/unreleased/require-all-templates-to-include-default-stages.yml new file mode 100644 index 0000000000000000000000000000000000000000..de8b07160bae74d5d49861bdb91ea2b25ea5c6da --- /dev/null +++ b/changelogs/unreleased/require-all-templates-to-include-default-stages.yml @@ -0,0 +1,5 @@ +--- +title: Require all templates to use default stages +merge_request: 26954 +author: +type: fixed diff --git a/lib/gitlab/ci/config.rb b/lib/gitlab/ci/config.rb index 15643fa03ac2d1ab008c31e9d1d790d240322320..f187e4569930e6fbf698a2ca7be9542b23917688 100644 --- a/lib/gitlab/ci/config.rb +++ b/lib/gitlab/ci/config.rb @@ -73,17 +73,13 @@ module Gitlab def build_config(config, project:, sha:, user:) initial_config = Gitlab::Config::Loader::Yaml.new(config).load! - if project - process_external_files(initial_config, project: project, sha: sha, user: user) - else - initial_config - end + process_external_files(initial_config, project: project, sha: sha, user: user) end def process_external_files(config, project:, sha:, user:) Config::External::Processor.new(config, project: project, - sha: sha || project.repository.root_ref_sha, + sha: sha || project&.repository&.root_ref_sha, user: user, expandset: Set.new).perform end diff --git a/lib/gitlab/ci/config/external/file/local.rb b/lib/gitlab/ci/config/external/file/local.rb index 229a06451e866943e4c799a642f6d34e8fb5d7f8..cac321ec4a6d8f37a2281e9a2ba349635b90b34d 100644 --- a/lib/gitlab/ci/config/external/file/local.rb +++ b/lib/gitlab/ci/config/external/file/local.rb @@ -21,7 +21,9 @@ module Gitlab private def validate_content! - if content.nil? + if context.project&.repository.nil? + errors.push("Local file `#{location}` does not have project!") + elsif content.nil? errors.push("Local file `#{location}` does not exist!") elsif content.blank? errors.push("Local file `#{location}` is empty!") diff --git a/lib/gitlab/ci/templates/Android-Fastlane.gitlab-ci.yml b/lib/gitlab/ci/templates/Android-Fastlane.gitlab-ci.yml index 8e767b223608d31bbb5067b7454881d69bf0f313..120272200c6fee2a8e1b556ca7e02f9612613d85 100644 --- a/lib/gitlab/ci/templates/Android-Fastlane.gitlab-ci.yml +++ b/lib/gitlab/ci/templates/Android-Fastlane.gitlab-ci.yml @@ -6,6 +6,7 @@ stages: - environment - build - test + - deploy - internal - alpha - beta diff --git a/lib/gitlab/ci/templates/Android.gitlab-ci.yml b/lib/gitlab/ci/templates/Android.gitlab-ci.yml index c169e3f768603f2c336a6c5e49202277276e57c7..2be7f9334625c629e5b9f426410e35e8503322d4 100644 --- a/lib/gitlab/ci/templates/Android.gitlab-ci.yml +++ b/lib/gitlab/ci/templates/Android.gitlab-ci.yml @@ -24,10 +24,6 @@ before_script: - yes | android-sdk-linux/tools/bin/sdkmanager --licenses - set -o pipefail -stages: - - build - - test - lintDebug: stage: build script: diff --git a/lib/gitlab/ci/templates/Auto-DevOps.gitlab-ci.yml b/lib/gitlab/ci/templates/Auto-DevOps.gitlab-ci.yml index b3eec2b62104b1661bfe1ec4004674d979e4b970..939112e6e410a41bbc4c4ad88b84a55b00a99ad2 100644 --- a/lib/gitlab/ci/templates/Auto-DevOps.gitlab-ci.yml +++ b/lib/gitlab/ci/templates/Auto-DevOps.gitlab-ci.yml @@ -60,6 +60,7 @@ variables: stages: - build - test + - deploy # dummy stage to follow the template guidelines - review - dast - staging diff --git a/lib/gitlab/ci/templates/Chef.gitlab-ci.yml b/lib/gitlab/ci/templates/Chef.gitlab-ci.yml index 33507aa58e4793f7bf790895b5c519e53ff4f102..1e14aa8bea94101de9941160cd2114d345f998ab 100644 --- a/lib/gitlab/ci/templates/Chef.gitlab-ci.yml +++ b/lib/gitlab/ci/templates/Chef.gitlab-ci.yml @@ -14,9 +14,11 @@ variables: KITCHEN_LOCAL_YAML: ".kitchen.dokken.yml" stages: + - build - lint - - unit + - test - functional + - deploy foodcritic: stage: lint @@ -29,7 +31,7 @@ cookstyle: - chef exec cookstyle . chefspec: - stage: unit + stage: test script: - chef exec rspec spec diff --git a/lib/gitlab/ci/templates/Go.gitlab-ci.yml b/lib/gitlab/ci/templates/Go.gitlab-ci.yml index d572d7a1edc951712b0757e548f482e1e58cae14..55fda1a4799786b055b991d305d880ebf48183f9 100644 --- a/lib/gitlab/ci/templates/Go.gitlab-ci.yml +++ b/lib/gitlab/ci/templates/Go.gitlab-ci.yml @@ -18,6 +18,7 @@ before_script: stages: - test - build + - deploy format: stage: test diff --git a/lib/gitlab/ci/templates/Mono.gitlab-ci.yml b/lib/gitlab/ci/templates/Mono.gitlab-ci.yml index 86d62b93313b5c1712daeb57be108516eda19d63..10fb6be6c3936e34a17783b13ea923e039ad825b 100644 --- a/lib/gitlab/ci/templates/Mono.gitlab-ci.yml +++ b/lib/gitlab/ci/templates/Mono.gitlab-ci.yml @@ -15,6 +15,7 @@ image: mono:latest stages: + - build - test - deploy diff --git a/lib/gitlab/ci/templates/OpenShift.gitlab-ci.yml b/lib/gitlab/ci/templates/OpenShift.gitlab-ci.yml index 290b9997084953e51a40961339661fb28469c80e..61a925e0d2dcd938dea160629baa1cd963bf5675 100644 --- a/lib/gitlab/ci/templates/OpenShift.gitlab-ci.yml +++ b/lib/gitlab/ci/templates/OpenShift.gitlab-ci.yml @@ -1,7 +1,9 @@ image: ayufan/openshift-cli stages: + - build # dummy stage to follow the template guidelines - test + - deploy # dummy stage to follow the template guidelines - review - staging - production diff --git a/lib/gitlab/ci/templates/Packer.gitlab-ci.yml b/lib/gitlab/ci/templates/Packer.gitlab-ci.yml index fa296057c72b53e02a695833c58040269374d8bd..83e179f37c30bf4dfbea28082bff4eed5c8b6365 100644 --- a/lib/gitlab/ci/templates/Packer.gitlab-ci.yml +++ b/lib/gitlab/ci/templates/Packer.gitlab-ci.yml @@ -9,6 +9,8 @@ before_script: stages: - validate + - build + - test - deploy validate: diff --git a/lib/gitlab/ci/templates/Serverless.gitlab-ci.yml b/lib/gitlab/ci/templates/Serverless.gitlab-ci.yml index 4c92dcb7941f8ad8cf496bbb5e897c6fde068e9f..0fb7c57ab728517fa4fd7aacf78ea2af550c8d05 100644 --- a/lib/gitlab/ci/templates/Serverless.gitlab-ci.yml +++ b/lib/gitlab/ci/templates/Serverless.gitlab-ci.yml @@ -4,6 +4,7 @@ image: alpine:latest stages: - build + - test - deploy .serverless:build:image: diff --git a/lib/gitlab/ci/templates/Swift.gitlab-ci.yml b/lib/gitlab/ci/templates/Swift.gitlab-ci.yml index ba8a802ba4f80129c370fafc594b23f7482226b2..9aa4abd47912146254fd499dec36c0a43054f59e 100644 --- a/lib/gitlab/ci/templates/Swift.gitlab-ci.yml +++ b/lib/gitlab/ci/templates/Swift.gitlab-ci.yml @@ -2,7 +2,9 @@ # This file assumes an own GitLab CI runner, setup on a macOS system. stages: - build + - test - archive + - deploy build_project: stage: build diff --git a/lib/gitlab/ci/templates/Terraform.gitlab-ci.yml b/lib/gitlab/ci/templates/Terraform.gitlab-ci.yml index 7160fce26a80cef033aabf9ddfb7a57238218790..f374bc7e26a80a77f2604e3f60fb7956193ec0a4 100644 --- a/lib/gitlab/ci/templates/Terraform.gitlab-ci.yml +++ b/lib/gitlab/ci/templates/Terraform.gitlab-ci.yml @@ -24,6 +24,7 @@ before_script: stages: - validate - build + - test - deploy validate: diff --git a/lib/gitlab/ci/templates/iOS-Fastlane.gitlab-ci.yml b/lib/gitlab/ci/templates/iOS-Fastlane.gitlab-ci.yml index df6ac4d340d3d30313dae6e83a49a44351cb2670..a4a9e96c1d25d833834a794def60a3fb62073c21 100644 --- a/lib/gitlab/ci/templates/iOS-Fastlane.gitlab-ci.yml +++ b/lib/gitlab/ci/templates/iOS-Fastlane.gitlab-ci.yml @@ -12,6 +12,8 @@ stages: - build + - test + - deploy variables: LC_ALL: "en_US.UTF-8" diff --git a/lib/gitlab/template/base_template.rb b/lib/gitlab/template/base_template.rb index 0b4cc571dc058992c91c6e9f6cb45d4fe74a8751..14c9a3e0389db63dba2990b912797a87701d80e2 100644 --- a/lib/gitlab/template/base_template.rb +++ b/lib/gitlab/template/base_template.rb @@ -16,6 +16,12 @@ module Gitlab end alias_method :key, :name + def full_name + Pathname.new(@path) + .relative_path_from(self.class.base_dir) + .to_s + end + def content @finder.read(@path) end diff --git a/spec/lib/gitlab/ci/templates/templates_spec.rb b/spec/lib/gitlab/ci/templates/templates_spec.rb index 4e3681cd943364361d39614ddc084e1550655e3e..b52064b303672cc7380f57c3537ad09d5fa2f08f 100644 --- a/spec/lib/gitlab/ci/templates/templates_spec.rb +++ b/spec/lib/gitlab/ci/templates/templates_spec.rb @@ -3,46 +3,32 @@ require 'spec_helper' describe "CI YML Templates" do - ABSTRACT_TEMPLATES = %w[Serverless].freeze - # These templates depend on the presence of the `project` - # param to enable processing of `include:` within CI config. - PROJECT_DEPENDENT_TEMPLATES = %w[Auto-DevOps DAST].freeze - - def self.concrete_templates - Gitlab::Template::GitlabCiYmlTemplate.all.reject do |template| - ABSTRACT_TEMPLATES.include?(template.name) - end - end + using RSpec::Parameterized::TableSyntax - def self.abstract_templates - Gitlab::Template::GitlabCiYmlTemplate.all.select do |template| - ABSTRACT_TEMPLATES.include?(template.name) - end + subject { Gitlab::Ci::YamlProcessor.new(content) } + + where(:template_name) do + Gitlab::Template::GitlabCiYmlTemplate.all.map(&:full_name) end - describe 'concrete templates with CI/CD jobs' do - concrete_templates.each do |template| - it "#{template.name} template should be valid" do - # Trigger processing of included files - project = create(:project, :test_repo) if PROJECT_DEPENDENT_TEMPLATES.include?(template.name) + with_them do + let(:content) do + <<~EOS + include: + - template: #{template_name} - expect { Gitlab::Ci::YamlProcessor.new(template.content, project: project) } - .not_to raise_error - end + concrete_build_implemented_by_a_user: + stage: test + script: do something + EOS + end + + it 'is valid' do + expect { subject }.not_to raise_error end - end - describe 'abstract templates without concrete jobs defined' do - abstract_templates.each do |template| - it "#{template.name} template should be valid after being implemented" do - content = template.content + <<~EOS - concrete_build_implemented_by_a_user: - stage: build - script: do something - EOS - - expect { Gitlab::Ci::YamlProcessor.new(content) }.not_to raise_error - end + it 'require default stages to be included' do + expect(subject.stages).to include(*Gitlab::Ci::Config::Entry::Stages.default) end end end diff --git a/spec/lib/gitlab/ci/yaml_processor_spec.rb b/spec/lib/gitlab/ci/yaml_processor_spec.rb index b7b30e60d44c92820de17313d0ad7e121bfa61a4..0d998d89d739cc9a7a1a0cbb58be020d5fdded2d 100644 --- a/spec/lib/gitlab/ci/yaml_processor_spec.rb +++ b/spec/lib/gitlab/ci/yaml_processor_spec.rb @@ -615,19 +615,19 @@ module Gitlab subject { Gitlab::Ci::YamlProcessor.new(YAML.dump(config), opts) } context "when validating a ci config file with no project context" do - context "when a single string is provided", :quarantine do + context "when a single string is provided" do let(:include_content) { "/local.gitlab-ci.yml" } - it "does not return any error" do - expect { subject }.not_to raise_error + it "returns a validation error" do + expect { subject }.to raise_error /does not have project/ end end context "when an array is provided" do let(:include_content) { ["/local.gitlab-ci.yml"] } - it "does not return any error" do - expect { subject }.not_to raise_error + it "returns a validation error" do + expect { subject }.to raise_error(Gitlab::Ci::YamlProcessor::ValidationError, /does not have project/) end end @@ -643,11 +643,18 @@ module Gitlab let(:include_content) do [ 'https://gitlab.com/awesome-project/raw/master/.before-script-template.yml', - '/templates/.after-script-template.yml', { template: 'Auto-DevOps.gitlab-ci.yml' } ] end + before do + WebMock.stub_request(:get, 'https://gitlab.com/awesome-project/raw/master/.before-script-template.yml') + .to_return( + status: 200, + headers: { 'Content-Type' => 'application/json' }, + body: 'prepare: { script: ls -al }') + end + it "does not return any error" do expect { subject }.not_to raise_error end