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

193 lines
6.7 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
2022-07-16 23:28:13 +05:30
ALLOWED_KEYS = %i[tags script image services start_in artifacts
2020-10-24 23:57:45 +05:30
cache dependencies before_script after_script
2020-04-08 14:13:33 +05:30
environment coverage retry parallel interruptible timeout
2021-12-11 22:18:48 +05:30
release].freeze
2016-09-13 17:45:13 +05:30
validations do
2021-12-11 22:18:48 +05:30
validates :config, allowed_keys: Gitlab::Ci::Config::Entry::Job.allowed_keys + PROCESSABLE_ALLOWED_KEYS
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
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
2021-02-22 17:27:13 +05:30
validates :allow_failure, hash_or_boolean: 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
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?
2022-05-07 20:08:51 +05:30
if needs_value[:job].nil? && needs_value[:cross_dependency].present?
errors.add(:needs, "corresponding to dependencies must be from the same pipeline")
else
missing_needs = dependencies - needs_value[:job].pluck(:name) # rubocop:disable CodeReuse/ActiveRecord (Array#pluck)
2019-10-12 21:52:04 +05:30
2022-05-07 20:08:51 +05:30
errors.add(:dependencies, "the #{missing_needs.join(", ")} should be part of needs") if missing_needs.any?
2019-10-12 21:52:04 +05:30
end
end
2016-09-13 17:45:13 +05:30
end
2022-04-04 11:22:00 +05:30
entry :before_script, Entry::Commands,
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
2022-04-04 11:22:00 +05:30
entry :after_script, Entry::Commands,
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
2021-06-08 01:23:25 +05:30
entry :cache, Entry::Caches,
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
2021-11-11 11:23:49 +05:30
entry :tags, Entry::Tags,
2020-01-01 13:55:28 +05:30
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
2021-02-22 17:27:13 +05:30
entry :allow_failure, ::Gitlab::Ci::Config::Entry::AllowFailure,
description: 'Indicates whether this job is allowed to fail or not.',
inherit: false
2021-01-29 00:20:46 +05:30
attributes :script, :tags, :when, :dependencies,
2020-04-08 14:13:33 +05:30
:needs, :retry, :parallel, :start_in,
2021-03-11 19:13:27 +05:30
:interruptible, :timeout,
2021-02-22 17:27:13 +05:30
:release, :allow_failure
2016-09-29 09:46:39 +05:30
2019-09-30 21:07:59 +05:30
def self.matching?(name, config)
2020-11-24 15:15:51 +05:30
!name.to_s.start_with?('.') &&
config.is_a?(Hash) && config.key?(:script)
2019-09-30 21:07:59 +05:30
end
def self.visible?
true
end
2018-12-05 23:21:45 +05:30
def delayed?
self.when == 'delayed'
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?,
2021-02-22 17:27:13 +05:30
allow_failure_criteria: allow_failure_criteria,
2020-03-13 15:44:24 +05:30
needs: needs_defined? ? needs_value : nil,
2020-04-08 14:13:33 +05:30
scheduling_type: needs_defined? ? :dag : :stage
).compact
2016-09-13 17:45:13 +05:30
end
2021-02-22 17:27:13 +05:30
def ignored?
allow_failure_defined? ? static_allow_failure : manual_action?
end
2021-12-11 22:18:48 +05:30
def self.allowed_keys
ALLOWED_KEYS
end
2021-02-22 17:27:13 +05:30
private
def allow_failure_criteria
if allow_failure_defined? && allow_failure_value.is_a?(Hash)
allow_failure_value
end
end
def static_allow_failure
return false if allow_failure_value.is_a?(Hash)
allow_failure_value
end
2016-09-13 17:45:13 +05:30
end
end
end
end
end
2020-07-28 23:09:34 +05:30
2021-06-08 01:23:25 +05:30
::Gitlab::Ci::Config::Entry::Job.prepend_mod_with('Gitlab::Ci::Config::Entry::Job')