job.rb 5.7 KB
Newer Older
G
gfyoung 已提交
1 2
# frozen_string_literal: true

3 4 5
module Gitlab
  module Ci
    class Config
6
      module Entry
7 8 9
        ##
        # Entry that represents a concrete CI/CD job.
        #
10
        class Job < Node
11
          include Configurable
12 13
          include Attributable

14
          ALLOWED_KEYS = %i[tags script only except type image services
S
Shinya Maeda 已提交
15
                            allow_failure type stage when start_in artifacts cache
16
                            dependencies before_script after_script variables
M
Matija Čupić 已提交
17
                            environment coverage retry parallel extends].freeze
18

19
          validations do
20
            validates :config, allowed_keys: ALLOWED_KEYS
21
            validates :config, presence: true
22
            validates :script, presence: true
23 24
            validates :name, presence: true
            validates :name, type: Symbol
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
            with_options allow_nil: true do
              validates :tags, array_of_strings: true
              validates :allow_failure, boolean: true
              validates :parallel, numericality: { only_integer: true,
                                                   greater_than_or_equal_to: 2 }

              validates :when,
                inclusion: { in: %w[on_success on_failure always manual delayed],
                             message: 'should be on_success, on_failure, ' \
                                      'always, manual or delayed' }

              validates :dependencies, array_of_strings: true
              validates :extends, type: String
            end

S
Shinya Maeda 已提交
41
            validates :start_in, duration: { limit: '1 day' }, if: :delayed?
42
            validates :start_in, absence: true, unless: :delayed?
43 44
          end

45
          entry :before_script, Entry::Script,
46 47
            description: 'Global before script overridden in this job.'

48
          entry :script, Entry::Commands,
49 50
            description: 'Commands that will be executed in this job.'

51
          entry :stage, Entry::Stage,
52 53
            description: 'Pipeline stage this job will be executed into.'

54
          entry :type, Entry::Stage,
55 56
            description: 'Deprecated: stage this job will be executed into.'

57
          entry :after_script, Entry::Script,
58 59
            description: 'Commands that will be executed when finishing job.'

60
          entry :cache, Entry::Cache,
61 62
            description: 'Cache definition for this job.'

63
          entry :image, Entry::Image,
64 65
            description: 'Image that will be used to execute this job.'

66
          entry :services, Entry::Services,
67 68
            description: 'Services that will be used to execute this job.'

69
          entry :only, Entry::Policy,
70 71
            description: 'Refs policy this job will be executed for.'

72
          entry :except, Entry::Policy,
73 74
            description: 'Refs policy this job will be executed for.'

75
          entry :variables, Entry::Variables,
76 77
            description: 'Environment variables available for this job.'

78
          entry :artifacts, Entry::Artifacts,
79 80
            description: 'Artifacts configuration for this job.'

81
          entry :environment, Entry::Environment,
M
Matija Čupić 已提交
82
            description: 'Environment configuration for this job.'
83

84
          entry :coverage, Entry::Coverage,
M
Matija Čupić 已提交
85
            description: 'Coverage configuration for this job.'
86

87 88 89
          entry :retry, Entry::Retry,
               description: 'Retry configuration for this job.'

90
          helpers :before_script, :script, :stage, :type, :after_script,
91
                  :cache, :image, :services, :only, :except, :variables,
M
Matija Čupić 已提交
92 93
                  :artifacts, :commands, :environment, :coverage, :retry,
                  :parallel
94

95
          attributes :script, :tags, :allow_failure, :when, :dependencies,
M
Matija Čupić 已提交
96
                     :retry, :parallel, :extends, :start_in
97

98
          def compose!(deps = nil)
99 100 101 102 103 104 105
            super do
              if type_defined? && !stage_defined?
                @entries[:stage] = @entries[:type]
              end

              @entries.delete(:type)
            end
106 107

            inherit!(deps)
108 109
          end

110
          def name
111
            @metadata[:name]
112 113
          end

114
          def value
115
            @config.merge(to_hash.compact)
116 117
          end

118 119 120 121
          def commands
            (before_script_value.to_a + script_value.to_a).join("\n")
          end

122
          def manual_action?
123 124 125 126 127
            self.when == 'manual'
          end

          def delayed?
            self.when == 'delayed'
128 129 130 131 132 133
          end

          def ignored?
            allow_failure.nil? ? manual_action? : allow_failure
          end

134 135
          private

136 137 138 139 140
          def inherit!(deps)
            return unless deps

            self.class.nodes.each_key do |key|
              global_entry = deps[key]
141
              job_entry = self[key]
142 143 144 145 146 147 148

              if global_entry.specified? && !job_entry.specified?
                @entries[key] = global_entry
              end
            end
          end

149
          def to_hash
150
            { name: name,
151 152
              before_script: before_script_value,
              script: script_value,
153
              commands: commands,
154 155 156 157 158 159 160 161 162
              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,
163
              coverage: coverage_defined? ? coverage_value : nil,
164
              retry: retry_defined? ? retry_value : nil,
M
Matija Čupić 已提交
165
              parallel: parallel_defined? ? parallel_value.to_i : nil,
166
              artifacts: artifacts_value,
167 168
              after_script: after_script_value,
              ignore: ignored? }
169
          end
170 171 172 173 174
        end
      end
    end
  end
end