builds.rb 7.4 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
Kim "BKC" Carlbäcker 已提交
21
            return build_not_found!
K
Kim "BKC" Carlbäcker 已提交
22 23
          end

24 25
          new_update = current_runner.ensure_runner_queue_value

D
Douwe Maan 已提交
26 27 28
          build = Ci::RegisterBuildService.new.execute(current_runner)

          if build
Y
Yorick Peterse 已提交
29 30 31
            Gitlab::Metrics.add_event(:build_found,
                                      project: build.project.path_with_namespace)

32
            present build, with: Entities::BuildDetails
D
Douwe Maan 已提交
33
          else
Y
Yorick Peterse 已提交
34 35
            Gitlab::Metrics.add_event(:build_not_found)

36
            header 'X-GitLab-Last-Update', new_update
K
Kim "BKC" Carlbäcker 已提交
37

38
            build_not_found!
D
Douwe Maan 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52
          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])
53
          validate_build!(build)
54

55 56
          update_runner_info

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

Y
Yorick Peterse 已提交
59 60 61
          Gitlab::Metrics.add_event(:update_build,
                                    project: build.project.path_with_namespace)

D
Douwe Maan 已提交
62 63 64 65 66 67 68
          case params[:state].to_s
          when 'success'
            build.success
          when 'failed'
            build.drop
          end
        end
K
Kamil Trzcinski 已提交
69

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

T
Tomasz Maczukin 已提交
85 86 87 88
          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('-')

89 90 91
          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 已提交
92 93
          end

94
          build.append_trace(request.body.read, content_range[0].to_i)
T
Tomasz Maczukin 已提交
95 96 97 98

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

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

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

154
          artifacts_upload_path = ArtifactUploader.artifacts_upload_path
155 156 157 158
          artifacts = uploaded_file(:file, artifacts_upload_path)
          metadata = uploaded_file(:metadata, artifacts_upload_path)

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

161 162
          build.artifacts_file = artifacts
          build.artifacts_metadata = metadata
163
          build.artifacts_expire_in = params['expire_in']
164

G
Grzegorz Bizon 已提交
165
          if build.save
166
            present(build, with: Entities::BuildDetails)
K
Kamil Trzcinski 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
          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])
183
          authenticate_build!(build)
K
Kamil Trzcinski 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196
          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

197
        # Remove the artifacts file from build - Runners only
K
Kamil Trzcinski 已提交
198 199 200 201 202 203 204 205 206 207
        #
        # 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])
208
          authenticate_build!(build)
209

L
Lin Jen-Shin 已提交
210
          build.erase_artifacts!
K
Kamil Trzcinski 已提交
211
        end
D
Douwe Maan 已提交
212 213 214 215
      end
    end
  end
end