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

9 10
    MissingDependenciesError = Class.new(StandardError)

11
    belongs_to :project, inverse_of: :builds
12 13
    belongs_to :runner
    belongs_to :trigger_request
14
    belongs_to :erased_by, class_name: 'User'
15

16
    has_many :deployments, as: :deployable
17

18
    has_one :last_deployment, -> { order('deployments.id DESC') }, as: :deployable, class_name: 'Deployment'
19
    has_many :trace_sections, class_name: 'Ci::BuildTraceSection'
20

21
    has_many :job_artifacts, class_name: 'Ci::JobArtifact', foreign_key: :job_id, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
22 23
    has_one :job_artifacts_archive, -> { where(file_type: Ci::JobArtifact.file_types[:archive]) }, class_name: 'Ci::JobArtifact', inverse_of: :job, foreign_key: :job_id
    has_one :job_artifacts_metadata, -> { where(file_type: Ci::JobArtifact.file_types[:metadata]) }, class_name: 'Ci::JobArtifact', inverse_of: :job, foreign_key: :job_id
24

25 26 27 28
    # 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,
29
        project: project
30 31 32
      )
    end

33 34
    serialize :options # rubocop:disable Cop/ActiveRecordSerialize
    serialize :yaml_variables, Gitlab::Serializer::Ci::Variables # rubocop:disable Cop/ActiveRecordSerialize
35

Douwe Maan's avatar
Douwe Maan committed
36 37
    delegate :name, to: :project, prefix: true

38
    validates :coverage, numericality: true, allow_blank: true
Douwe Maan's avatar
Douwe Maan committed
39
    validates :ref, presence: true
40 41

    scope :unstarted, ->() { where(runner_id: nil) }
42
    scope :ignore_failures, ->() { where(allow_failure: false) }
43
    scope :with_artifacts, ->() do
Kamil Trzcinski's avatar
Kamil Trzcinski committed
44 45
      where('(artifacts_file IS NOT NULL AND artifacts_file <> ?) OR EXISTS (?)',
        '', Ci::JobArtifact.select(1).where('ci_builds.id = ci_job_artifacts.job_id'))
46
    end
47
    scope :with_artifacts_not_expired, ->() { with_artifacts.where('artifacts_expire_at IS NULL OR artifacts_expire_at > ?', Time.now) }
48
    scope :with_expired_artifacts, ->() { with_artifacts.where('artifacts_expire_at < ?', Time.now) }
49
    scope :last_month, ->() { where('created_at > ?', Date.today - 1.month) }
50
    scope :manual_actions, ->() { where(when: :manual, status: COMPLETED_STATUSES + [:manual]) }
51
    scope :ref_protected, -> { where(protected: true) }
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    scope :matches_tag_ids, -> (tag_ids) do
      matcher = ::ActsAsTaggableOn::Tagging
        .where(taggable_type: CommitStatus)
        .where(context: 'tags')
        .where('taggable_id = ci_builds.id')
        .where.not(tag_id: tag_ids).select('1')

      where("NOT EXISTS (?)", matcher)
    end

    scope :with_any_tags, -> do
      matcher = ::ActsAsTaggableOn::Tagging
        .where(taggable_type: CommitStatus)
        .where(context: 'tags')
        .where('taggable_id = ci_builds.id').select('1')

      where("EXISTS (?)", matcher)
    end

72 73
    mount_uploader :legacy_artifacts_file, LegacyArtifactUploader, mount_on: :artifacts_file
    mount_uploader :legacy_artifacts_metadata, LegacyArtifactUploader, mount_on: :artifacts_metadata
74

75 76
    acts_as_taggable

77 78
    add_authentication_token_field :token

79
    before_save :update_artifacts_size, if: :artifacts_file_changed?
80
    before_save :ensure_token
81
    before_destroy { unscoped_project }
82

83
    after_create unless: :importing? do |build|
84
      run_after_commit { BuildHooksWorker.perform_async(build.id) }
85 86
    end

87 88
    after_commit :update_project_statistics_after_save, on: [:create, :update]
    after_commit :update_project_statistics, on: :destroy
89 90

    class << self
91 92 93 94 95 96
      # This is needed for url_for to work,
      # as the controller is JobsController
      def model_name
        ActiveModel::Name.new(self, nil, 'job')
      end

97 98 99 100
      def first_pending
        pending.unstarted.order('created_at ASC').first
      end

101
      def retry(build, current_user)
102 103 104
        Ci::RetryBuildService
          .new(build.project, current_user)
          .execute(build)
105 106 107
      end
    end

108
    state_machine :status do
109 110
      event :actionize do
        transition created: :manual
111 112
      end

113 114
      after_transition any => [:pending] do |build|
        build.run_after_commit do
Kim "BKC" Carlbäcker's avatar
Kim "BKC" Carlbäcker committed
115
          BuildQueueWorker.perform_async(id)
116 117 118
        end
      end

119
      after_transition pending: :running do |build|
120 121 122
        build.run_after_commit do
          BuildHooksWorker.perform_async(id)
        end
123 124
      end

125
      after_transition any => [:success, :failed, :canceled] do |build|
126
        build.run_after_commit do
127
          BuildFinishedWorker.perform_async(id)
128
        end
129
      end
130

131
      after_transition any => [:success] do |build|
132 133
        build.run_after_commit do
          BuildSuccessWorker.perform_async(id)
134 135
        end
      end
136

137
      before_transition any => [:failed] do |build|
138
        next unless build.project
139
        next if build.retries_max.zero?
140

141 142
        if build.retries_count < build.retries_max
          Ci::Build.retry(build, build.user)
143 144
        end
      end
145 146

      before_transition any => [:running] do |build|
147
        build.validates_dependencies! unless Feature.enabled?('ci_disable_validates_dependencies')
148
      end
149 150
    end

151
    def detailed_status(current_user)
152 153 154
      Gitlab::Ci::Status::Build::Factory
        .new(self, current_user)
        .fabricate!
Kamil Trzcinski's avatar
Kamil Trzcinski committed
155 156
    end

157
    def other_actions
158
      pipeline.manual_actions.where.not(name: name)
159 160
    end

161
    def playable?
162
      action? && (manual? || complete?)
163 164
    end

165
    def action?
166 167 168
      self.when == 'manual'
    end

169
    def play(current_user)
170 171 172
      Ci::PlayBuildService
        .new(project, current_user)
        .execute(self)
173 174
    end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
175 176 177 178
    def cancelable?
      active?
    end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
179
    def retryable?
180
      success? || failed? || canceled?
Kamil Trzcinski's avatar
Kamil Trzcinski committed
181
    end
182 183 184 185 186 187 188 189

    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
190

191 192
    def latest?
      !retried?
193 194
    end

195
    def expanded_environment_name
196
      ExpandVariables.expand(environment, simple_variables) if environment
197 198
    end

199
    def has_environment?
200
      environment.present?
201 202
    end

203
    def starts_environment?
204
      has_environment? && self.environment_action == 'start'
205 206 207
    end

    def stops_environment?
208
      has_environment? && self.environment_action == 'stop'
209 210 211
    end

    def environment_action
212
      self.options.fetch(:environment, {}).fetch(:action, 'start') if self.options
213 214 215 216
    end

    def outdated_deployment?
      success? && !last_deployment.try(:last?)
217
    end
218

219 220
    def depends_on_builds
      # Get builds of the same type
221
      latest_builds = self.pipeline.builds.latest
222 223 224 225 226

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

227
    def timeout
228
      project.build_timeout
229 230
    end

231
    def triggered_by?(current_user)
232 233 234
      user == current_user
    end

Nick Thomas's avatar
Nick Thomas committed
235 236 237 238 239 240
    # 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
241
    #   * First/Last Character is not a hyphen
Nick Thomas's avatar
Nick Thomas committed
242
    def ref_slug
243
      Gitlab::Utils.slugify(ref.to_s)
Nick Thomas's avatar
Nick Thomas committed
244 245
    end

246
    # Variables whose value does not depend on environment
247
    def simple_variables
Lin Jen-Shin's avatar
Lin Jen-Shin committed
248 249 250 251 252 253
      variables(environment: nil)
    end

    # All variables, including those dependent on environment, which could
    # contain unexpanded variables.
    def variables(environment: persisted_environment)
254
      variables = predefined_variables
255 256 257 258 259
      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?
260
      variables += project.auto_devops_variables
261 262
      variables += yaml_variables
      variables += user_variables
Shinya Maeda's avatar
Shinya Maeda committed
263
      variables += project.group.secret_variables_for(ref, project).map(&:to_runner_variable) if project.group
Lin Jen-Shin's avatar
Lin Jen-Shin committed
264
      variables += secret_variables(environment: environment)
265
      variables += trigger_request.user_variables if trigger_request
266
      variables += pipeline.variables.map(&:to_runner_variable)
Shinya Maeda's avatar
Shinya Maeda committed
267
      variables += pipeline.pipeline_schedule.job_variables if pipeline.pipeline_schedule
Lin Jen-Shin's avatar
Lin Jen-Shin committed
268
      variables += persisted_environment_variables if environment
269

Lin Jen-Shin's avatar
Lin Jen-Shin committed
270
      variables
271 272
    end

273 274 275 276
    def features
      { trace_sections: true }
    end

277
    def merge_request
278
      return @merge_request if defined?(@merge_request)
Z.J. van de Weg's avatar
Z.J. van de Weg committed
279

280 281
      @merge_request ||=
        begin
282
          merge_requests = MergeRequest.includes(:latest_merge_request_diff)
283 284
            .where(source_branch: ref,
                   source_project: pipeline.project)
Z.J. van de Weg's avatar
Z.J. van de Weg committed
285
            .reorder(iid: :desc)
286 287

          merge_requests.find do |merge_request|
288
            merge_request.commit_shas.include?(pipeline.sha)
289 290
          end
        end
291 292
    end

293
    def repo_url
Kamil Trzcinski's avatar
Kamil Trzcinski committed
294
      auth = "gitlab-ci-token:#{ensure_token!}@"
295
      project.http_url_to_repo.sub(%r{^https?://}) do |prefix|
296 297
        prefix + auth
      end
298 299 300
    end

    def allow_git_fetch
301
      project.build_allow_git_fetch
302 303 304
    end

    def update_coverage
305
      coverage = trace.extract_coverage(coverage_regex)
306
      update_attributes(coverage: coverage) if coverage.present?
307 308
    end

309
    def parse_trace_sections!
310
      ExtractSectionsFromBuildTraceService.new(project, user).execute(self)
311 312
    end

313 314
    def trace
      Gitlab::Ci::Trace.new(self)
315 316
    end

317
    def has_trace?
318
      trace.exist?
319 320
    end

321 322
    def trace=(data)
      raise NotImplementedError
Tomasz Maczukin's avatar
Tomasz Maczukin committed
323 324
    end

325 326
    def old_trace
      read_attribute(:trace)
327 328
    end

329 330 331
    def erase_old_trace!
      write_attribute(:trace, nil)
      save
332 333
    end

334 335 336 337
    def needs_touch?
      Time.now - updated_at > 15.minutes.to_i
    end

Lin Jen-Shin's avatar
Lin Jen-Shin committed
338
    def valid_token?(token)
339
      self.token && ActiveSupport::SecurityUtils.variable_size_secure_compare(token, self.token)
340 341
    end

342 343 344 345
    def has_tags?
      tag_list.any?
    end

346
    def any_runners_online?
347
      project.any_runners? { |runner| runner.active? && runner.online? && runner.can_pick?(self) }
348 349
    end

350
    def stuck?
351 352 353
      pending? && !any_runners_online?
    end

354
    def execute_hooks
355
      return unless project
356

357
      build_data = Gitlab::DataBuilder::Build.build(self)
358 359
      project.execute_hooks(build_data.dup, :job_hooks)
      project.execute_services(build_data.dup, :job_hooks)
360
      PagesService.new(build_data).execute
Josh Frye's avatar
Josh Frye committed
361
      project.running_or_pending_build_count(force: true)
362 363
    end

364
    def artifacts_metadata_entry(path, **options)
365 366 367 368 369 370
      metadata = Gitlab::Ci::Build::Artifacts::Metadata.new(
        artifacts_metadata.path,
        path,
        **options)

      metadata.to_entry
371 372
    end

373 374 375
    def erase_artifacts!
      remove_artifacts_file!
      remove_artifacts_metadata!
376
      save
377 378
    end

379 380 381
    def erase(opts = {})
      return false unless erasable?

382
      erase_artifacts!
383 384 385 386 387 388 389 390 391 392 393 394
      erase_trace!
      update_erased!(opts[:erased_by])
    end

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

    def erased?
      !self.erased_at.nil?
    end

395
    def artifacts_expired?
396
      artifacts_expire_at && artifacts_expire_at < Time.now
397 398
    end

399 400 401 402 403
    def artifacts_expire_in
      artifacts_expire_at - Time.now if artifacts_expire_at
    end

    def artifacts_expire_in=(value)
404 405
      self.artifacts_expire_at =
        if value
406
          ChronicDuration.parse(value)&.seconds&.from_now
407
        end
408 409
    end

410
    def has_expiring_artifacts?
Z.J. van de Weg's avatar
Z.J. van de Weg committed
411
      artifacts_expire_at.present? && artifacts_expire_at > Time.now
412 413
    end

414
    def keep_artifacts!
415
      self.update(artifacts_expire_at: nil)
416
      self.job_artifacts.update_all(expire_at: nil)
417 418
    end

419
    def coverage_regex
420
      super || project.try(:build_coverage_regex)
421 422
    end

423 424
    def when
      read_attribute(:when) || build_attributes_from_config[:when] || 'on_success'
425 426
    end

427 428
    def yaml_variables
      read_attribute(:yaml_variables) || build_attributes_from_config[:yaml_variables] || []
429 430
    end

431 432 433 434 435
    def user_variables
      return [] if user.blank?

      [
        { key: 'GITLAB_USER_ID', value: user.id.to_s, public: true },
436
        { key: 'GITLAB_USER_EMAIL', value: user.email, public: true },
437
        { key: 'GITLAB_USER_LOGIN', value: user.username, public: true },
438
        { key: 'GITLAB_USER_NAME', value: user.name, public: true }
439 440 441
      ]
    end

Lin Jen-Shin's avatar
Lin Jen-Shin committed
442 443 444 445 446
    def secret_variables(environment: persisted_environment)
      project.secret_variables_for(ref: ref, environment: environment)
        .map(&:to_runner_variable)
    end

447
    def steps
Tomasz Maczukin's avatar
Tomasz Maczukin committed
448 449
      [Gitlab::Ci::Build::Step.from_commands(self),
       Gitlab::Ci::Build::Step.from_after_script(self)].compact
450 451 452
    end

    def image
453
      Gitlab::Ci::Build::Image.from_image(self)
454 455 456
    end

    def services
457
      Gitlab::Ci::Build::Image.from_services(self)
458 459
    end

460
    def artifacts
461
      [options[:artifacts]]
462 463 464
    end

    def cache
Matija Čupić's avatar
Matija Čupić committed
465 466 467 468
      cache = options[:cache]

      if cache && project.jobs_cache_index
        cache = cache.merge(
469
          key: "#{cache[:key]}_#{project.jobs_cache_index}")
470
      end
Matija Čupić's avatar
Matija Čupić committed
471 472

      [cache]
473 474
    end

475
    def credentials
476
      Gitlab::Ci::Build::Credentials::Factory.new(self).create!
477 478
    end

479
    def dependencies
480 481
      return [] if empty_dependencies?

482 483
      depended_jobs = depends_on_builds

484
      return depended_jobs unless options[:dependencies].present?
485

486 487
      depended_jobs.select do |job|
        options[:dependencies].include?(job.name)
488 489 490
      end
    end

491 492 493 494
    def empty_dependencies?
      options[:dependencies]&.empty?
    end

495
    def validates_dependencies!
496 497
      dependencies.each do |dependency|
        raise MissingDependenciesError unless dependency.valid_dependency?
498
      end
499 500
    end

Shinya Maeda's avatar
Shinya Maeda committed
501 502 503 504 505 506 507
    def valid_dependency?
      return false if artifacts_expired?
      return false if erased?

      true
    end

508 509 510 511
    def hide_secrets(trace)
      return unless trace

      trace = trace.dup
512 513
      Gitlab::Ci::MaskSecret.mask!(trace, project.runners_token) if project
      Gitlab::Ci::MaskSecret.mask!(trace, token)
514 515 516
      trace
    end

517
    def serializable_hash(options = {})
518
      super(options).merge(when: read_attribute(:when))
519 520
    end

521 522
    private

523
    def update_artifacts_size
Kamil Trzcinski's avatar
Kamil Trzcinski committed
524
      self.artifacts_size = legacy_artifacts_file&.size
525 526
    end

527
    def erase_trace!
528
      trace.erase!
529 530 531
    end

    def update_erased!(user = nil)
532
      self.update(erased_by: user, erased_at: Time.now, artifacts_expire_at: nil)
533 534
    end

535
    def unscoped_project
536
      @unscoped_project ||= Project.unscoped.find_by(id: project_id)
537 538
    end

539 540
    CI_REGISTRY_USER = 'gitlab-ci-token'.freeze

541
    def predefined_variables
542 543 544
      variables = [
        { key: 'CI', value: 'true', public: true },
        { key: 'GITLAB_CI', value: 'true', public: true },
545 546 547 548 549 550 551
        { 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
552
        { key: 'CI_COMMIT_SHA', value: sha, public: true },
553 554 555 556 557 558 559 560 561 562 563 564 565
        { 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

566
    def persisted_environment_variables
567 568
      return [] unless persisted_environment

569 570
      variables = persisted_environment.predefined_variables

571 572 573
      # 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.
574
      variables << { key: 'CI_ENVIRONMENT_URL', value: environment_url, public: true } if environment_url
575 576

      variables
577 578
    end

579 580
    def legacy_variables
      variables = [
581 582 583 584 585
        { 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
586
        { key: 'CI_BUILD_REF_SLUG', value: ref_slug, public: true },
587
        { key: 'CI_BUILD_NAME', value: name, public: true },
588
        { key: 'CI_BUILD_STAGE', value: stage, public: true }
589
      ]
590 591 592 593

      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?
594 595
      variables
    end
596

597
    def environment_url
598
      options&.dig(:environment, :url) || persisted_environment&.external_url
599 600
    end

601 602
    def build_attributes_from_config
      return {} unless pipeline.config_processor
603

604 605
      pipeline.config_processor.build_attributes(name)
    end
606

607 608 609 610 611
    def update_project_statistics
      return unless project

      ProjectCacheWorker.perform_async(project_id, [], [:build_artifacts_size])
    end
612 613 614 615 616 617

    def update_project_statistics_after_save
      if previous_changes.include?('artifacts_size')
        update_project_statistics
      end
    end
618 619
  end
end