job.rb 4.51 KB
Newer Older
1 2 3
module Gitlab
  module Ci
    class Config
4
      module Entry
5 6 7
        ##
        # Entry that represents a concrete CI/CD job.
        #
8
        class Job < Node
9
          include Configurable
10 11
          include Attributable

12 13 14 15
          ALLOWED_KEYS = %i[tags script only except type image services allow_failure
                            type stage when artifacts cache dependencies before_script
                            after_script variables environment]

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

            with_options allow_nil: true do
              validates :tags, array_of_strings: true
              validates :allow_failure, boolean: true
26
              validates :when,
27 28 29
                inclusion: { in: %w[on_success on_failure always manual],
                             message: 'should be on_success, on_failure, ' \
                                      'always or manual' }
30 31

              validates :dependencies, array_of_strings: true
32
            end
33 34
          end

35
          entry :before_script, Entry::Script,
36 37
            description: 'Global before script overridden in this job.'

38
          entry :script, Entry::Commands,
39 40
            description: 'Commands that will be executed in this job.'

41
          entry :stage, Entry::Stage,
42 43
            description: 'Pipeline stage this job will be executed into.'

44
          entry :type, Entry::Stage,
45 46
            description: 'Deprecated: stage this job will be executed into.'

47
          entry :after_script, Entry::Script,
48 49
            description: 'Commands that will be executed when finishing job.'

50
          entry :cache, Entry::Cache,
51 52
            description: 'Cache definition for this job.'

53
          entry :image, Entry::Image,
54 55
            description: 'Image that will be used to execute this job.'

56
          entry :services, Entry::Services,
57 58
            description: 'Services that will be used to execute this job.'

59
          entry :only, Entry::Trigger,
60 61
            description: 'Refs policy this job will be executed for.'

62
          entry :except, Entry::Trigger,
63 64
            description: 'Refs policy this job will be executed for.'

65
          entry :variables, Entry::Variables,
66 67
            description: 'Environment variables available for this job.'

68
          entry :artifacts, Entry::Artifacts,
69 70
            description: 'Artifacts configuration for this job.'

71
          entry :environment, Entry::Environment,
72 73
               description: 'Environment configuration for this job.'

74
          helpers :before_script, :script, :stage, :type, :after_script,
75
                  :cache, :image, :services, :only, :except, :variables,
76
                  :artifacts, :commands, :environment
77

78 79
          attributes :script, :tags, :allow_failure, :when, :dependencies

80
          def compose!(deps = nil)
81 82 83 84 85 86 87
            super do
              if type_defined? && !stage_defined?
                @entries[:stage] = @entries[:type]
              end

              @entries.delete(:type)
            end
88 89

            inherit!(deps)
90 91
          end

92
          def name
93
            @metadata[:name]
94 95
          end

96
          def value
97
            @config.merge(to_hash.compact)
98 99
          end

100 101 102 103
          def commands
            (before_script_value.to_a + script_value.to_a).join("\n")
          end

104 105
          private

106 107 108 109 110 111 112 113 114 115 116 117 118
          def inherit!(deps)
            return unless deps

            self.class.nodes.each_key do |key|
              global_entry = deps[key]
              job_entry = @entries[key]

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

119
          def to_hash
120
            { name: name,
121 122
              before_script: before_script_value,
              script: script_value,
123
              commands: commands,
124 125 126 127 128 129 130 131 132 133 134
              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,
              artifacts: artifacts_value,
              after_script: after_script_value }
135
          end
136 137 138 139 140
        end
      end
    end
  end
end