debian-mirror-gitlab/lib/gitlab/ci/config/entry/processable.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

143 lines
5.1 KiB
Ruby
Raw Normal View History

2020-04-08 14:13:33 +05:30
# frozen_string_literal: true
module Gitlab
module Ci
class Config
module Entry
##
# Entry that represents a CI/CD Processable (a job)
#
module Processable
extend ActiveSupport::Concern
include ::Gitlab::Config::Entry::Configurable
include ::Gitlab::Config::Entry::Attributable
include ::Gitlab::Config::Entry::Inheritable
2020-10-24 23:57:45 +05:30
PROCESSABLE_ALLOWED_KEYS = %i[extends stage only except rules variables
2021-03-11 19:13:27 +05:30
inherit allow_failure when needs resource_group].freeze
2021-11-11 11:23:49 +05:30
MAX_NESTING_LEVEL = 10
2020-04-08 14:13:33 +05:30
included do
validations do
validates :config, presence: true
validates :name, presence: true
validates :name, type: Symbol
2022-06-21 17:19:12 +05:30
validates :name, length: { maximum: 255 }
2020-04-08 14:13:33 +05:30
validates :config, disallowed_keys: {
2022-01-26 12:08:38 +05:30
in: %i[only except start_in],
2023-03-17 16:20:25 +05:30
message: 'key may not be used with `rules`',
ignore_nil: true
}, if: :has_rules_value?
2020-04-08 14:13:33 +05:30
with_options allow_nil: true do
validates :extends, array_of_strings_or_string: true
2021-11-11 11:23:49 +05:30
validates :rules, nested_array_of_hashes_or_arrays: { max_level: MAX_NESTING_LEVEL }
2021-03-11 19:13:27 +05:30
validates :resource_group, type: String
2020-04-08 14:13:33 +05:30
end
end
entry :stage, Entry::Stage,
description: 'Pipeline stage this job will be executed into.',
inherit: false
entry :only, ::Gitlab::Ci::Config::Entry::Policy,
description: 'Refs policy this job will be executed for.',
default: ::Gitlab::Ci::Config::Entry::Policy::DEFAULT_ONLY,
inherit: false
entry :except, ::Gitlab::Ci::Config::Entry::Policy,
description: 'Refs policy this job will be executed for.',
inherit: false
entry :rules, ::Gitlab::Ci::Config::Entry::Rules,
description: 'List of evaluable Rules to determine job inclusion.',
inherit: false,
metadata: {
allowed_when: %w[on_success on_failure always never manual delayed].freeze
}
entry :variables, ::Gitlab::Ci::Config::Entry::Variables,
description: 'Environment variables available for this job.',
2023-01-13 00:05:48 +05:30
metadata: { allowed_value_data: %i[value expand] },
2020-04-08 14:13:33 +05:30
inherit: false
entry :inherit, ::Gitlab::Ci::Config::Entry::Inherit,
description: 'Indicates whether to inherit defaults or not.',
inherit: false,
default: {}
2021-03-11 19:13:27 +05:30
attributes :extends, :rules, :resource_group
2020-04-08 14:13:33 +05:30
end
def compose!(deps = nil)
2022-08-13 15:12:31 +05:30
has_workflow_rules = deps&.workflow_entry&.has_rules?
2020-04-08 14:13:33 +05:30
2022-08-13 15:12:31 +05:30
super do
2020-04-08 14:13:33 +05:30
# If workflow:rules: or rules: are used
# they are considered not compatible
# with `only/except` defaults
#
# Context: https://gitlab.com/gitlab-org/gitlab/merge_requests/21742
if has_rules? || has_workflow_rules
# Remove only/except defaults
# defaults are not considered as defined
@entries.delete(:only) unless only_defined? # rubocop:disable Gitlab/ModuleWithInstanceVariables
@entries.delete(:except) unless except_defined? # rubocop:disable Gitlab/ModuleWithInstanceVariables
end
yield if block_given?
end
2022-08-13 15:12:31 +05:30
validate_against_warnings unless has_workflow_rules
2020-04-08 14:13:33 +05:30
end
2020-10-24 23:57:45 +05:30
def validate_against_warnings
# If rules are valid format and workflow rules are not specified
return unless rules_value
last_rule = rules_value.last
if last_rule&.keys == [:when] && last_rule[:when] != 'never'
docs_url = 'read more: https://docs.gitlab.com/ee/ci/troubleshooting.html#pipeline-warnings'
add_warning("may allow multiple pipelines to run for a single action due to `rules:when` clause with no `workflow:rules` - #{docs_url}")
end
end
2020-04-08 14:13:33 +05:30
def name
metadata[:name]
end
def overwrite_entry(deps, key, current_entry)
return unless inherit_entry&.default_entry&.inherit?(key)
return unless deps.default_entry
deps.default_entry[key] unless current_entry.specified?
end
def value
{ name: name,
stage: stage_value,
extends: extends,
rules: rules_value,
2022-10-11 01:57:18 +05:30
job_variables: variables_entry.value_with_data,
2021-04-29 21:17:54 +05:30
root_variables_inheritance: root_variables_inheritance,
2020-04-08 14:13:33 +05:30
only: only_value,
2021-03-11 19:13:27 +05:30
except: except_value,
resource_group: resource_group }.compact
2020-04-08 14:13:33 +05:30
end
2021-04-29 21:17:54 +05:30
def root_variables_inheritance
inherit_entry&.variables_entry&.value
end
2021-01-29 00:20:46 +05:30
def manual_action?
self.when == 'manual'
end
2020-04-08 14:13:33 +05:30
end
end
end
end
end