summaryrefslogtreecommitdiff
path: root/app/services/ci/create_job_artifacts_service.rb
blob: f0ffe67510ba9df7ca446eeb7a68dbbc358c778d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true

module Ci
  class CreateJobArtifactsService < ::BaseService
    ArtifactsExistError = Class.new(StandardError)
    OBJECT_STORAGE_ERRORS = [
      Errno::EIO,
      Google::Apis::ServerError,
      Signet::RemoteServerError
    ].freeze

    def execute(job, artifacts_file, params, metadata_file: nil)
      return success if sha256_matches_existing_artifact?(job, params['artifact_type'], artifacts_file)

      artifact, artifact_metadata = build_artifact(job, artifacts_file, params, metadata_file)
      result = parse_artifact(job, artifact)

      return result unless result[:status] == :success

      persist_artifact(job, artifact, artifact_metadata)
    end

    private

    def build_artifact(job, artifacts_file, params, metadata_file)
      expire_in = params['expire_in'] ||
        Gitlab::CurrentSettings.current_application_settings.default_artifacts_expire_in

      artifact = Ci::JobArtifact.new(
        job_id: job.id,
        project: job.project,
        file: artifacts_file,
        file_type: params['artifact_type'],
        file_format: params['artifact_format'],
        file_sha256: artifacts_file.sha256,
        expire_in: expire_in)

      artifact_metadata = if metadata_file
                            Ci::JobArtifact.new(
                              job_id: job.id,
                              project: job.project,
                              file: metadata_file,
                              file_type: :metadata,
                              file_format: :gzip,
                              file_sha256: metadata_file.sha256,
                              expire_in: expire_in)
                          end

      if Feature.enabled?(:keep_latest_artifact_for_ref, job.project)
        artifact.locked = true
        artifact_metadata&.locked = true
      end

      [artifact, artifact_metadata]
    end

    def parse_artifact(job, artifact)
      unless Feature.enabled?(:ci_synchronous_artifact_parsing, job.project, default_enabled: true)
        return success
      end

      case artifact.file_type
      when 'dotenv' then parse_dotenv_artifact(job, artifact)
      when 'cluster_applications' then parse_cluster_applications_artifact(job, artifact)
      else success
      end
    end

    def persist_artifact(job, artifact, artifact_metadata)
      Ci::JobArtifact.transaction do
        artifact.save!
        artifact_metadata&.save!
        unlock_previous_artifacts!(artifact)

        # NOTE: The `artifacts_expire_at` column is already deprecated and to be removed in the near future.
        job.update_column(:artifacts_expire_at, artifact.expire_at)
      end

      success
    rescue ActiveRecord::RecordNotUnique => error
      track_exception(error, job, params)
      error('another artifact of the same type already exists', :bad_request)
    rescue *OBJECT_STORAGE_ERRORS => error
      track_exception(error, job, params)
      error(error.message, :service_unavailable)
    rescue => error
      track_exception(error, job, params)
      error(error.message, :bad_request)
    end

    def unlock_previous_artifacts!(artifact)
      return unless Feature.enabled?(:keep_latest_artifact_for_ref, artifact.job.project)

      Ci::JobArtifact.for_ref(artifact.job.ref, artifact.project_id).locked.update_all(locked: false)
    end

    def sha256_matches_existing_artifact?(job, artifact_type, artifacts_file)
      existing_artifact = job.job_artifacts.find_by_file_type(artifact_type)
      return false unless existing_artifact

      existing_artifact.file_sha256 == artifacts_file.sha256
    end

    def track_exception(error, job, params)
      Gitlab::ErrorTracking.track_exception(error,
        job_id: job.id,
        project_id: job.project_id,
        uploading_type: params['artifact_type']
      )
    end

    def parse_dotenv_artifact(job, artifact)
      Ci::ParseDotenvArtifactService.new(job.project, current_user).execute(artifact)
    end

    def parse_cluster_applications_artifact(job, artifact)
      Clusters::ParseClusterApplicationsArtifactService.new(job, job.user).execute(artifact)
    end
  end
end