job.rb 6.96 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
        class Job < ::Gitlab::Config::Entry::Node
11
          include ::Gitlab::Ci::Config::Entry::Processable
12

13
          ALLOWED_WHEN = %w[on_success on_failure always manual delayed].freeze
14 15
          ALLOWED_KEYS = %i[tags script type image services start_in artifacts
                            cache dependencies before_script after_script
16
                            environment coverage retry parallel interruptible timeout
17
                            release dast_configuration secrets].freeze
18

19 20
          REQUIRED_BY_NEEDS = %i[stage].freeze

21
          validations do
22
            validates :config, allowed_keys: ALLOWED_KEYS + PROCESSABLE_ALLOWED_KEYS
23
            validates :config, required_keys: REQUIRED_BY_NEEDS, if: :has_needs?
24
            validates :script, presence: true
25

26
            with_options allow_nil: true do
27 28 29 30 31
              validates :when, inclusion: {
                in: ALLOWED_WHEN,
                message: "should be one of: #{ALLOWED_WHEN.join(', ')}"
              }

32
              validates :dependencies, array_of_strings: true
33
              validates :allow_failure, hash_or_boolean: true
34 35
            end

36
            validates :start_in, duration: { limit: '1 week' }, if: :delayed?
37
            validates :start_in, absence: true, if: -> { has_rules? || !delayed? }
Kamil Trzciński's avatar
Kamil Trzciński committed
38

39
            validate on: :composed do
Kamil Trzciński's avatar
Kamil Trzciński committed
40
              next unless dependencies.present?
41 42 43
              next unless needs_value.present?

              missing_needs = dependencies - needs_value[:job].pluck(:name) # rubocop:disable CodeReuse/ActiveRecord (Array#pluck)
Kamil Trzciński's avatar
Kamil Trzciński committed
44 45 46 47 48

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

51
          entry :before_script, Entry::Script,
52 53
            description: 'Global before script overridden in this job.',
            inherit: true
54

55
          entry :script, Entry::Commands,
56 57
            description: 'Commands that will be executed in this job.',
            inherit: false
58

59
          entry :type, Entry::Stage,
60 61
            description: 'Deprecated: stage this job will be executed into.',
            inherit: false
62

63
          entry :after_script, Entry::Script,
64 65
            description: 'Commands that will be executed when finishing job.',
            inherit: true
66

67
          entry :cache, Entry::Caches,
68 69
            description: 'Cache definition for this job.',
            inherit: true
70

71
          entry :image, Entry::Image,
72 73
            description: 'Image that will be used to execute this job.',
            inherit: true
74

75
          entry :services, Entry::Services,
76 77
            description: 'Services that will be used to execute this job.',
            inherit: true
78

79
          entry :interruptible, ::Gitlab::Config::Entry::Boolean,
80 81 82
            description: 'Set jobs interruptible value.',
            inherit: true

83 84 85 86
          entry :timeout, Entry::Timeout,
            description: 'Timeout duration of this job.',
            inherit: true

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

91 92 93 94
          entry :tags, ::Gitlab::Config::Entry::ArrayOfStrings,
            description: 'Set the tags.',
            inherit: true

95 96 97 98
          entry :artifacts, Entry::Artifacts,
            description: 'Artifacts configuration for this job.',
            inherit: true

99 100
          entry :needs, Entry::Needs,
            description: 'Needs configuration for this job.',
101
            metadata: { allowed_needs: %i[job cross_dependency] },
102 103
            inherit: false

104
          entry :environment, Entry::Environment,
105 106
            description: 'Environment configuration for this job.',
            inherit: false
107

108
          entry :coverage, Entry::Coverage,
109 110
            description: 'Coverage configuration for this job.',
            inherit: false
111

112 113 114 115
          entry :release, Entry::Release,
            description: 'This job will produce a release.',
            inherit: false

116 117 118 119
          entry :parallel, Entry::Product::Parallel,
            description: 'Parallel configuration for this job.',
            inherit: false

120 121 122 123
          entry :allow_failure, ::Gitlab::Ci::Config::Entry::AllowFailure,
            description: 'Indicates whether this job is allowed to fail or not.',
            inherit: false

124
          attributes :script, :tags, :when, :dependencies,
125
                     :needs, :retry, :parallel, :start_in,
126
                     :interruptible, :timeout,
127
                     :release, :allow_failure
128

129
          def self.matching?(name, config)
130 131
            !name.to_s.start_with?('.') &&
              config.is_a?(Hash) && config.key?(:script)
132 133 134 135 136 137
          end

          def self.visible?
            true
          end

138
          def compose!(deps = nil)
139 140 141 142 143 144 145 146 147
            super do
              if type_defined? && !stage_defined?
                @entries[:stage] = @entries[:type]
              end

              @entries.delete(:type)
            end
          end

148 149
          def delayed?
            self.when == 'delayed'
150 151
          end

152 153
          def value
            super.merge(
154 155 156 157 158
              before_script: before_script_value,
              script: script_value,
              image: image_value,
              services: services_value,
              cache: cache_value,
159
              tags: tags_value,
160 161 162
              when: self.when,
              start_in: self.start_in,
              dependencies: dependencies,
163 164
              environment: environment_defined? ? environment_value : nil,
              environment_name: environment_defined? ? environment_value[:name] : nil,
165
              coverage: coverage_defined? ? coverage_value : nil,
166
              retry: retry_defined? ? retry_value : nil,
167
              parallel: has_parallel? ? parallel_value : nil,
168
              interruptible: interruptible_defined? ? interruptible_value : nil,
169
              timeout: has_timeout? ? ChronicDuration.parse(timeout.to_s) : nil,
170
              artifacts: artifacts_value,
171
              release: release_value,
172
              after_script: after_script_value,
Kamil Trzciński's avatar
Kamil Trzciński committed
173
              ignore: ignored?,
174
              allow_failure_criteria: allow_failure_criteria,
175
              needs: needs_defined? ? needs_value : nil,
176 177
              scheduling_type: needs_defined? ? :dag : :stage
            ).compact
178
          end
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

          def ignored?
            allow_failure_defined? ? static_allow_failure : manual_action?
          end

          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
197 198 199 200 201
        end
      end
    end
  end
end
202

203
::Gitlab::Ci::Config::Entry::Job.prepend_mod_with('Gitlab::Ci::Config::Entry::Job')