build.rb 15.5 KB
Newer Older
1
module Ci
2
  class Build < CommitStatus
3
    include TokenAuthenticatable
4
    include AfterCommitQueue
Rémy Coutable's avatar
Rémy Coutable committed
5
    include Presentable
Shinya Maeda's avatar
Shinya Maeda committed
6
    include Importable
7

8 9
    belongs_to :runner
    belongs_to :trigger_request
10
    belongs_to :erased_by, class_name: 'User'
11

12
    has_many :deployments, as: :deployable
13
    has_one :last_deployment, -> { order('deployments.id DESC') }, as: :deployable, class_name: 'Deployment'
14

15 16 17 18
    # The "environment" field for builds is a String, and is the unexpanded name
    def persisted_environment
      @persisted_environment ||= Environment.find_by(
        name: expanded_environment_name,
19
        project: project
20 21 22
      )
    end

23 24
    serialize :options # rubocop:disable Cop/ActiveRecordSerialize
    serialize :yaml_variables, Gitlab::Serializer::Ci::Variables # rubocop:disable Cop/ActiveRecordSerialize
25

Douwe Maan's avatar
Douwe Maan committed
26 27
    delegate :name, to: :project, prefix: true

28
    validates :coverage, numericality: true, allow_blank: true
Douwe Maan's avatar
Douwe Maan committed
29
    validates :ref, presence: true
30 31

    scope :unstarted, ->() { where(runner_id: nil) }
32
    scope :ignore_failures, ->() { where(allow_failure: false) }
33
    scope :with_artifacts, ->() { where.not(artifacts_file: [nil, '']) }
34
    scope :with_artifacts_not_expired, ->() { with_artifacts.where('artifacts_expire_at IS NULL OR artifacts_expire_at > ?', Time.now) }
35
    scope :with_expired_artifacts, ->() { with_artifacts.where('artifacts_expire_at < ?', Time.now) }
36
    scope :last_month, ->() { where('created_at > ?', Date.today - 1.month) }
37
    scope :manual_actions, ->() { where(when: :manual, status: COMPLETED_STATUSES + [:manual]) }
38
    scope :ref_protected, -> { where(protected: true) }
39

40
    mount_uploader :artifacts_file, ArtifactUploader
41
    mount_uploader :artifacts_metadata, ArtifactUploader
42

43 44
    acts_as_taggable

45 46
    add_authentication_token_field :token

47
    before_save :update_artifacts_size, if: :artifacts_file_changed?
48
    before_save :ensure_token
49
    before_destroy { unscoped_project }
50

51
    after_create do |build|
52
      run_after_commit { BuildHooksWorker.perform_async(build.id) }
53 54
    end

55 56
    after_commit :update_project_statistics_after_save, on: [:create, :update]
    after_commit :update_project_statistics, on: :destroy
57 58

    class << self
59 60 61 62 63 64
      # This is needed for url_for to work,
      # as the controller is JobsController
      def model_name
        ActiveModel::Name.new(self, nil, 'job')
      end

65 66 67 68
      def first_pending
        pending.unstarted.order('created_at ASC').first
      end

69
      def retry(build, current_user)
70 71 72
        Ci::RetryBuildService
          .new(build.project, current_user)
          .execute(build)
73 74 75
      end
    end

76
    state_machine :status do
77 78
      event :actionize do
        transition created: :manual
79 80
      end

81 82
      after_transition any => [:pending] do |build|
        build.run_after_commit do
Kim "BKC" Carlbäcker's avatar
Kim "BKC" Carlbäcker committed
83
          BuildQueueWorker.perform_async(id)
84 85 86
        end
      end

87
      after_transition pending: :running do |build|
88 89 90
        build.run_after_commit do
          BuildHooksWorker.perform_async(id)
        end
91 92
      end

93
      after_transition any => [:success, :failed, :canceled] do |build|
94
        build.run_after_commit do
95
          BuildFinishedWorker.perform_async(id)
96
        end
97
      end
98

99
      after_transition any => [:success] do |build|
100 101
        build.run_after_commit do
          BuildSuccessWorker.perform_async(id)
102 103
        end
      end
104

105 106
      before_transition any => [:failed] do |build|
        next if build.retries_max.zero?
107

108 109
        if build.retries_count < build.retries_max
          Ci::Build.retry(build, build.user)
110 111
        end
      end
112 113
    end

114
    def detailed_status(current_user)
115 116 117
      Gitlab::Ci::Status::Build::Factory
        .new(self, current_user)
        .fabricate!
Kamil Trzcinski's avatar
Kamil Trzcinski committed
118 119
    end

120
    def other_actions
121
      pipeline.manual_actions.where.not(name: name)
122 123
    end

124
    def playable?
125
      action? && (manual? || complete?)
126 127
    end

128
    def action?
129 130 131
      self.when == 'manual'
    end

132
    def play(current_user)
133 134 135
      Ci::PlayBuildService
        .new(project, current_user)
        .execute(self)
136 137
    end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
138 139 140 141
    def cancelable?
      active?
    end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
142
    def retryable?
143
      success? || failed? || canceled?
Kamil Trzcinski's avatar
Kamil Trzcinski committed
144
    end
145 146 147 148 149 150 151 152

    def retries_count
      pipeline.builds.retried.where(name: self.name).count
    end

    def retries_max
      self.options.fetch(:retry, 0).to_i
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
153

154 155
    def latest?
      !retried?
156 157
    end

158
    def expanded_environment_name
159
      ExpandVariables.expand(environment, simple_variables) if environment
160 161
    end

162
    def has_environment?
163
      environment.present?
164 165
    end

166
    def starts_environment?
167
      has_environment? && self.environment_action == 'start'
168 169 170
    end

    def stops_environment?
171
      has_environment? && self.environment_action == 'stop'
172 173 174
    end

    def environment_action
175
      self.options.fetch(:environment, {}).fetch(:action, 'start') if self.options
176 177 178 179
    end

    def outdated_deployment?
      success? && !last_deployment.try(:last?)
180
    end
181

182 183
    def depends_on_builds
      # Get builds of the same type
184
      latest_builds = self.pipeline.builds.latest
185 186 187 188 189

      # Return builds from previous stages
      latest_builds.where('stage_idx < ?', stage_idx)
    end

190
    def timeout
191
      project.build_timeout
192 193
    end

Nick Thomas's avatar
Nick Thomas committed
194 195 196 197 198 199
    # A slugified version of the build ref, suitable for inclusion in URLs and
    # domain names. Rules:
    #
    #   * Lowercased
    #   * Anything not matching [a-z0-9-] is replaced with a -
    #   * Maximum length is 63 bytes
Shinya Maeda's avatar
Shinya Maeda committed
200
    #   * First/Last Character is not a hyphen
Nick Thomas's avatar
Nick Thomas committed
201
    def ref_slug
202
      Gitlab::Utils.slugify(ref.to_s)
Nick Thomas's avatar
Nick Thomas committed
203 204
    end

205
    # Variables whose value does not depend on environment
206
    def simple_variables
Lin Jen-Shin's avatar
Lin Jen-Shin committed
207 208 209 210 211 212
      variables(environment: nil)
    end

    # All variables, including those dependent on environment, which could
    # contain unexpanded variables.
    def variables(environment: persisted_environment)
213
      variables = predefined_variables
214 215 216 217 218
      variables += project.predefined_variables
      variables += pipeline.predefined_variables
      variables += runner.predefined_variables if runner
      variables += project.container_registry_variables
      variables += project.deployment_variables if has_environment?
219
      variables += project.auto_devops_variables
220 221
      variables += yaml_variables
      variables += user_variables
Shinya Maeda's avatar
Shinya Maeda committed
222
      variables += project.group.secret_variables_for(ref, project).map(&:to_runner_variable) if project.group
Lin Jen-Shin's avatar
Lin Jen-Shin committed
223
      variables += secret_variables(environment: environment)
224
      variables += trigger_request.user_variables if trigger_request
225
      variables += pipeline.variables.map(&:to_runner_variable)
Shinya Maeda's avatar
Shinya Maeda committed
226
      variables += pipeline.pipeline_schedule.job_variables if pipeline.pipeline_schedule
Lin Jen-Shin's avatar
Lin Jen-Shin committed
227
      variables += persisted_environment_variables if environment
228

Lin Jen-Shin's avatar
Lin Jen-Shin committed
229
      variables
230 231
    end

232
    def merge_request
233
      return @merge_request if defined?(@merge_request)
Z.J. van de Weg's avatar
Z.J. van de Weg committed
234

235 236 237 238 239
      @merge_request ||=
        begin
          merge_requests = MergeRequest.includes(:merge_request_diff)
            .where(source_branch: ref,
                   source_project: pipeline.project)
Z.J. van de Weg's avatar
Z.J. van de Weg committed
240
            .reorder(iid: :desc)
241 242

          merge_requests.find do |merge_request|
243
            merge_request.commit_shas.include?(pipeline.sha)
244 245
          end
        end
246 247
    end

248
    def repo_url
Kamil Trzcinski's avatar
Kamil Trzcinski committed
249
      auth = "gitlab-ci-token:#{ensure_token!}@"
250 251 252
      project.http_url_to_repo.sub(/^https?:\/\//) do |prefix|
        prefix + auth
      end
253 254 255
    end

    def allow_git_fetch
256
      project.build_allow_git_fetch
257 258 259
    end

    def update_coverage
260
      coverage = trace.extract_coverage(coverage_regex)
261
      update_attributes(coverage: coverage) if coverage.present?
262 263
    end

264 265
    def trace
      Gitlab::Ci::Trace.new(self)
266 267
    end

268
    def has_trace?
269
      trace.exist?
270 271
    end

272 273
    def trace=(data)
      raise NotImplementedError
Tomasz Maczukin's avatar
Tomasz Maczukin committed
274 275
    end

276 277
    def old_trace
      read_attribute(:trace)
278 279
    end

280 281 282
    def erase_old_trace!
      write_attribute(:trace, nil)
      save
283 284
    end

285 286 287 288
    def needs_touch?
      Time.now - updated_at > 15.minutes.to_i
    end

Lin Jen-Shin's avatar
Lin Jen-Shin committed
289
    def valid_token?(token)
290
      self.token && ActiveSupport::SecurityUtils.variable_size_secure_compare(token, self.token)
291 292
    end

293 294 295 296
    def has_tags?
      tag_list.any?
    end

297
    def any_runners_online?
298
      project.any_runners? { |runner| runner.active? && runner.online? && runner.can_pick?(self) }
299 300
    end

301
    def stuck?
302 303 304
      pending? && !any_runners_online?
    end

305
    def execute_hooks
306
      return unless project
307
      build_data = Gitlab::DataBuilder::Build.build(self)
308 309
      project.execute_hooks(build_data.dup, :job_hooks)
      project.execute_services(build_data.dup, :job_hooks)
310
      PagesService.new(build_data).execute
Josh Frye's avatar
Josh Frye committed
311
      project.running_or_pending_build_count(force: true)
312 313
    end

314
    def artifacts?
315
      !artifacts_expired? && artifacts_file.exists?
316 317
    end

318
    def artifacts_metadata?
319
      artifacts? && artifacts_metadata.exists?
320 321
    end

322
    def artifacts_metadata_entry(path, **options)
323 324 325 326 327 328
      metadata = Gitlab::Ci::Build::Artifacts::Metadata.new(
        artifacts_metadata.path,
        path,
        **options)

      metadata.to_entry
329 330
    end

331 332 333
    def erase_artifacts!
      remove_artifacts_file!
      remove_artifacts_metadata!
334
      save
335 336
    end

337 338 339
    def erase(opts = {})
      return false unless erasable?

340
      erase_artifacts!
341 342 343 344 345 346 347 348 349 350 351 352
      erase_trace!
      update_erased!(opts[:erased_by])
    end

    def erasable?
      complete? && (artifacts? || has_trace?)
    end

    def erased?
      !self.erased_at.nil?
    end

353
    def artifacts_expired?
354
      artifacts_expire_at && artifacts_expire_at < Time.now
355 356
    end

357 358 359 360 361
    def artifacts_expire_in
      artifacts_expire_at - Time.now if artifacts_expire_at
    end

    def artifacts_expire_in=(value)
362 363
      self.artifacts_expire_at =
        if value
364
          ChronicDuration.parse(value)&.seconds&.from_now
365
        end
366 367
    end

368
    def has_expiring_artifacts?
Z.J. van de Weg's avatar
Z.J. van de Weg committed
369
      artifacts_expire_at.present? && artifacts_expire_at > Time.now
370 371
    end

372
    def keep_artifacts!
373 374 375
      self.update(artifacts_expire_at: nil)
    end

376
    def coverage_regex
377
      super || project.try(:build_coverage_regex)
378 379
    end

380 381
    def when
      read_attribute(:when) || build_attributes_from_config[:when] || 'on_success'
382 383
    end

384 385
    def yaml_variables
      read_attribute(:yaml_variables) || build_attributes_from_config[:yaml_variables] || []
386 387
    end

388 389 390 391 392
    def user_variables
      return [] if user.blank?

      [
        { key: 'GITLAB_USER_ID', value: user.id.to_s, public: true },
393
        { key: 'GITLAB_USER_EMAIL', value: user.email, public: true },
394
        { key: 'GITLAB_USER_LOGIN', value: user.username, public: true },
395
        { key: 'GITLAB_USER_NAME', value: user.name, public: true }
396 397 398
      ]
    end

Lin Jen-Shin's avatar
Lin Jen-Shin committed
399 400 401 402 403
    def secret_variables(environment: persisted_environment)
      project.secret_variables_for(ref: ref, environment: environment)
        .map(&:to_runner_variable)
    end

404
    def steps
Tomasz Maczukin's avatar
Tomasz Maczukin committed
405 406
      [Gitlab::Ci::Build::Step.from_commands(self),
       Gitlab::Ci::Build::Step.from_after_script(self)].compact
407 408 409
    end

    def image
410
      Gitlab::Ci::Build::Image.from_image(self)
411 412 413
    end

    def services
414
      Gitlab::Ci::Build::Image.from_services(self)
415 416 417
    end

    def artifacts
418
      [options[:artifacts]]
419 420 421
    end

    def cache
422
      [options[:cache]]
423 424
    end

425
    def credentials
426
      Gitlab::Ci::Build::Credentials::Factory.new(self).create!
427 428
    end

429
    def dependencies
430 431
      return [] if empty_dependencies?

432 433
      depended_jobs = depends_on_builds

434
      return depended_jobs unless options[:dependencies].present?
435

436 437
      depended_jobs.select do |job|
        options[:dependencies].include?(job.name)
438 439 440
      end
    end

441 442 443 444
    def empty_dependencies?
      options[:dependencies]&.empty?
    end

445 446 447 448 449 450 451 452 453
    def hide_secrets(trace)
      return unless trace

      trace = trace.dup
      Ci::MaskSecret.mask!(trace, project.runners_token) if project
      Ci::MaskSecret.mask!(trace, token)
      trace
    end

454
    def serializable_hash(options = {})
455
      super(options).merge(when: read_attribute(:when))
456 457
    end

458 459
    private

460
    def update_artifacts_size
461 462
      self.artifacts_size = if artifacts_file.exists?
                              artifacts_file.size
463 464
                            else
                              nil
465
                            end
466 467
    end

468
    def erase_trace!
469
      trace.erase!
470 471 472
    end

    def update_erased!(user = nil)
473
      self.update(erased_by: user, erased_at: Time.now, artifacts_expire_at: nil)
474 475
    end

476
    def unscoped_project
477
      @unscoped_project ||= Project.unscoped.find_by(id: project_id)
478 479
    end

480 481
    CI_REGISTRY_USER = 'gitlab-ci-token'.freeze

482
    def predefined_variables
483 484 485
      variables = [
        { key: 'CI', value: 'true', public: true },
        { key: 'GITLAB_CI', value: 'true', public: true },
486 487 488 489 490 491 492
        { key: 'CI_SERVER_NAME', value: 'GitLab', public: true },
        { key: 'CI_SERVER_VERSION', value: Gitlab::VERSION, public: true },
        { key: 'CI_SERVER_REVISION', value: Gitlab::REVISION, public: true },
        { key: 'CI_JOB_ID', value: id.to_s, public: true },
        { key: 'CI_JOB_NAME', value: name, public: true },
        { key: 'CI_JOB_STAGE', value: stage, public: true },
        { key: 'CI_JOB_TOKEN', value: token, public: false },
Z.J. van de Weg's avatar
Z.J. van de Weg committed
493
        { key: 'CI_COMMIT_SHA', value: sha, public: true },
494 495 496 497 498 499 500 501 502 503 504 505 506
        { key: 'CI_COMMIT_REF_NAME', value: ref, public: true },
        { key: 'CI_COMMIT_REF_SLUG', value: ref_slug, public: true },
        { key: 'CI_REGISTRY_USER', value: CI_REGISTRY_USER, public: true },
        { key: 'CI_REGISTRY_PASSWORD', value: token, public: false },
        { key: 'CI_REPOSITORY_URL', value: repo_url, public: false }
      ]

      variables << { key: "CI_COMMIT_TAG", value: ref, public: true } if tag?
      variables << { key: "CI_PIPELINE_TRIGGERED", value: 'true', public: true } if trigger_request
      variables << { key: "CI_JOB_MANUAL", value: 'true', public: true } if action?
      variables.concat(legacy_variables)
    end

507
    def persisted_environment_variables
508 509
      return [] unless persisted_environment

510 511
      variables = persisted_environment.predefined_variables

512 513 514
      # Here we're passing unexpanded environment_url for runner to expand,
      # and we need to make sure that CI_ENVIRONMENT_NAME and
      # CI_ENVIRONMENT_SLUG so on are available for the URL be expanded.
515
      variables << { key: 'CI_ENVIRONMENT_URL', value: environment_url, public: true } if environment_url
516 517

      variables
518 519
    end

520 521
    def legacy_variables
      variables = [
522 523 524 525 526
        { key: 'CI_BUILD_ID', value: id.to_s, public: true },
        { key: 'CI_BUILD_TOKEN', value: token, public: false },
        { key: 'CI_BUILD_REF', value: sha, public: true },
        { key: 'CI_BUILD_BEFORE_SHA', value: before_sha, public: true },
        { key: 'CI_BUILD_REF_NAME', value: ref, public: true },
Nick Thomas's avatar
Nick Thomas committed
527
        { key: 'CI_BUILD_REF_SLUG', value: ref_slug, public: true },
528
        { key: 'CI_BUILD_NAME', value: name, public: true },
529
        { key: 'CI_BUILD_STAGE', value: stage, public: true }
530
      ]
531 532 533 534

      variables << { key: "CI_BUILD_TAG", value: ref, public: true } if tag?
      variables << { key: "CI_BUILD_TRIGGERED", value: 'true', public: true } if trigger_request
      variables << { key: "CI_BUILD_MANUAL", value: 'true', public: true } if action?
535 536
      variables
    end
537

538
    def environment_url
539
      options&.dig(:environment, :url) || persisted_environment&.external_url
540 541
    end

542 543
    def build_attributes_from_config
      return {} unless pipeline.config_processor
544

545 546
      pipeline.config_processor.build_attributes(name)
    end
547

548
    def update_project_statistics
549 550
      return unless project

551 552
      ProjectCacheWorker.perform_async(project_id, [], [:build_artifacts_size])
    end
553 554 555 556 557 558

    def update_project_statistics_after_save
      if previous_changes.include?('artifacts_size')
        update_project_statistics
      end
    end
559 560
  end
end