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

181 lines
6.1 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.
#
2018-12-23 12:14:25 +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
2018-12-23 12:14:25 +05:30
DEFAULT_ONLY_POLICY = {
refs: %w(branches tags)
}.freeze
DEFAULT_EXCEPT_POLICY = {
}.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
2018-11-20 20:47:30 +05:30
validates :extends, type: String
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,
2016-09-13 17:45:13 +05:30
description: 'Global before script overridden in this job.'
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,
2016-09-13 17:45:13 +05:30
description: 'Commands that will be executed when finishing job.'
2017-08-17 22:00:37 +05:30
entry :cache, Entry::Cache,
2016-09-13 17:45:13 +05:30
description: 'Cache definition for this job.'
2017-08-17 22:00:37 +05:30
entry :image, Entry::Image,
2016-09-13 17:45:13 +05:30
description: 'Image that will be used to execute this job.'
2017-08-17 22:00:37 +05:30
entry :services, Entry::Services,
2016-09-13 17:45:13 +05:30
description: 'Services that will be used to execute this job.'
2018-12-23 12:14:25 +05:30
entry :only, Entry::OnlyPolicy,
2016-09-13 17:45:13 +05:30
description: 'Refs policy this job will be executed for.'
2018-12-23 12:14:25 +05:30
entry :except, Entry::ExceptPolicy,
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,
2018-12-13 13:39:08 +05:30
:artifacts, :commands, :environment, :coverage, :retry,
: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
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
2016-09-29 09:46:39 +05:30
def commands
(before_script_value.to_a + script_value.to_a).join("\n")
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
2016-09-29 09:46:39 +05:30
def inherit!(deps)
return unless deps
self.class.nodes.each_key do |key|
global_entry = deps[key]
2017-08-17 22:00:37 +05:30
job_entry = self[key]
2016-09-29 09:46:39 +05:30
if global_entry.specified? && !job_entry.specified?
@entries[key] = global_entry
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,
2016-09-29 09:46:39 +05:30
commands: commands,
2017-08-17 22:00:37 +05:30
image: image_value,
services: services_value,
stage: stage_value,
cache: cache_value,
2018-12-23 12:14:25 +05:30
only: DEFAULT_ONLY_POLICY.deep_merge(only_value.to_h),
except: DEFAULT_EXCEPT_POLICY.deep_merge(except_value.to_h),
2017-08-17 22:00:37 +05:30
variables: variables_defined? ? variables_value : nil,
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