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

188 lines
6.3 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
2016-09-13 17:45:13 +05:30
2018-11-20 20:47:30 +05:30
ALLOWED_KEYS = %i[tags script only except type image services
2018-12-05 23:21:45 +05:30
allow_failure type stage when start_in artifacts cache
2018-11-20 20:47:30 +05:30
dependencies before_script after_script variables
2018-12-13 13:39:08 +05:30
environment coverage retry parallel extends].freeze
2016-09-13 17:45:13 +05:30
validations do
validates :config, allowed_keys: ALLOWED_KEYS
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
with_options allow_nil: true do
validates :tags, array_of_strings: true
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 }
2016-09-13 17:45:13 +05:30
validates :when,
2018-12-05 23:21:45 +05:30
inclusion: { in: %w[on_success on_failure always manual delayed],
2016-09-13 17:45:13 +05:30
message: 'should be on_success, on_failure, ' \
2018-12-05 23:21:45 +05:30
'always, manual or delayed' }
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
2016-09-13 17:45:13 +05:30
end
2018-12-05 23:21:45 +05:30
validates :start_in, duration: { limit: '1 day' }, if: :delayed?
validates :start_in, absence: true, unless: :delayed?
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,
2016-09-13 17:45:13 +05:30
description: 'Commands that will be executed in this job.'
2017-08-17 22:00:37 +05:30
entry :stage, Entry::Stage,
2016-09-13 17:45:13 +05:30
description: 'Pipeline stage this job will be executed into.'
2017-08-17 22:00:37 +05:30
entry :type, Entry::Stage,
2016-09-13 17:45:13 +05:30
description: 'Deprecated: stage this job will be executed into.'
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
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.',
default: Entry::Policy::DEFAULT_ONLY
2016-09-13 17:45:13 +05:30
2019-01-03 12:48:30 +05:30
entry :except, Entry::Policy,
2016-09-13 17:45:13 +05:30
description: 'Refs policy this job will be executed for.'
2017-08-17 22:00:37 +05:30
entry :variables, Entry::Variables,
2016-09-13 17:45:13 +05:30
description: 'Environment variables available for this job.'
2017-08-17 22:00:37 +05:30
entry :artifacts, Entry::Artifacts,
2016-09-13 17:45:13 +05:30
description: 'Artifacts configuration for this job.'
2017-08-17 22:00:37 +05:30
entry :environment, Entry::Environment,
2018-12-13 13:39:08 +05:30
description: 'Environment configuration for this job.'
2016-09-29 09:46:39 +05:30
2017-08-17 22:00:37 +05:30
entry :coverage, Entry::Coverage,
2018-12-13 13:39:08 +05:30
description: 'Coverage configuration for this job.'
entry :retry, Entry::Retry,
description: 'Retry configuration for this job.'
2017-08-17 22:00:37 +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-02-15 15:39:39 +05:30
:artifacts, :environment, :coverage, :retry,
2018-12-13 13:39:08 +05:30
:parallel
2017-08-17 22:00:37 +05:30
2018-11-20 20:47:30 +05:30
attributes :script, :tags, :allow_failure, :when, :dependencies,
2018-12-13 13:39:08 +05:30
:retry, :parallel, :extends, :start_in
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)
end
inherit!(deps)
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
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-09-30 21:07:59 +05:30
# We inherit config entries from `default:`
# if the entry has the `inherit: true` flag set
2016-09-29 09:46:39 +05:30
def inherit!(deps)
return unless deps
2019-09-30 21:07:59 +05:30
self.class.nodes.each do |key, factory|
next unless factory.inheritable?
default_entry = deps.default[key]
2017-08-17 22:00:37 +05:30
job_entry = self[key]
2016-09-29 09:46:39 +05:30
2019-09-30 21:07:59 +05:30
if default_entry.specified? && !job_entry.specified?
@entries[key] = default_entry
2016-09-29 09:46:39 +05:30
end
end
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-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,
2017-08-17 22:00:37 +05:30
artifacts: artifacts_value,
after_script: after_script_value,
ignore: ignored? }
2016-09-13 17:45:13 +05:30
end
end
end
end
end
end