Commit 907c0e67 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Added initial version of deployments

parent cf7da039
......@@ -51,7 +51,7 @@ class Projects::BuildsController < Projects::ApplicationController
return render_404
end
build = Ci::Build.retry(@build)
build = Ci::Build.retry(@build, current_user)
redirect_to build_path(build)
end
......
......@@ -46,7 +46,7 @@ class Projects::CommitController < Projects::ApplicationController
def retry_builds
ci_builds.latest.failed.each do |build|
if build.retryable?
Ci::Build.retry(build)
Ci::Build.retry(build, current_user)
end
end
......
class Projects::EnvironmentsController < Projects::ApplicationController
layout 'project'
before_action :authorize_read_environment!
before_action :environment, only: [:show]
def index
@environments = project.builds.where.not(environment: nil).pluck(:environment).uniq
@environments = @environments.map { |env| build_for_env(env) }.compact
@environments = project.environments
end
def show
@environment = params[:id].to_s
@builds = project.builds.where.not(status: ["manual"]).where(environment: params[:id].to_s).order(id: :desc).page(params[:page]).per(30)
end
def build_for_env(environment)
project.builds.success.order(id: :desc).find_by(environment: environment)
private
def environment
@environment ||= project.environments.find(params[:id].to_s)
@environment || render_404
end
end
......@@ -156,6 +156,10 @@ module ProjectsHelper
nav_tabs << :container_registry
end
if can?(current_user, :read_environment, project)
nav_tabs << :environments
end
if can?(current_user, :admin_project, project)
nav_tabs << :settings
end
......
......@@ -228,6 +228,8 @@ class Ability
:read_build,
:read_container_image,
:read_pipeline,
:read_environment,
:read_deployment
]
end
......@@ -246,6 +248,10 @@ class Ability
:push_code,
:create_container_image,
:update_container_image,
:create_environment,
:update_environment,
:create_deployment,
:update_deployment,
]
end
......@@ -273,7 +279,9 @@ class Ability
:admin_commit_status,
:admin_build,
:admin_container_image,
:admin_pipeline
:admin_pipeline,
:admin_environment,
:admin_deployment
]
end
......@@ -317,6 +325,8 @@ class Ability
unless project.builds_enabled
rules += named_abilities('build')
rules += named_abilities('pipeline')
rules += named_abilities('environment')
rules += named_abilities('deployment')
end
unless project.container_registry_enabled
......
......@@ -38,7 +38,7 @@ module Ci
new_build.save
end
def retry(build)
def retry(build, user = nil)
new_build = Ci::Build.new(status: 'pending')
new_build.ref = build.ref
new_build.tag = build.tag
......@@ -52,6 +52,7 @@ module Ci
new_build.stage = build.stage
new_build.stage_idx = build.stage_idx
new_build.trigger_request = build.trigger_request
new_build.user = user
new_build.save
MergeRequests::AddTodoWhenBuildFailsService.new(build.project, nil).close(new_build)
new_build
......@@ -73,6 +74,12 @@ module Ci
build.update_coverage
build.execute_hooks
end
after_transition any: :success do |build|
if build.environment.present?
CreateDeploymentService.new(build.project, build.user, environment: build.environment).execute(build)
end
end
end
def retryable?
......@@ -83,10 +90,6 @@ module Ci
!self.pipeline.statuses.latest.include?(self)
end
def retry
Ci::Build.retry(self)
end
def depends_on_builds
# Get builds of the same type
latest_builds = self.pipeline.builds.latest
......
class Deployment < ActiveRecord::Base
include InternalId
belongs_to :project
belongs_to :environment
belongs_to :user
belongs_to :deployable, polymorphic: true
validates_presence_of :sha
validates_presence_of :ref
delegate :name, to: :environment, prefix: true
def commit
project.commit(sha)
end
def commit_title
commit.try(:title)
end
def short_sha
Commit::truncate_sha(sha)
end
end
class Environment < ActiveRecord::Base
belongs_to :project
has_many :deployments
validates_presence_of :name
def last_deployment
deployments.last
end
end
......@@ -125,6 +125,8 @@ class Project < ActiveRecord::Base
has_many :runners, through: :runner_projects, source: :runner, class_name: 'Ci::Runner'
has_many :variables, dependent: :destroy, class_name: 'Ci::Variable', foreign_key: :gl_project_id
has_many :triggers, dependent: :destroy, class_name: 'Ci::Trigger', foreign_key: :gl_project_id
has_many :environments, dependent: :destroy
has_many :deployments, dependent: :destroy
accepts_nested_attributes_for :variables, allow_destroy: true
......
......@@ -29,7 +29,8 @@ module Ci
:options,
:allow_failure,
:stage,
:stage_idx)
:stage_idx,
:environment)
build_attrs.merge!(ref: @pipeline.ref,
tag: @pipeline.tag,
......
require_relative 'base_service'
class CreateDeploymentService < BaseService
def execute(deployable)
environment = find_or_create_environment(params[:environment])
deployment = create_deployment(environment, deployable)
if deployment.persisted?
success(deployment)
else
error(deployment.errors)
end
end
private
def find_or_create_environment(environment)
find_environment(environment) || create_environment(environment)
end
def create_environment(environment)
project.environments.create(name: environment)
end
def find_environment(environment)
project.environments.find_by(name: environment)
end
def create_deployment(environment, deployable)
environment.deployments.create(
project: project,
ref: build.ref,
tag: build.tag,
sha: build.sha,
user: current_user,
deployable: deployable,
)
end
def success(deployment)
out = super()
out[:deployment] = deployment
out
end
end
%tr.deployment
%td
%strong= "##{environment.id}"
%td
%div.branch-commit
- if deployment.ref
= link_to last.ref, namespace_project_commits_path(@project.namespace, @project, deployment.ref), class: "monospace"
&middot;
= link_to deployment.short_sha, namespace_project_commit_path(@project.namespace, @project, deployment.sha), class: "commit-id monospace"
%p
%span
- if commit_title = deployment.commit_title
= link_to_gfm commit_title, namespace_project_commit_path(@project.namespace, @project, deployment.sha), class: "commit-row-message"
- else
Cant find HEAD commit for this branch
%td
- if deployment.deployable
= link_to [@project.namespace.becomes(Namespace), @project, deployment.deployable], class: "monospace" do
= "#{deployment.deployable.name} (##{deployment.deployable.id})"
%td
%p
%i.fa.fa-calendar
&nbsp;
#{time_ago_with_tooltip(deployment.created_at)}
%td
- if can?(current_user, :update_deployment, @project) && deployment.deployable
= link_to [@project.namespace.becomes(Namespace), @project, deployment.deployable, :retry], method: :post, title: 'Retry', class: 'btn btn-build'
%tr.commit
- commit = build.commit
- status = build.status
- last_deployment = environment.last_deployment
%tr.environment
%td
%strong
= link_to build.environment, namespace_project_environment_path(@project.namespace, @project, build.environment), class: "monospace"
%td.commit-link
= link_to namespace_project_pipeline_path(@project.namespace, @project, commit.id), class: "ci-status ci-#{commit.status}" do
= ci_icon_for_status(commit.status)
%strong ##{commit.id}
%td.commit-link
= link_to namespace_project_build_path(@project.namespace, @project, build.id), class: "ci-status ci-#{build.status}" do
= ci_icon_for_status(build.status)
%strong ##{build.id}
= link_to environment.name, namespace_project_environment_path(@project.namespace, @project, environment), class: "monospace"
%td
%div.branch-commit
- if commit.ref
= link_to commit.ref, namespace_project_commits_path(@project.namespace, @project, commit.ref), class: "monospace"
&middot;
= link_to commit.short_sha, namespace_project_commit_path(@project.namespace, @project, commit.sha), class: "commit-id monospace"
- if last_deployment
%div.branch-commit
- if last_deployment.ref
= link_to last.ref, namespace_project_commits_path(@project.namespace, @project, last_deployment.ref), class: "monospace"
&middot;
= link_to last_deployment.short_sha, namespace_project_commit_path(@project.namespace, @project, last_deployment.sha), class: "commit-id monospace"
%p
%span
- if commit_title = last_deployment.commit_title
= link_to_gfm commit_title, namespace_project_commit_path(@project.namespace, @project, last_deployment.sha), class: "commit-row-message"
- else
Cant find HEAD commit for this branch
- else
%p
%span
- if commit_data = commit.commit_data
= link_to_gfm commit_data.title, namespace_project_commit_path(@project.namespace, @project, commit_data.id), class: "commit-row-message"
- else
Cant find HEAD commit for this branch
No deployments yet
%td
- if build.started_at && build.finished_at
%p
%i.fa.fa-clock-o
&nbsp;
#{duration_in_words(build.finished_at, build.started_at)}
- if build.finished_at
%p
%i.fa.fa-calendar
&nbsp;
#{time_ago_with_tooltip(build.finished_at)}
%p
%i.fa.fa-calendar
&nbsp;
#{time_ago_with_tooltip(last_deployment.created_at)}
%td
.controls.hidden-xs.pull-right
- manual = commit.builds.latest.manual_actions.to_a
- if manual.any?
.dropdown.inline
%button.dropdown-toggle.btn{type: 'button', 'data-toggle' => 'dropdown'}
= icon('play')
%b.caret
%ul.dropdown-menu.dropdown-menu-align-right
- manual.each do |manual_build|
%li
= link_to '#', rel: 'nofollow' do
%i.fa.fa-play
%span #{manual_build.name}
- @no_container = true
- page_title "Environments"
= render "header_title"
= render "projects/pipelines/head"
.gray-content-block
Environments for this project
%div{ class: (container_class) }
.gray-content-block
Environments for this project
%ul.content-list
- if @environments.blank?
%li
.nothing-here-block No environments to show
- else
.table-holder
%table.table.builds
%tbody
%th Environment
%th Pipeline ID
%th Build ID
%th Changes
%th
%th
- @environments.each do |build|
= render "environment", build: build
%ul.content-list
- if @environments.blank?
%li
.nothing-here-block No environments to show
- else
.table-holder
%table.table.builds
%tbody
%th Environment
%th Last deployment
%th Date
%th
- @environments.each do |environment|
= render 'environment', environment: environment
- @no_container = true
- page_title "Environments"
= render "projects/pipelines/head"
= render "header_title"
%div{ class: (container_class) }
.gray-content-block
Latest deployments for
%strong= @environment.name
.gray-content-block
Latest deployments for
%strong
= @environment
%ul.content-list
- if @deployments.blank?
%li
.nothing-here-block No deployment for specific environment
- else
.table-holder
%table.table.builds
%thead
%tr
%th Commit
%th Context
%th Date
%th
%ul.content-list
- if @builds.blank?
%li
.nothing-here-block No builds to show for specific environment
- else
.table-holder
%table.table.builds
%thead
%tr
%th Status
%th Build ID
%th Commit
%th Ref
%th Name
%th Duration
%th Finished at
%th
= render @deployments
= render @builds, commit_sha: true, ref: true, allow_retry: true
= paginate @builds, theme: 'gitlab'
= paginate @deployments, theme: 'gitlab'
......@@ -13,3 +13,9 @@
%span
Builds
%span.badge.count.builds_counter= number_with_delimiter(@project.running_or_pending_build_count)
- if project_nav_tab? :environments
= nav_link(controller: %w(environments)) do
= link_to project_environments_path(@project), title: 'Environments', class: 'shortcuts-environments' do
%span
Environments
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class AddDeployments < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
def change
create_table :deployments, force: true do |t|
t.integer :iid
t.integer :project_id
t.integer :environment_id
t.string :ref
t.boolean :tag
t.string :sha
t.integer :user_id
t.integer :deployable_id, null: false
t.string :deployable_type, null: false
t.datetime :created_at
t.datetime :updated_at
end
add_index :deployments, :project_id
add_index :deployments, [:project_id, :iid]
add_index :deployments, [:project_id, :environment_id]
add_index :deployments, [:project_id, :environment_id, :iid]
end
end
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class AddEnvironments < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
def change
create_table :environments, force: true do |t|
t.integer :project_id
t.string :name, null: false
t.datetime :created_at
t.datetime :updated_at
end
add_index :environments, [:project_id, :name]
end
end
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class AddEnvironmentToBuilds < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
def change
add_column :ci_builds, :environment, :string
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160608155312) do
ActiveRecord::Schema.define(version: 20160610211845) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -144,9 +144,9 @@ ActiveRecord::Schema.define(version: 20160608155312) do
t.text "commands"
t.integer "job_id"
t.string "name"
t.boolean "deploy", default: false
t.boolean "deploy", default: false
t.text "options"
t.boolean "allow_failure", default: false, null: false
t.boolean "allow_failure", default: false, null: false
t.string "stage"
t.integer "trigger_request_id"
t.integer "stage_idx"
......@@ -161,6 +161,7 @@ ActiveRecord::Schema.define(version: 20160608155312) do
t.text "artifacts_metadata"
t.integer "erased_by_id"
t.datetime "erased_at"
t.string "environment"
end
add_index "ci_builds", ["commit_id", "stage_idx", "created_at"], name: "index_ci_builds_on_commit_id_and_stage_idx_and_created_at", using: :btree
......@@ -381,6 +382,25 @@ ActiveRecord::Schema.define(version: 20160608155312) do
add_index "deploy_keys_projects", ["project_id"], name: "index_deploy_keys_projects_on_project_id", using: :btree
create_table "deployments", force: :cascade do |t|
t.integer "iid"
t.integer "project_id"
t.integer "environment_id"
t.string "ref"
t.boolean "tag"
t.string "sha"
t.integer "user_id"
t.integer "deployable_id", null: false
t.string "deployable_type", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "deployments", ["project_id", "environment_id", "iid"], name: "index_deployments_on_project_id_and_environment_id_and_iid", using: :btree
add_index "deployments", ["project_id", "environment_id"], name: "index_deployments_on_project_id_and_environment_id", using: :btree
add_index "deployments", ["project_id", "iid"], name: "index_deployments_on_project_id_and_iid", using: :btree
add_index "deployments", ["project_id"], name: "index_deployments_on_project_id", using: :btree
create_table "emails", force: :cascade do |t|
t.integer "user_id", null: false
t.string "email", null: false
......@@ -391,6 +411,15 @@ ActiveRecord::Schema.define(version: 20160608155312) do
add_index "emails", ["email"], name: "index_emails_on_email", unique: true, using: :btree
add_index "emails", ["user_id"], name: "index_emails_on_user_id", using: :btree
create_table "environments", force: :cascade do |t|
t.integer "project_id"
t.string "name", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "environments", ["project_id", "name"], name: "index_environments_on_project_id_and_name", using: :btree
create_table "events", force: :cascade do |t|
t.string "target_type"
t.integer "target_id"
......
......@@ -142,7 +142,7 @@ module API
return not_found!(build) unless build
return forbidden!('Build is not retryable') unless build.retryable?
build = Ci::Build.retry(build)
build = Ci::Build.retry(build, current_user)
present build, with: Entities::Build,
user_can_download_artifacts: can?(current_user, :read_build, user_project)
......
......@@ -7,7 +7,8 @@ module Ci
ALLOWED_YAML_KEYS = [:before_script, :after_script, :image, :services, :types, :stages, :variables, :cache]
ALLOWED_JOB_KEYS = [:tags, :script, :only, :except, :type, :image, :services,
:allow_failure, :type, :stage, :when, :artifacts, :cache,
:dependencies, :before_script, :after_script, :variables]
:dependencies, :before_script, :after_script, :variables,
:environment]
attr_reader :before_script, :after_script, :image, :services, :path, :cache
......@@ -85,6 +86,7 @@ module Ci
except: job[:except],
allow_failure: job[:allow_failure] || false,
when: job[:when] || 'on_success',
environment: job[:environment],
options: {
image: job[:image] || @image,
services: job[:services] || @services,
......@@ -203,6 +205,10 @@ module Ci
if job[:when] && !job[:when].in?(%w(on_success on_failure always))
raise ValidationError, "#{name} job: when parameter should be on_success, on_failure or always"
end
if job[:environment] && !validate_string(job[:environment])
raise ValidationError, "#{name} job: environment should be a string"
end
end
def validate_job_script!(name, job)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment