debian-mirror-gitlab/lib/ci/api/builds.rb

220 lines
7.5 KiB
Ruby
Raw Normal View History

2015-09-25 12:07:36 +05:30
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?
2016-11-03 12:29:30 +05:30
update_runner_info
2015-09-25 12:07:36 +05:30
2017-08-17 22:00:37 +05:30
if current_runner.is_runner_queue_value_latest?(params[:last_update])
header 'X-GitLab-Last-Update', params[:last_update]
Gitlab::Metrics.add_event(:build_not_found_cached)
return build_not_found!
end
2015-09-25 12:07:36 +05:30
2017-08-17 22:00:37 +05:30
new_update = current_runner.ensure_runner_queue_value
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
result = Ci::RegisterJobService.new(current_runner).execute
if result.valid?
if result.build
Gitlab::Metrics.add_event(:build_found,
project: result.build.project.path_with_namespace)
present result.build, with: Entities::BuildDetails
else
Gitlab::Metrics.add_event(:build_not_found)
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
header 'X-GitLab-Last-Update', new_update
build_not_found!
end
else
# We received build that is invalid due to concurrency conflict
Gitlab::Metrics.add_event(:build_invalid)
conflict!
2015-09-25 12:07:36 +05:30
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])
2017-08-17 22:00:37 +05:30
validate_build!(build)
2016-04-02 18:10:28 +05:30
2016-11-03 12:29:30 +05:30
update_runner_info
2017-08-17 22:00:37 +05:30
build.trace.set(params[:trace]) if params[:trace]
2015-09-25 12:07:36 +05:30
2016-09-13 17:45:13 +05:30
Gitlab::Metrics.add_event(:update_build,
project: build.project.path_with_namespace)
2015-09-25 12:07:36 +05:30
case params[:state].to_s
when 'success'
build.success
when 'failed'
build.drop
end
end
2015-11-26 14:37:03 +05:30
2016-06-02 11:05:42 +05:30
# Send incremental log update - Runners only
#
# Parameters:
# id (required) - The ID of a build
# Body:
# content of logs to append
# Headers:
# Content-Range (required) - range of content that was sent
# BUILD-TOKEN (required) - The build authorization token
# Example Request:
# PATCH /builds/:id/trace.txt
patch ":id/trace.txt" do
2017-08-17 22:00:37 +05:30
build = authenticate_build!
2016-06-02 11:05:42 +05:30
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('-')
2017-08-17 22:00:37 +05:30
stream_size = build.trace.append(request.body.read, content_range[0].to_i)
if stream_size < 0
return error!('416 Range Not Satisfiable', 416, { 'Range' => "0-#{-stream_size}" })
2016-06-02 11:05:42 +05:30
end
status 202
header 'Build-Status', build.status
2017-08-17 22:00:37 +05:30
header 'Range', "0-#{stream_size}"
2016-06-02 11:05:42 +05:30
end
2015-11-26 14:37:03 +05:30
# Authorize artifacts uploading for build - Runners only
#
# Parameters:
# id (required) - The ID of a build
# token (required) - The build authorization token
# filesize (optional) - the size of uploaded file
# Example Request:
# POST /builds/:id/artifacts/authorize
post ":id/artifacts/authorize" do
require_gitlab_workhorse!
2016-09-29 09:46:39 +05:30
Gitlab::Workhorse.verify_api_request!(headers)
2015-11-26 14:37:03 +05:30
not_allowed! unless Gitlab.config.artifacts.enabled
2017-08-17 22:00:37 +05:30
build = authenticate_build!
2015-11-26 14:37:03 +05:30
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
2016-09-29 09:46:39 +05:30
content_type Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE
Gitlab::Workhorse.artifact_upload_ok
2015-11-26 14:37:03 +05:30
end
# Upload artifacts to build - Runners only
#
# Parameters:
# id (required) - The ID of a build
# token (required) - The build authorization token
# file (required) - Artifacts file
# expire_in (optional) - Specify when artifacts should expire (ex. 7d)
2015-11-26 14:37:03 +05:30
# 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
# metadata.path - path to locally stored body (generated by Workhorse)
# metadata.name - filename (generated by Workhorse)
2015-11-26 14:37:03 +05:30
# 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!
not_allowed! unless Gitlab.config.artifacts.enabled
2017-08-17 22:00:37 +05:30
build = authenticate_build!
forbidden!('Build is not running!') unless build.running?
artifacts_upload_path = ArtifactUploader.artifacts_upload_path
artifacts = uploaded_file(:file, artifacts_upload_path)
metadata = uploaded_file(:metadata, artifacts_upload_path)
bad_request!('Missing artifacts file!') unless artifacts
file_to_large! unless artifacts.size < max_artifacts_size
2015-11-26 14:37:03 +05:30
build.artifacts_file = artifacts
build.artifacts_metadata = metadata
2017-08-17 22:00:37 +05:30
build.artifacts_expire_in =
params['expire_in'] ||
Gitlab::CurrentSettings.current_application_settings
.default_artifacts_expire_in
2015-11-26 14:37:03 +05:30
if build.save
present(build, with: Entities::BuildDetails)
2015-11-26 14:37:03 +05:30
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
2017-08-17 22:00:37 +05:30
build = authenticate_build!
2015-11-26 14:37:03 +05:30
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
2016-04-02 18:10:28 +05:30
# Remove the artifacts file from build - Runners only
2015-11-26 14:37:03 +05:30
#
# 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
2017-08-17 22:00:37 +05:30
build = authenticate_build!
2016-04-02 18:10:28 +05:30
2017-08-17 22:00:37 +05:30
status(200)
2016-08-24 12:49:21 +05:30
build.erase_artifacts!
2015-11-26 14:37:03 +05:30
end
2015-09-25 12:07:36 +05:30
end
end
end
end