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

253 lines
8.9 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
module Gitlab
module Ci
class Config
2017-08-17 22:00:37 +05:30
module Entry
2016-09-13 17:45:13 +05:30
##
# Entry that represents a concrete CI/CD job.
#
2019-02-15 15:39:39 +05:30
class Job < ::Gitlab::Config::Entry::Node
include ::Gitlab::Config::Entry::Configurable
include ::Gitlab::Config::Entry::Attributable
2019-12-26 22:10:19 +05:30
include ::Gitlab::Config::Entry::Inheritable
2016-09-13 17:45:13 +05:30
2019-12-04 20:38:33 +05:30
ALLOWED_WHEN = %w[on_success on_failure always manual delayed].freeze
ALLOWED_KEYS = %i[tags script only except rules type image services
2018-12-05 23:21:45 +05:30
allow_failure type stage when start_in artifacts cache
2019-12-04 20:38:33 +05:30
dependencies before_script needs after_script variables
environment coverage retry parallel extends interruptible timeout].freeze
2016-09-13 17:45:13 +05:30
2019-10-12 21:52:04 +05:30
REQUIRED_BY_NEEDS = %i[stage].freeze
2016-09-13 17:45:13 +05:30
validations do
2019-12-04 20:38:33 +05:30
validates :config, type: Hash
2016-09-13 17:45:13 +05:30
validates :config, allowed_keys: ALLOWED_KEYS
2019-10-12 21:52:04 +05:30
validates :config, required_keys: REQUIRED_BY_NEEDS, if: :has_needs?
2016-09-13 17:45:13 +05:30
validates :config, presence: true
2017-08-17 22:00:37 +05:30
validates :script, presence: true
2016-09-13 17:45:13 +05:30
validates :name, presence: true
validates :name, type: Symbol
2019-12-04 20:38:33 +05:30
validates :config,
disallowed_keys: {
in: %i[only except when start_in],
message: 'key may not be used with `rules`'
},
if: :has_rules?
2016-09-13 17:45:13 +05:30
with_options allow_nil: true do
validates :allow_failure, boolean: true
2018-12-13 13:39:08 +05:30
validates :parallel, numericality: { only_integer: true,
greater_than_or_equal_to: 2,
less_than_or_equal_to: 50 }
2019-12-04 20:38:33 +05:30
validates :when, inclusion: {
in: ALLOWED_WHEN,
message: "should be one of: #{ALLOWED_WHEN.join(', ')}"
}
2016-09-13 17:45:13 +05:30
validates :dependencies, array_of_strings: true
2019-09-04 21:01:54 +05:30
validates :extends, array_of_strings_or_string: true
2019-12-04 20:38:33 +05:30
validates :rules, array_of_hashes: true
2016-09-13 17:45:13 +05:30
end
2018-12-05 23:21:45 +05:30
2020-01-01 13:55:28 +05:30
validates :start_in, duration: { limit: '1 week' }, if: :delayed?
2019-12-04 20:38:33 +05:30
validates :start_in, absence: true, if: -> { has_rules? || !delayed? }
2019-10-12 21:52:04 +05:30
validate do
next unless dependencies.present?
next unless needs.present?
missing_needs = dependencies - needs
if missing_needs.any?
errors.add(:dependencies, "the #{missing_needs.join(", ")} should be part of needs")
end
end
2016-09-13 17:45:13 +05:30
end
2017-08-17 22:00:37 +05:30
entry :before_script, Entry::Script,
2019-09-30 21:07:59 +05:30
description: 'Global before script overridden in this job.',
inherit: true
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
entry :script, Entry::Commands,
2019-12-26 22:10:19 +05:30
description: 'Commands that will be executed in this job.',
inherit: false
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
entry :stage, Entry::Stage,
2019-12-26 22:10:19 +05:30
description: 'Pipeline stage this job will be executed into.',
inherit: false
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
entry :type, Entry::Stage,
2019-12-26 22:10:19 +05:30
description: 'Deprecated: stage this job will be executed into.',
inherit: false
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
entry :after_script, Entry::Script,
2019-09-30 21:07:59 +05:30
description: 'Commands that will be executed when finishing job.',
inherit: true
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
entry :cache, Entry::Cache,
2019-09-30 21:07:59 +05:30
description: 'Cache definition for this job.',
inherit: true
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
entry :image, Entry::Image,
2019-09-30 21:07:59 +05:30
description: 'Image that will be used to execute this job.',
inherit: true
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
entry :services, Entry::Services,
2019-09-30 21:07:59 +05:30
description: 'Services that will be used to execute this job.',
inherit: true
2016-09-13 17:45:13 +05:30
2020-01-01 13:55:28 +05:30
entry :interruptible, ::Gitlab::Config::Entry::Boolean,
2019-12-26 22:10:19 +05:30
description: 'Set jobs interruptible value.',
inherit: true
2020-01-01 13:55:28 +05:30
entry :timeout, Entry::Timeout,
description: 'Timeout duration of this job.',
inherit: true
entry :retry, Entry::Retry,
description: 'Retry configuration for this job.',
inherit: true
entry :tags, ::Gitlab::Config::Entry::ArrayOfStrings,
description: 'Set the tags.',
inherit: true
entry :artifacts, Entry::Artifacts,
description: 'Artifacts configuration for this job.',
inherit: true
2019-01-03 12:48:30 +05:30
entry :only, Entry::Policy,
2019-03-02 22:35:43 +05:30
description: 'Refs policy this job will be executed for.',
2020-01-01 13:55:28 +05:30
default: ::Gitlab::Ci::Config::Entry::Policy::DEFAULT_ONLY,
2019-12-26 22:10:19 +05:30
inherit: false
2016-09-13 17:45:13 +05:30
2019-01-03 12:48:30 +05:30
entry :except, Entry::Policy,
2019-12-26 22:10:19 +05:30
description: 'Refs policy this job will be executed for.',
inherit: false
2016-09-13 17:45:13 +05:30
2019-12-04 20:38:33 +05:30
entry :rules, Entry::Rules,
2019-12-26 22:10:19 +05:30
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 :needs, Entry::Needs,
description: 'Needs configuration for this job.',
2020-01-01 13:55:28 +05:30
metadata: { allowed_needs: %i[job cross_dependency] },
2019-12-26 22:10:19 +05:30
inherit: false
2019-12-04 20:38:33 +05:30
2017-08-17 22:00:37 +05:30
entry :variables, Entry::Variables,
2019-12-26 22:10:19 +05:30
description: 'Environment variables available for this job.',
inherit: false
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
entry :environment, Entry::Environment,
2019-12-26 22:10:19 +05:30
description: 'Environment configuration for this job.',
inherit: false
2016-09-29 09:46:39 +05:30
2017-08-17 22:00:37 +05:30
entry :coverage, Entry::Coverage,
2019-12-26 22:10:19 +05:30
description: 'Coverage configuration for this job.',
inherit: false
2018-12-13 13:39:08 +05:30
2016-09-13 17:45:13 +05:30
helpers :before_script, :script, :stage, :type, :after_script,
:cache, :image, :services, :only, :except, :variables,
2019-12-04 20:38:33 +05:30
:artifacts, :environment, :coverage, :retry, :rules,
:parallel, :needs, :interruptible
2017-08-17 22:00:37 +05:30
2018-11-20 20:47:30 +05:30
attributes :script, :tags, :allow_failure, :when, :dependencies,
2019-12-04 20:38:33 +05:30
:needs, :retry, :parallel, :extends, :start_in, :rules,
:interruptible, :timeout
2016-09-29 09:46:39 +05:30
2019-09-30 21:07:59 +05:30
def self.matching?(name, config)
!name.to_s.start_with?('.') &&
config.is_a?(Hash) && config.key?(:script)
end
def self.visible?
true
end
2016-09-29 09:46:39 +05:30
def compose!(deps = nil)
super do
if type_defined? && !stage_defined?
@entries[:stage] = @entries[:type]
end
@entries.delete(:type)
2019-12-04 20:38:33 +05:30
2020-01-01 13:55:28 +05:30
has_workflow_rules = deps&.workflow&.has_rules?
# 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?
@entries.delete(:except) unless except_defined?
2019-12-04 20:38:33 +05:30
end
2016-09-29 09:46:39 +05:30
end
end
2016-09-13 17:45:13 +05:30
def name
@metadata[:name]
end
def value
@config.merge(to_hash.compact)
end
2017-08-17 22:00:37 +05:30
def manual_action?
self.when == 'manual'
end
2018-12-05 23:21:45 +05:30
def delayed?
self.when == 'delayed'
end
2019-12-04 20:38:33 +05:30
def has_rules?
@config.try(:key?, :rules)
end
2017-08-17 22:00:37 +05:30
def ignored?
allow_failure.nil? ? manual_action? : allow_failure
end
2016-09-13 17:45:13 +05:30
private
2019-12-26 22:10:19 +05:30
def overwrite_entry(deps, key, current_entry)
deps.default[key] unless current_entry.specified?
2016-09-29 09:46:39 +05:30
end
2016-09-13 17:45:13 +05:30
def to_hash
{ name: name,
2017-08-17 22:00:37 +05:30
before_script: before_script_value,
script: script_value,
image: image_value,
services: services_value,
stage: stage_value,
cache: cache_value,
2019-03-02 22:35:43 +05:30
only: only_value,
except: except_value,
2019-12-04 20:38:33 +05:30
rules: has_rules? ? rules_value : nil,
2019-09-30 21:07:59 +05:30
variables: variables_defined? ? variables_value : {},
2017-08-17 22:00:37 +05:30
environment: environment_defined? ? environment_value : nil,
environment_name: environment_defined? ? environment_value[:name] : nil,
coverage: coverage_defined? ? coverage_value : nil,
2018-12-13 13:39:08 +05:30
retry: retry_defined? ? retry_value : nil,
parallel: parallel_defined? ? parallel_value.to_i : nil,
2019-12-04 20:38:33 +05:30
interruptible: interruptible_defined? ? interruptible_value : nil,
timeout: has_timeout? ? ChronicDuration.parse(timeout.to_s) : nil,
2017-08-17 22:00:37 +05:30
artifacts: artifacts_value,
after_script: after_script_value,
2019-10-12 21:52:04 +05:30
ignore: ignored?,
needs: needs_defined? ? needs_value : nil }
2016-09-13 17:45:13 +05:30
end
end
end
end
end
end