builds.rb 7.6 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::RegisterJobService.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
89
          build = authenticate_build!
90

T
Tomasz Maczukin 已提交
91 92 93 94
          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('-')

95 96 97
          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 已提交
98 99
          end

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

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

K
Kamil Trzcinski 已提交
107 108 109 110 111
        # Authorize artifacts uploading for build - Runners only
        #
        # Parameters:
        #   id (required) - The ID of a build
        #   token (required) - The build authorization token
112
        #   filesize (optional) - the size of uploaded file
K
Kamil Trzcinski 已提交
113 114 115 116
        # Example Request:
        #   POST /builds/:id/artifacts/authorize
        post ":id/artifacts/authorize" do
          require_gitlab_workhorse!
117
          Gitlab::Workhorse.verify_api_request!(headers)
K
Kamil Trzcinski 已提交
118
          not_allowed! unless Gitlab.config.artifacts.enabled
119
          build = authenticate_build!
K
Kamil Trzcinski 已提交
120 121 122 123 124 125 126 127
          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
128 129
          content_type Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE
          Gitlab::Workhorse.artifact_upload_ok
K
Kamil Trzcinski 已提交
130 131 132 133 134 135 136
        end

        # Upload artifacts to build - Runners only
        #
        # Parameters:
        #   id (required) - The ID of a build
        #   token (required) - The build authorization token
137
        #   file (required) - Artifacts file
138
        #   expire_in (optional) - Specify when artifacts should expire (ex. 7d)
139 140 141 142
        # 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
143
        #   metadata.path - path to locally stored body (generated by Workhorse)
G
Grzegorz Bizon 已提交
144
        #   metadata.name - filename (generated by Workhorse)
K
Kamil Trzcinski 已提交
145 146 147 148 149 150 151 152 153
        # 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 已提交
154
          not_allowed! unless Gitlab.config.artifacts.enabled
155
          build = authenticate_build!
156
          forbidden!('Build is not running!') unless build.running?
K
Kamil Trzcinski 已提交
157

158
          artifacts_upload_path = ArtifactUploader.artifacts_upload_path
159 160 161 162
          artifacts = uploaded_file(:file, artifacts_upload_path)
          metadata = uploaded_file(:metadata, artifacts_upload_path)

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

165 166
          build.artifacts_file = artifacts
          build.artifacts_metadata = metadata
167 168
          build.artifacts_expire_in =
            params['expire_in'] ||
169 170
            Gitlab::CurrentSettings.current_application_settings
              .default_artifacts_expire_in
171

G
Grzegorz Bizon 已提交
172
          if build.save
173
            present(build, with: Entities::BuildDetails)
K
Kamil Trzcinski 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
          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
189
          build = authenticate_build!
K
Kamil Trzcinski 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202
          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

203
        # Remove the artifacts file from build - Runners only
K
Kamil Trzcinski 已提交
204 205 206 207 208 209 210 211 212
        #
        # 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
213
          build = authenticate_build!
214

215
          status(200)
L
Lin Jen-Shin 已提交
216
          build.erase_artifacts!
K
Kamil Trzcinski 已提交
217
        end
D
Douwe Maan 已提交
218 219 220 221
      end
    end
  end
end