diff --git a/app/helpers/environment_helper.rb b/app/helpers/environment_helper.rb new file mode 100644 index 00000000000..1e2a8c7ddbd --- /dev/null +++ b/app/helpers/environment_helper.rb @@ -0,0 +1,21 @@ +module EnvironmentHelper + def environment_for_build(project, build) + return unless build.environment + + environment_name = ExpandVariables.expand(build.environment, build.variables) + project.environments.find_by(name: environment_name) + end + + def environment_link_for_build(project, build) + environment = environment_for_build(project, build) + return unless environment + + link_to environment.name, namespace_project_environment_path(project.namespace, project, environment) + end + + def deployment_link(project, deployment) + return unless deployment + + link_to "##{deployment.id}", [deployment.project.namespace.becomes(Namespace), deployment.project, deployment.deployable] + end +end diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 1b37c70ee4f..09bbea1c653 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -7,6 +7,8 @@ module Ci belongs_to :trigger_request belongs_to :erased_by, class_name: 'User' + has_many :deployments, as: :deployable + serialize :options serialize :yaml_variables @@ -125,10 +127,12 @@ module Ci !self.pipeline.statuses.latest.include?(self) end - def last_deployment - return @last_deployment if defined?(@last_deployment) + def deployable? + self.environment.present? + end - @last_deployment = Deployment.where(deployable: self).order(id: :desc).last + def last_deployment + deployments.order(id: :desc).last end def depends_on_builds diff --git a/app/views/projects/builds/show.html.haml b/app/views/projects/builds/show.html.haml index 6741be25ace..18a336c3fa3 100644 --- a/app/views/projects/builds/show.html.haml +++ b/app/views/projects/builds/show.html.haml @@ -26,28 +26,30 @@ = link_to namespace_project_runners_path(@build.project.namespace, @build.project) do Runners page - - if @build.deploy + - if @build.deployable? .prepend-top-default .environment-information = ci_icon_for_status(@build.status) - - if @build.last_deployment - This build is the most recent deployment to - = link_to @build.environment, namespace_project_environment_path(@project.namespace, @project, @build.environment) - - else - - case @build.status - - when 'failed', 'canceled' + - last_deployment = @build.last_deployment + - if @build.complete? + - if @build.success? + - if last_deployment.try(:last?) + This build is the most recent deployment to + = environment_link_for_build(@build.project, @build) + - else + This build is an out-of-date deployment to + = environment_link_for_build(@build.project, @build) + View the most recent deployment + = deployment_link(@project, last_deployment) + - else The deployment of this build to - = link_to @build.environment, namespace_project_environment_path(@project.namespace, @project, @build.environment) + = environment_link_for_build(@build.project, @build) did not complete - - when 'pending', 'running' - This build is creating a deployment to - = link_to @build.environment, namespace_project_environment_path(@project.namespace, @project, @build.environment) - and will overwrite the latest deployment - - when 'success' - This build is an out-of-date deployment to - = link_to @build.environment, namespace_project_environment_path(@project.namespace, @project, @build.environment) - View the most recent deployment #24869 + - else + This build is creating a deployment to + = environment_link_for_build(@build.project, @build) + and will overwrite the latest deployment .prepend-top-default - if @build.erased? diff --git a/app/workers/build_success_worker.rb b/app/workers/build_success_worker.rb index e0ad5268664..1bc9745ecbc 100644 --- a/app/workers/build_success_worker.rb +++ b/app/workers/build_success_worker.rb @@ -4,15 +4,13 @@ class BuildSuccessWorker def perform(build_id) Ci::Build.find_by(id: build_id).try do |build| - create_deployment(build) + create_deployment(build) if build.deployable? end end private def create_deployment(build) - return if build.environment.blank? - service = CreateDeploymentService.new( build.project, build.user, environment: build.environment,