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

220 lines
7.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
2020-04-08 14:13:33 +05:30
include ::Gitlab::Ci::Config::Entry::Processable
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
2020-10-24 23:57:45 +05:30
ALLOWED_KEYS = %i[tags script type image services start_in artifacts
cache dependencies before_script after_script
2020-04-08 14:13:33 +05:30
environment coverage retry parallel interruptible timeout
2020-07-28 23:09:34 +05:30
resource_group release secrets].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
2020-04-08 14:13:33 +05:30
validates :config, allowed_keys: ALLOWED_KEYS + PROCESSABLE_ALLOWED_KEYS
2019-10-12 21:52:04 +05:30
validates :config, required_keys: REQUIRED_BY_NEEDS, if: :has_needs?
2017-08-17 22:00:37 +05:30
validates :script, presence: true
2016-09-13 17:45:13 +05:30
with_options allow_nil: true do
validates :allow_failure, boolean: true
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
2020-03-13 15:44:24 +05:30
validates :resource_group, type: String
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
2020-03-13 15:44:24 +05:30
validate on: :composed do
2019-10-12 21:52:04 +05:30
next unless dependencies.present?
2020-03-13 15:44:24 +05:30
next unless needs_value.present?
missing_needs = dependencies - needs_value[:job].pluck(:name) # rubocop:disable CodeReuse/ActiveRecord (Array#pluck)
2019-10-12 21:52:04 +05:30
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 :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-12-26 22:10:19 +05:30
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 :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
2020-03-13 15:44:24 +05:30
entry :release, Entry::Release,
description: 'This job will produce a release.',
inherit: false
2020-10-24 23:57:45 +05:30
entry :parallel, Entry::Product::Parallel,
description: 'Parallel configuration for this job.',
inherit: false
2018-11-20 20:47:30 +05:30
attributes :script, :tags, :allow_failure, :when, :dependencies,
2020-04-08 14:13:33 +05:30
:needs, :retry, :parallel, :start_in,
2020-03-13 15:44:24 +05:30
:interruptible, :timeout, :resource_group, :release
2016-09-29 09:46:39 +05:30
2020-10-24 23:57:45 +05:30
Matcher = Struct.new(:name, :config) do
def applies?
job_is_not_hidden? &&
config_is_a_hash? &&
has_job_keys?
end
private
def job_is_not_hidden?
!name.to_s.start_with?('.')
end
def config_is_a_hash?
config.is_a?(Hash)
end
def has_job_keys?
if name == :default
config.key?(:script)
else
(ALLOWED_KEYS & config.keys).any?
end
end
end
2019-09-30 21:07:59 +05:30
def self.matching?(name, config)
2020-10-24 23:57:45 +05:30
if Gitlab::Ci::Features.job_entry_matches_all_keys?
Matcher.new(name, config).applies?
else
!name.to_s.start_with?('.') &&
config.is_a?(Hash) && config.key?(:script)
end
2019-09-30 21:07:59 +05:30
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
end
2016-09-13 17:45:13 +05:30
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
2020-04-08 14:13:33 +05:30
def value
super.merge(
2017-08-17 22:00:37 +05:30
before_script: before_script_value,
script: script_value,
image: image_value,
services: services_value,
cache: cache_value,
2020-03-13 15:44:24 +05:30
tags: tags_value,
2020-04-08 14:13:33 +05:30
when: self.when,
start_in: self.start_in,
dependencies: dependencies,
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,
2020-10-24 23:57:45 +05:30
parallel: has_parallel? ? parallel_value : 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,
2020-03-13 15:44:24 +05:30
release: release_value,
2017-08-17 22:00:37 +05:30
after_script: after_script_value,
2019-10-12 21:52:04 +05:30
ignore: ignored?,
2020-03-13 15:44:24 +05:30
needs: needs_defined? ? needs_value : nil,
resource_group: resource_group,
2020-04-08 14:13:33 +05:30
scheduling_type: needs_defined? ? :dag : :stage
).compact
2016-09-13 17:45:13 +05:30
end
end
end
end
end
end
2020-07-28 23:09:34 +05:30
::Gitlab::Ci::Config::Entry::Job.prepend_if_ee('::EE::Gitlab::Ci::Config::Entry::Job')