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

154 lines
4.9 KiB
Ruby
Raw Normal View History

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.
#
2017-08-17 22:00:37 +05:30
class Job < Node
2016-09-13 17:45:13 +05:30
include Configurable
include Attributable
ALLOWED_KEYS = %i[tags script only except type image services allow_failure
type stage when artifacts cache dependencies before_script
2017-08-17 22:00:37 +05:30
after_script variables environment coverage].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
validates :when,
inclusion: { in: %w[on_success on_failure always manual],
message: 'should be on_success, on_failure, ' \
'always or manual' }
validates :dependencies, array_of_strings: true
end
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.'
2017-08-17 22:00:37 +05:30
entry :only, Entry::Trigger,
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 :except, Entry::Trigger,
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,
2016-09-29 09:46:39 +05:30
description: 'Environment configuration for this job.'
2017-08-17 22:00:37 +05:30
entry :coverage, Entry::Coverage,
description: 'Coverage configuration for this job.'
2016-09-13 17:45:13 +05:30
helpers :before_script, :script, :stage, :type, :after_script,
:cache, :image, :services, :only, :except, :variables,
2017-08-17 22:00:37 +05:30
:artifacts, :commands, :environment, :coverage
attributes :script, :tags, :allow_failure, :when, :dependencies
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
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,
only: only_value,
except: except_value,
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,
artifacts: artifacts_value,
after_script: after_script_value,
ignore: ignored? }
2016-09-13 17:45:13 +05:30
end
end
end
end
end
end