Commit e0df05cf authored by Kamil Trzciński's avatar Kamil Trzciński

Require all templates to use default stages

Our templates are free to override the 'stages:'.
However, they should follow the convention to always include
'build', 'test', 'deploy' which are defaults when not defined.
parent 6d000c9f
---
title: Require all templates to use default stages
merge_request: 26954
author:
type: fixed
......@@ -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
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
......
......@@ -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!")
......
......@@ -6,6 +6,7 @@ stages:
- environment
- build
- test
- deploy
- internal
- alpha
- beta
......
......@@ -24,10 +24,6 @@ before_script:
- yes | android-sdk-linux/tools/bin/sdkmanager --licenses
- set -o pipefail
stages:
- build
- test
lintDebug:
stage: build
script:
......
......@@ -60,6 +60,7 @@ variables:
stages:
- build
- test
- deploy # dummy stage to follow the template guidelines
- review
- dast
- staging
......
......@@ -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
......
......@@ -18,6 +18,7 @@ before_script:
stages:
- test
- build
- deploy
format:
stage: test
......
......@@ -15,6 +15,7 @@
image: mono:latest
stages:
- build
- test
- deploy
......
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
......
......@@ -9,6 +9,8 @@ before_script:
stages:
- validate
- build
- test
- deploy
validate:
......
......@@ -4,6 +4,7 @@ image: alpine:latest
stages:
- build
- test
- deploy
.serverless:build:image:
......
......@@ -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
......
......@@ -24,6 +24,7 @@ before_script:
stages:
- validate
- build
- test
- deploy
validate:
......
......@@ -12,6 +12,8 @@
stages:
- build
- test
- deploy
variables:
LC_ALL: "en_US.UTF-8"
......
......@@ -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
......
......@@ -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
def self.abstract_templates
Gitlab::Template::GitlabCiYmlTemplate.all.select do |template|
ABSTRACT_TEMPLATES.include?(template.name)
end
end
using RSpec::Parameterized::TableSyntax
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)
subject { Gitlab::Ci::YamlProcessor.new(content) }
expect { Gitlab::Ci::YamlProcessor.new(template.content, project: project) }
.not_to raise_error
end
end
where(:template_name) do
Gitlab::Template::GitlabCiYmlTemplate.all.map(&:full_name)
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
with_them do
let(:content) do
<<~EOS
include:
- template: #{template_name}
concrete_build_implemented_by_a_user:
stage: build
stage: test
script: do something
EOS
end
expect { Gitlab::Ci::YamlProcessor.new(content) }.not_to raise_error
it 'is valid' do
expect { subject }.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
......@@ -618,8 +618,8 @@ module Gitlab
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
......@@ -635,11 +635,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
......
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