job.rb 8.54 KB
Newer Older
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
11
12
        class Job < ::Gitlab::Config::Entry::Node
          include ::Gitlab::Config::Entry::Configurable
          include ::Gitlab::Config::Entry::Attributable
13
          include ::Gitlab::Config::Entry::Inheritable
14

15
16
          ALLOWED_WHEN = %w[on_success on_failure always manual delayed].freeze
          ALLOWED_KEYS = %i[tags script only except rules type image services
Shinya Maeda's avatar
Shinya Maeda committed
17
                            allow_failure type stage when start_in artifacts cache
18
19
                            dependencies before_script needs after_script variables
                            environment coverage retry parallel extends interruptible timeout].freeze
20

21
22
          REQUIRED_BY_NEEDS = %i[stage].freeze

23
          validations do
24
            validates :config, type: Hash
25
            validates :config, allowed_keys: ALLOWED_KEYS
26
            validates :config, required_keys: REQUIRED_BY_NEEDS, if: :has_needs?
27
            validates :config, presence: true
28
            validates :script, presence: true
29
30
            validates :name, presence: true
            validates :name, type: Symbol
31
32
33
34
35
36
            validates :config,
              disallowed_keys: {
                in: %i[only except when start_in],
                message: 'key may not be used with `rules`'
              },
              if: :has_rules?
37

38
39
40
            with_options allow_nil: true do
              validates :allow_failure, boolean: true
              validates :parallel, numericality: { only_integer: true,
Kamil Trzciński's avatar
Kamil Trzciński committed
41
42
                                                   greater_than_or_equal_to: 2,
                                                   less_than_or_equal_to: 50 }
43
44
45
46
47
              validates :when, inclusion: {
                in: ALLOWED_WHEN,
                message: "should be one of: #{ALLOWED_WHEN.join(', ')}"
              }

48
              validates :dependencies, array_of_strings: true
Wolphin's avatar
Wolphin committed
49
              validates :extends, array_of_strings_or_string: true
50
              validates :rules, array_of_hashes: true
51
52
            end

53
            validates :start_in, duration: { limit: '1 week' }, if: :delayed?
54
            validates :start_in, absence: true, if: -> { has_rules? || !delayed? }
Kamil Trzciński's avatar
Kamil Trzciński committed
55
56
57
58
59
60
61
62
63
64

            validate do
              next unless dependencies.present?
              next unless needs.present?

              missing_needs = dependencies - needs
              if missing_needs.any?
                errors.add(:dependencies, "the #{missing_needs.join(", ")} should be part of needs")
              end
            end
65
66
          end

67
          entry :before_script, Entry::Script,
68
69
            description: 'Global before script overridden in this job.',
            inherit: true
70

71
          entry :script, Entry::Commands,
72
73
            description: 'Commands that will be executed in this job.',
            inherit: false
74

75
          entry :stage, Entry::Stage,
76
77
            description: 'Pipeline stage this job will be executed into.',
            inherit: false
78

79
          entry :type, Entry::Stage,
80
81
            description: 'Deprecated: stage this job will be executed into.',
            inherit: false
82

83
          entry :after_script, Entry::Script,
84
85
            description: 'Commands that will be executed when finishing job.',
            inherit: true
86

87
          entry :cache, Entry::Cache,
88
89
            description: 'Cache definition for this job.',
            inherit: true
90

91
          entry :image, Entry::Image,
92
93
            description: 'Image that will be used to execute this job.',
            inherit: true
94

95
          entry :services, Entry::Services,
96
97
            description: 'Services that will be used to execute this job.',
            inherit: true
98

99
          entry :interruptible, ::Gitlab::Config::Entry::Boolean,
100
101
102
            description: 'Set jobs interruptible value.',
            inherit: true

103
104
105
106
          entry :timeout, Entry::Timeout,
            description: 'Timeout duration of this job.',
            inherit: true

107
108
109
110
          entry :retry, Entry::Retry,
            description: 'Retry configuration for this job.',
            inherit: true

111
112
113
114
          entry :tags, ::Gitlab::Config::Entry::ArrayOfStrings,
            description: 'Set the tags.',
            inherit: true

115
          entry :only, Entry::Policy,
116
            description: 'Refs policy this job will be executed for.',
117
118
            default: Entry::Policy::DEFAULT_ONLY,
            inherit: false
119

120
          entry :except, Entry::Policy,
121
122
            description: 'Refs policy this job will be executed for.',
            inherit: false
123

124
          entry :rules, Entry::Rules,
125
            description: 'List of evaluable Rules to determine job inclusion.',
126
127
128
129
            inherit: false,
            metadata: {
              allowed_when: %w[on_success on_failure always never manual delayed].freeze
            }
130

131
132
133
134
135
          entry :needs, Entry::Needs,
            description: 'Needs configuration for this job.',
            metadata: { allowed_needs: %i[job] },
            inherit: false

136
          entry :variables, Entry::Variables,
137
138
            description: 'Environment variables available for this job.',
            inherit: false
139

140
          entry :artifacts, Entry::Artifacts,
141
142
            description: 'Artifacts configuration for this job.',
            inherit: false
143

144
          entry :environment, Entry::Environment,
145
146
            description: 'Environment configuration for this job.',
            inherit: false
147

148
          entry :coverage, Entry::Coverage,
149
150
            description: 'Coverage configuration for this job.',
            inherit: false
151

152
          helpers :before_script, :script, :stage, :type, :after_script,
153
                  :cache, :image, :services, :only, :except, :variables,
154
                  :artifacts, :environment, :coverage, :retry, :rules,
155
                  :parallel, :needs, :interruptible
156

157
          attributes :script, :tags, :allow_failure, :when, :dependencies,
158
                     :needs, :retry, :parallel, :extends, :start_in, :rules,
159
                     :interruptible, :timeout
160

161
162
163
164
165
166
167
168
169
          def self.matching?(name, config)
            !name.to_s.start_with?('.') &&
              config.is_a?(Hash) && config.key?(:script)
          end

          def self.visible?
            true
          end

170
          def compose!(deps = nil)
171
172
173
174
175
176
            super do
              if type_defined? && !stage_defined?
                @entries[:stage] = @entries[:type]
              end

              @entries.delete(:type)
177
178

              # This is something of a hack, see issue for details:
drew's avatar
drew committed
179
              # https://gitlab.com/gitlab-org/gitlab/issues/31685
180
181
182
183
              if !only_defined? && has_rules?
                @entries.delete(:only)
                @entries.delete(:except)
              end
184
185
186
            end
          end

187
          def name
188
            @metadata[:name]
189
190
          end

191
          def value
192
            @config.merge(to_hash.compact)
193
194
          end

195
          def manual_action?
196
197
198
199
200
            self.when == 'manual'
          end

          def delayed?
            self.when == 'delayed'
201
202
          end

203
204
205
206
          def has_rules?
            @config.try(:key?, :rules)
          end

207
208
209
210
          def ignored?
            allow_failure.nil? ? manual_action? : allow_failure
          end

211
212
          private

213
214
          def overwrite_entry(deps, key, current_entry)
            deps.default[key] unless current_entry.specified?
215
216
          end

217
          def to_hash
218
            { name: name,
219
220
221
222
223
224
              before_script: before_script_value,
              script: script_value,
              image: image_value,
              services: services_value,
              stage: stage_value,
              cache: cache_value,
225
              only: only_value,
226
              except: except_value,
227
              rules: has_rules? ? rules_value : nil,
228
              variables: variables_defined? ? variables_value : {},
229
230
              environment: environment_defined? ? environment_value : nil,
              environment_name: environment_defined? ? environment_value[:name] : nil,
231
              coverage: coverage_defined? ? coverage_value : nil,
232
              retry: retry_defined? ? retry_value : nil,
233
              parallel: parallel_defined? ? parallel_value.to_i : nil,
234
              interruptible: interruptible_defined? ? interruptible_value : nil,
235
              timeout: has_timeout? ? ChronicDuration.parse(timeout.to_s) : nil,
236
              artifacts: artifacts_value,
237
              after_script: after_script_value,
Kamil Trzciński's avatar
Kamil Trzciński committed
238
239
              ignore: ignored?,
              needs: needs_defined? ? needs_value : nil }
240
          end
241
242
243
244
245
        end
      end
    end
  end
end