debian-mirror-gitlab/app/services/projects/update_pages_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

146 lines
4.1 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Projects
class UpdatePagesService < BaseService
2021-01-29 00:20:46 +05:30
# old deployment can be cached by pages daemon
# so we need to give pages daemon some time update cache
# 10 minutes is enough, but 30 feels safer
OLD_DEPLOYMENTS_DESTRUCTION_DELAY = 30.minutes.freeze
2022-08-13 15:12:31 +05:30
attr_reader :build, :deployment_update
2017-08-17 22:00:37 +05:30
def initialize(project, build)
2021-04-29 21:17:54 +05:30
@project = project
@build = build
2022-08-13 15:12:31 +05:30
@deployment_update = ::Gitlab::Pages::DeploymentUpdate.new(project, build)
2017-08-17 22:00:37 +05:30
end
def execute
2018-03-17 18:26:18 +05:30
register_attempt
2017-08-17 22:00:37 +05:30
# Create status notifying the deployment of pages
2022-05-07 20:08:51 +05:30
@commit_status = build_commit_status
::Ci::Pipelines::AddJobService.new(@build.pipeline).execute!(@commit_status) do |job|
2021-09-30 23:02:18 +05:30
job.enqueue!
job.run!
end
2017-08-17 22:00:37 +05:30
2022-08-13 15:12:31 +05:30
return error(deployment_update.errors.first.full_message) unless deployment_update.valid?
2017-08-17 22:00:37 +05:30
2021-03-08 18:12:59 +05:30
build.artifacts_file.use_file do |artifacts_path|
2022-08-13 15:12:31 +05:30
deployment = create_pages_deployment(artifacts_path, build)
break error('The uploaded artifact size does not match the expected value') unless deployment
if deployment_update.valid?
update_project_pages_deployment(deployment)
success
else
error(deployment_update.errors.first.full_message)
end
2017-08-17 22:00:37 +05:30
end
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2018-10-15 14:42:47 +05:30
error(e.message)
2018-05-01 15:08:00 +05:30
raise e
2017-08-17 22:00:37 +05:30
end
private
def success
2022-05-07 20:08:51 +05:30
@commit_status.success
@project.mark_pages_as_deployed
2022-08-13 15:12:31 +05:30
publish_deployed_event
2017-08-17 22:00:37 +05:30
super
end
2018-10-15 14:42:47 +05:30
def error(message)
2018-05-01 15:08:00 +05:30
register_failure
2017-08-17 22:00:37 +05:30
log_error("Projects::UpdatePagesService: #{message}")
2022-08-13 15:12:31 +05:30
@commit_status.allow_failure = !deployment_update.latest?
2022-05-07 20:08:51 +05:30
@commit_status.description = message
@commit_status.drop(:script_failure)
2017-08-17 22:00:37 +05:30
super
end
2021-09-30 23:02:18 +05:30
def build_commit_status
2023-03-04 22:38:38 +05:30
stage = create_stage
2017-08-17 22:00:37 +05:30
GenericCommitStatus.new(
user: build.user,
2022-10-11 01:57:18 +05:30
ci_stage: stage,
name: 'pages:deploy',
2023-03-04 22:38:38 +05:30
stage: 'deploy',
stage_idx: stage.position
2017-08-17 22:00:37 +05:30
)
end
2022-10-11 01:57:18 +05:30
# rubocop: disable Performance/ActiveRecordSubtransactionMethods
2023-03-04 22:38:38 +05:30
def create_stage
2022-10-11 01:57:18 +05:30
build.pipeline.stages.safe_find_or_create_by(name: 'deploy', pipeline_id: build.pipeline.id) do |stage|
stage.position = GenericCommitStatus::EXTERNAL_STAGE_IDX
stage.project = build.project
end
end
# rubocop: enable Performance/ActiveRecordSubtransactionMethods
2021-01-29 00:20:46 +05:30
def create_pages_deployment(artifacts_path, build)
sha256 = build.job_artifacts_archive.file_sha256
2021-01-03 14:25:43 +05:30
File.open(artifacts_path) do |file|
2022-08-13 15:12:31 +05:30
deployment = project.pages_deployments.create!(
file: file,
file_count: deployment_update.entries_count,
file_sha256: sha256,
ci_build_id: build.id
)
2022-07-23 23:45:48 +05:30
2022-08-13 15:12:31 +05:30
break if deployment.size != file.size || deployment.file.size != file.size
2021-03-08 18:12:59 +05:30
2022-08-13 15:12:31 +05:30
deployment
2021-01-03 14:25:43 +05:30
end
2022-08-13 15:12:31 +05:30
end
2021-01-03 14:25:43 +05:30
2022-08-13 15:12:31 +05:30
def update_project_pages_deployment(deployment)
project.update_pages_deployment!(deployment)
2021-01-29 00:20:46 +05:30
DestroyPagesDeploymentsWorker.perform_in(
OLD_DEPLOYMENTS_DESTRUCTION_DELAY,
project.id,
deployment.id
)
2021-01-03 14:25:43 +05:30
end
2017-08-17 22:00:37 +05:30
def ref
build.ref
end
def artifacts
build.artifacts_file.path
end
2017-09-10 17:25:29 +05:30
def register_attempt
pages_deployments_total_counter.increment
end
def register_failure
pages_deployments_failed_total_counter.increment
end
def pages_deployments_total_counter
@pages_deployments_total_counter ||= Gitlab::Metrics.counter(:pages_deployments_total, "Counter of GitLab Pages deployments triggered")
end
def pages_deployments_failed_total_counter
@pages_deployments_failed_total_counter ||= Gitlab::Metrics.counter(:pages_deployments_failed_total, "Counter of GitLab Pages deployments which failed")
end
2019-02-02 18:00:53 +05:30
2022-08-13 15:12:31 +05:30
def publish_deployed_event
event = ::Pages::PageDeployedEvent.new(data: {
project_id: project.id,
namespace_id: project.namespace_id,
root_namespace_id: project.root_namespace.id
})
2021-10-27 15:23:28 +05:30
2022-08-13 15:12:31 +05:30
Gitlab::EventStore.publish(event)
2021-10-27 15:23:28 +05:30
end
2017-08-17 22:00:37 +05:30
end
end