builds.rb 7.8 KB
Newer Older
D
Douwe Maan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
module Ci
  module API
    # Builds API
    class Builds < Grape::API
      resource :builds do
        # Runs oldest pending build by runner - Runners only
        #
        # Parameters:
        #   token (required) - The uniq token of runner
        #
        # Example Request:
        #   POST /builds/register
        post "register" do
          authenticate_runner!
          required_attributes! [:token]
          not_found! unless current_runner.active?
17
          update_runner_info
D
Douwe Maan 已提交
18

K
Kim "BKC" Carlbäcker 已提交
19
          if current_runner.is_runner_queue_value_latest?(params[:last_update])
20
            header 'X-GitLab-Last-Update', params[:last_update]
K
Kamil Trzcinski 已提交
21
            Gitlab::Metrics.add_event(:build_not_found_cached)
K
Kim "BKC" Carlbäcker 已提交
22
            return build_not_found!
K
Kim "BKC" Carlbäcker 已提交
23 24
          end

25 26
          new_update = current_runner.ensure_runner_queue_value

27
          result = Ci::RegisterBuildService.new(current_runner).execute
28

29 30
          if result.valid?
            if result.build
K
Kamil Trzcinski 已提交
31
              Gitlab::Metrics.add_event(:build_found,
32
                                        project: result.build.project.path_with_namespace)
Y
Yorick Peterse 已提交
33

34
              present result.build, with: Entities::BuildDetails
K
Kamil Trzcinski 已提交
35 36
            else
              Gitlab::Metrics.add_event(:build_not_found)
Y
Yorick Peterse 已提交
37

K
Kamil Trzcinski 已提交
38
              header 'X-GitLab-Last-Update', new_update
K
Kim "BKC" Carlbäcker 已提交
39

K
Kamil Trzcinski 已提交
40 41 42 43 44 45
              build_not_found!
            end
          else
            # We received build that is invalid due to concurrency conflict
            Gitlab::Metrics.add_event(:build_invalid)
            conflict!
D
Douwe Maan 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59
          end
        end

        # Update an existing build - Runners only
        #
        # Parameters:
        #   id (required) - The ID of a project
        #   state (optional) - The state of a build
        #   trace (optional) - The trace of a build
        # Example Request:
        #   PUT /builds/:id
        put ":id" do
          authenticate_runner!
          build = Ci::Build.where(runner_id: current_runner.id).running.find(params[:id])
60
          validate_build!(build)
61

62 63
          update_runner_info

D
Douwe Maan 已提交
64 65
          build.update_attributes(trace: params[:trace]) if params[:trace]

Y
Yorick Peterse 已提交
66 67 68
          Gitlab::Metrics.add_event(:update_build,
                                    project: build.project.path_with_namespace)

D
Douwe Maan 已提交
69 70 71 72 73 74 75
          case params[:state].to_s
          when 'success'
            build.success
          when 'failed'
            build.drop
          end
        end
K
Kamil Trzcinski 已提交
76

77 78 79 80 81 82 83
        # Send incremental log update - Runners only
        #
        # Parameters:
        #   id (required) - The ID of a build
        # Body:
        #   content of logs to append
        # Headers:
84
        #   Content-Range (required) - range of content that was sent
85
        #   BUILD-TOKEN (required) - The build authorization token
86 87
        # Example Request:
        #   PATCH /builds/:id/trace.txt
88
        patch ":id/trace.txt" do
T
Tomasz Maczukin 已提交
89
          build = Ci::Build.find_by_id(params[:id])
90
          authenticate_build!(build)
91

T
Tomasz Maczukin 已提交
92 93 94 95
          error!('400 Missing header Content-Range', 400) unless request.headers.has_key?('Content-Range')
          content_range = request.headers['Content-Range']
          content_range = content_range.split('-')

96 97 98
          current_length = build.trace_length
          unless current_length == content_range[0].to_i
            return error!('416 Range Not Satisfiable', 416, { 'Range' => "0-#{current_length}" })
T
Tomasz Maczukin 已提交
99 100
          end

101
          build.append_trace(request.body.read, content_range[0].to_i)
T
Tomasz Maczukin 已提交
102 103 104 105

          status 202
          header 'Build-Status', build.status
          header 'Range', "0-#{build.trace_length}"
106 107
        end

K
Kamil Trzcinski 已提交
108 109 110 111 112
        # Authorize artifacts uploading for build - Runners only
        #
        # Parameters:
        #   id (required) - The ID of a build
        #   token (required) - The build authorization token
113
        #   filesize (optional) - the size of uploaded file
K
Kamil Trzcinski 已提交
114 115 116 117
        # Example Request:
        #   POST /builds/:id/artifacts/authorize
        post ":id/artifacts/authorize" do
          require_gitlab_workhorse!
118
          Gitlab::Workhorse.verify_api_request!(headers)
K
Kamil Trzcinski 已提交
119
          not_allowed! unless Gitlab.config.artifacts.enabled
K
Kamil Trzcinski 已提交
120
          build = Ci::Build.find_by_id(params[:id])
121
          authenticate_build!(build)
K
Kamil Trzcinski 已提交
122 123 124 125 126 127 128 129
          forbidden!('build is not running') unless build.running?

          if params[:filesize]
            file_size = params[:filesize].to_i
            file_to_large! unless file_size < max_artifacts_size
          end

          status 200
130 131
          content_type Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE
          Gitlab::Workhorse.artifact_upload_ok
K
Kamil Trzcinski 已提交
132 133 134 135 136 137 138
        end

        # Upload artifacts to build - Runners only
        #
        # Parameters:
        #   id (required) - The ID of a build
        #   token (required) - The build authorization token
139
        #   file (required) - Artifacts file
140
        #   expire_in (optional) - Specify when artifacts should expire (ex. 7d)
141 142 143 144
        # Parameters (accelerated by GitLab Workhorse):
        #   file.path - path to locally stored body (generated by Workhorse)
        #   file.name - real filename as send in Content-Disposition
        #   file.type - real content type as send in Content-Type
145
        #   metadata.path - path to locally stored body (generated by Workhorse)
G
Grzegorz Bizon 已提交
146
        #   metadata.name - filename (generated by Workhorse)
K
Kamil Trzcinski 已提交
147 148 149 150 151 152 153 154 155
        # Headers:
        #   BUILD-TOKEN (required) - The build authorization token, the same as token
        # Body:
        #   The file content
        #
        # Example Request:
        #   POST /builds/:id/artifacts
        post ":id/artifacts" do
          require_gitlab_workhorse!
K
Kamil Trzcinski 已提交
156
          not_allowed! unless Gitlab.config.artifacts.enabled
K
Kamil Trzcinski 已提交
157
          build = Ci::Build.find_by_id(params[:id])
158
          authenticate_build!(build)
159
          forbidden!('Build is not running!') unless build.running?
K
Kamil Trzcinski 已提交
160

161
          artifacts_upload_path = ArtifactUploader.artifacts_upload_path
162 163 164 165
          artifacts = uploaded_file(:file, artifacts_upload_path)
          metadata = uploaded_file(:metadata, artifacts_upload_path)

          bad_request!('Missing artifacts file!') unless artifacts
166
          file_to_large! unless artifacts.size < max_artifacts_size
K
Kamil Trzcinski 已提交
167

168 169
          build.artifacts_file = artifacts
          build.artifacts_metadata = metadata
170 171
          build.artifacts_expire_in =
            params['expire_in'] ||
172 173
            Gitlab::CurrentSettings.current_application_settings
              .default_artifacts_expire_in
174

G
Grzegorz Bizon 已提交
175
          if build.save
176
            present(build, with: Entities::BuildDetails)
K
Kamil Trzcinski 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
          else
            render_validation_error!(build)
          end
        end

        # Download the artifacts file from build - Runners only
        #
        # Parameters:
        #   id (required) - The ID of a build
        #   token (required) - The build authorization token
        # Headers:
        #   BUILD-TOKEN (required) - The build authorization token, the same as token
        # Example Request:
        #   GET /builds/:id/artifacts
        get ":id/artifacts" do
          build = Ci::Build.find_by_id(params[:id])
193
          authenticate_build!(build)
K
Kamil Trzcinski 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206
          artifacts_file = build.artifacts_file

          unless artifacts_file.file_storage?
            return redirect_to build.artifacts_file.url
          end

          unless artifacts_file.exists?
            not_found!
          end

          present_file!(artifacts_file.path, artifacts_file.filename)
        end

207
        # Remove the artifacts file from build - Runners only
K
Kamil Trzcinski 已提交
208 209 210 211 212 213 214 215 216 217
        #
        # Parameters:
        #   id (required) - The ID of a build
        #   token (required) - The build authorization token
        # Headers:
        #   BUILD-TOKEN (required) - The build authorization token, the same as token
        # Example Request:
        #   DELETE /builds/:id/artifacts
        delete ":id/artifacts" do
          build = Ci::Build.find_by_id(params[:id])
218
          authenticate_build!(build)
219

L
Lin Jen-Shin 已提交
220
          build.erase_artifacts!
K
Kamil Trzcinski 已提交
221
        end
D
Douwe Maan 已提交
222 223 224 225
      end
    end
  end
end