Read about our upcoming Code of Conduct on this issue

This instance was upgraded to Heptapod 0.28.0 today

build.rb 7.79 KB
Newer Older
Douwe Maan's avatar
Douwe Maan committed
1
2
# == Schema Information
#
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
3
# Table name: ci_builds
Douwe Maan's avatar
Douwe Maan committed
4
5
6
7
8
9
10
11
12
13
14
#
#  id                 :integer          not null, primary key
#  project_id         :integer
#  status             :string(255)
#  finished_at        :datetime
#  trace              :text
#  created_at         :datetime
#  updated_at         :datetime
#  started_at         :datetime
#  runner_id          :integer
#  coverage           :float
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
15
#  commit_id          :integer
Douwe Maan's avatar
Douwe Maan committed
16
17
18
#  commands           :text
#  job_id             :integer
#  name               :string(255)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
19
#  deploy             :boolean          default(FALSE)
Douwe Maan's avatar
Douwe Maan committed
20
21
22
23
#  options            :text
#  allow_failure      :boolean          default(FALSE), not null
#  stage              :string(255)
#  trigger_request_id :integer
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
24
25
26
27
28
29
30
31
#  stage_idx          :integer
#  tag                :boolean
#  ref                :string(255)
#  user_id            :integer
#  type               :string(255)
#  target_url         :string(255)
#  description        :string(255)
#  artifacts_file     :text
Douwe Maan's avatar
Douwe Maan committed
32
33
34
#

module Ci
Kamil Trzcinski's avatar
Kamil Trzcinski committed
35
  class Build < CommitStatus
Douwe Maan's avatar
Douwe Maan committed
36
37
38
39
40
41
42
43
    LAZY_ATTRIBUTES = ['trace']

    belongs_to :runner, class_name: 'Ci::Runner'
    belongs_to :trigger_request, class_name: 'Ci::TriggerRequest'

    serialize :options

    validates :coverage, numericality: true, allow_blank: true
Kamil Trzcinski's avatar
Kamil Trzcinski committed
44
    validates_presence_of :ref
Douwe Maan's avatar
Douwe Maan committed
45
46

    scope :unstarted, ->() { where(runner_id: nil) }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
47
    scope :ignore_failures, ->() { where(allow_failure: false) }
Kamil Trzcinski's avatar
Kamil Trzcinski committed
48
    scope :similar, ->(build) { where(ref: build.ref, tag: build.tag, trigger_request_id: build.trigger_request_id) }
Douwe Maan's avatar
Douwe Maan committed
49

Kamil Trzcinski's avatar
Kamil Trzcinski committed
50
51
    mount_uploader :artifacts_file, ArtifactUploader

Douwe Maan's avatar
Douwe Maan committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    acts_as_taggable

    # To prevent db load megabytes of data from trace
    default_scope -> { select(Ci::Build.columns_without_lazy) }

    class << self
      def columns_without_lazy
        (column_names - LAZY_ATTRIBUTES).map do |column_name|
          "#{table_name}.#{column_name}"
        end
      end

      def last_month
        where('created_at > ?', Date.today - 1.month)
      end

      def first_pending
        pending.unstarted.order('created_at ASC').first
      end

      def create_from(build)
        new_build = build.dup
Kamil Trzcinski's avatar
Kamil Trzcinski committed
74
        new_build.status = 'pending'
Douwe Maan's avatar
Douwe Maan committed
75
        new_build.runner_id = nil
Kamil Trzcinski's avatar
Kamil Trzcinski committed
76
        new_build.trigger_request_id = nil
Douwe Maan's avatar
Douwe Maan committed
77
78
79
80
        new_build.save
      end

      def retry(build)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
81
        new_build = Ci::Build.new(status: 'pending')
Kamil Trzcinski's avatar
Kamil Trzcinski committed
82
83
        new_build.ref = build.ref
        new_build.tag = build.tag
Douwe Maan's avatar
Douwe Maan committed
84
85
86
        new_build.options = build.options
        new_build.commands = build.commands
        new_build.tag_list = build.tag_list
87
        new_build.gl_project_id = build.gl_project_id
Douwe Maan's avatar
Douwe Maan committed
88
89
90
91
        new_build.commit_id = build.commit_id
        new_build.name = build.name
        new_build.allow_failure = build.allow_failure
        new_build.stage = build.stage
Kamil Trzcinski's avatar
Kamil Trzcinski committed
92
        new_build.stage_idx = build.stage_idx
Douwe Maan's avatar
Douwe Maan committed
93
94
95
96
97
98
99
        new_build.trigger_request = build.trigger_request
        new_build.save
        new_build
      end
    end

    state_machine :status, initial: :pending do
100
101
102
103
      after_transition pending: :running do |build, transition|
        build.execute_hooks
      end

Douwe Maan's avatar
Douwe Maan committed
104
      after_transition any => [:success, :failed, :canceled] do |build, transition|
105
        return unless build.project
106

107
        build.update_coverage
108
109
        build.commit.create_next_builds(build)
        build.execute_hooks
Douwe Maan's avatar
Douwe Maan committed
110
111
112
      end
    end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
113
114
    def ignored?
      failed? && allow_failure?
Kamil Trzcinski's avatar
Kamil Trzcinski committed
115
116
    end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
117
    def retryable?
118
      project.builds_enabled? && commands.present?
Kamil Trzcinski's avatar
Kamil Trzcinski committed
119
120
121
122
123
124
    end

    def retried?
      !self.commit.latest_builds_for_ref(self.ref).include?(self)
    end

Douwe Maan's avatar
Douwe Maan committed
125
126
    def trace_html
      html = Ci::Ansi2html::convert(trace) if trace.present?
127
      html || ''
Douwe Maan's avatar
Douwe Maan committed
128
129
130
    end

    def timeout
131
      project.build_timeout
Douwe Maan's avatar
Douwe Maan committed
132
133
134
    end

    def variables
135
      predefined_variables + yaml_variables + project_variables + trigger_variables
Douwe Maan's avatar
Douwe Maan committed
136
137
138
139
140
141
142
    end

    def project
      commit.project
    end

    def project_id
Kamil Trzcinski's avatar
Kamil Trzcinski committed
143
      commit.project.id
Douwe Maan's avatar
Douwe Maan committed
144
145
146
147
148
149
150
    end

    def project_name
      project.name
    end

    def repo_url
151
152
153
154
      auth = "gitlab-ci-token:#{token}@"
      project.http_url_to_repo.sub(/^https?:\/\//) do |prefix|
        prefix + auth
      end
Douwe Maan's avatar
Douwe Maan committed
155
156
157
    end

    def allow_git_fetch
158
      project.build_allow_git_fetch
Douwe Maan's avatar
Douwe Maan committed
159
160
161
    end

    def update_coverage
162
163
164
      coverage_regex = project.build_coverage_regex
      return unless coverage_regex
      coverage = extract_coverage(trace, coverage_regex)
Douwe Maan's avatar
Douwe Maan committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178

      if coverage.is_a? Numeric
        update_attributes(coverage: coverage)
      end
    end

    def extract_coverage(text, regex)
      begin
        matches = text.gsub(Regexp.new(regex)).to_a.last
        coverage = matches.gsub(/\d+(\.\d+)?/).first

        if coverage.present?
          coverage.to_f
        end
179
      rescue
Douwe Maan's avatar
Douwe Maan committed
180
181
182
183
184
        # if bad regex or something goes wrong we dont want to interrupt transition
        # so we just silentrly ignore error for now
      end
    end

185
    def raw_trace
Douwe Maan's avatar
Douwe Maan committed
186
187
188
189
190
191
192
      if File.exist?(path_to_trace)
        File.read(path_to_trace)
      else
        # backward compatibility
        read_attribute :trace
      end
    end
193
194
195
196
197
198
199
200
201

    def trace
      trace = raw_trace
      if project && trace.present?
        trace.gsub(project.token, 'xxxxxx')
      else
        trace
      end
    end
Douwe Maan's avatar
Douwe Maan committed
202
203
204
205
206
207
208
209
210
211
212

    def trace=(trace)
      unless Dir.exists? dir_to_trace
        FileUtils.mkdir_p dir_to_trace
      end

      File.write(path_to_trace, trace)
    end

    def dir_to_trace
      File.join(
Valery Sizov's avatar
Valery Sizov committed
213
        Settings.gitlab_ci.builds_path,
Douwe Maan's avatar
Douwe Maan committed
214
215
216
217
218
219
220
221
222
        created_at.utc.strftime("%Y_%m"),
        project.id.to_s
      )
    end

    def path_to_trace
      "#{dir_to_trace}/#{id}.log"
    end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
223
224
225
226
227
    def token
      project.token
    end

    def valid_token? token
Kamil Trzcinski's avatar
Kamil Trzcinski committed
228
      project.valid_runners_token? token
Kamil Trzcinski's avatar
Kamil Trzcinski committed
229
230
    end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
231
232
    def target_url
      Gitlab::Application.routes.url_helpers.
233
        namespace_project_build_url(project.namespace, project, self)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
234
235
    end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
236
237
    def cancel_url
      if active?
Kamil Trzcinski's avatar
Kamil Trzcinski committed
238
        Gitlab::Application.routes.url_helpers.
239
          cancel_namespace_project_build_path(project.namespace, project, self)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
240
241
242
243
      end
    end

    def retry_url
Kamil Trzcinski's avatar
Kamil Trzcinski committed
244
      if retryable?
Kamil Trzcinski's avatar
Kamil Trzcinski committed
245
        Gitlab::Application.routes.url_helpers.
246
          retry_namespace_project_build_path(project.namespace, project, self)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
247
248
249
      end
    end

250
251
252
253
254
255
256
257
258
259
260
261
    def can_be_served?(runner)
      (tag_list - runner.tag_list).empty?
    end

    def any_runners_online?
      project.any_runners? { |runner| runner.active? && runner.online? && can_be_served?(runner) }
    end

    def show_warning?
      pending? && !any_runners_online?
    end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
262
263
264
    def download_url
      if artifacts_file.exists?
        Gitlab::Application.routes.url_helpers.
265
          download_namespace_project_build_path(project.namespace, project, self)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
266
267
268
      end
    end

269
270
    def execute_hooks
      build_data = Gitlab::BuildDataBuilder.build(self)
271
272
      project.execute_hooks(build_data.dup, :build_hooks)
      project.execute_services(build_data.dup, :build_hooks)
273
274
    end

275
276


Douwe Maan's avatar
Douwe Maan committed
277
278
279
280
281
282
283
284
285
286
287
288
289
    private

    def yaml_variables
      if commit.config_processor
        commit.config_processor.variables.map do |key, value|
          { key: key, value: value, public: true }
        end
      else
        []
      end
    end

    def project_variables
290
      project.variables.map do |variable|
Douwe Maan's avatar
Douwe Maan committed
291
292
293
294
295
296
297
298
299
300
301
302
303
        { key: variable.key, value: variable.value, public: false }
      end
    end

    def trigger_variables
      if trigger_request && trigger_request.variables
        trigger_request.variables.map do |key, value|
          { key: key, value: value, public: false }
        end
      else
        []
      end
    end
304
305
306

    def predefined_variables
      variables = []
307
      variables << { key: :CI_BUILD_TAG, value: ref, public: true } if tag?
308
309
310
311
312
      variables << { key: :CI_BUILD_NAME, value: name, public: true }
      variables << { key: :CI_BUILD_STAGE, value: stage, public: true }
      variables << { key: :CI_BUILD_TRIGGERED, value: 'true', public: true } if trigger_request
      variables
    end
Douwe Maan's avatar
Douwe Maan committed
313
314
  end
end