diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/models/ci/pipeline.rb | 59 | ||||
-rw-r--r-- | app/models/commit_status.rb | 28 | ||||
-rw-r--r-- | app/services/ci/create_pipeline_service.rb | 5 |
3 files changed, 72 insertions, 20 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 8de799d6088..1c8fe550153 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -19,6 +19,40 @@ module Ci after_save :keep_around_commits + state_machine :status, initial: :created do + event :start do + transition any => :pending, if: ->(pipeline) { pipeline.can_transition_to?('pending') } + end + + event :run do + transition any => :running, if: ->(pipeline) { pipeline.can_transition_to?('running') } + end + + event :drop do + transition any => :failed, if: ->(pipeline) { pipeline.can_transition_to?('failed') } + end + + event :succeed do + transition any => :success, if: ->(pipeline) { pipeline.can_transition_to?('success') } + end + + event :cancel do + transition any => :canceled, if: ->(pipeline) { pipeline.can_transition_to?('canceled') } + end + + event :skip do + transition any => :skipped, if: ->(pipeline) { pipeline.can_transition_to?('skipped') } + end + + before_transition do |pipeline| + pipeline.update_counters + end + + after_transition any => any do |pipeline, transition| + pipeline.execute_hooks unless transition.loopback? + end + end + # ref can't be HEAD or SHA, can only be branch/tag name scope :latest_successful_for, ->(ref = default_branch) do where(ref: ref).success.order(id: :desc).limit(1) @@ -89,16 +123,12 @@ module Ci def cancel_running builds.running_or_pending.each(&:cancel) - - reload_status! end def retry_failed(user) builds.latest.failed.select(&:retryable?).each do |build| Ci::Build.retry(build, user) end - - reload_status! end def latest? @@ -185,8 +215,6 @@ module Ci def process! Ci::ProcessPipelineService.new(project, user).execute(self) - - reload_status! end def predefined_variables @@ -194,15 +222,12 @@ module Ci { key: 'CI_PIPELINE_ID', value: id.to_s, public: true } ] end + + def can_transition_to?(expected_status) + current_status == expected_status + end - def reload_status! - reload - self.status = - if yaml_errors.blank? - statuses.latest.status || 'skipped' - else - 'failed' - end + def update_counters self.started_at = statuses.started_at self.finished_at = statuses.finished_at self.duration = statuses.latest.duration @@ -211,6 +236,12 @@ module Ci private + def current_status + return 'failed' unless yaml_errors.blank? + + statuses.latest.status || 'skipped' + end + def keep_around_commits return unless project diff --git a/app/models/commit_status.rb b/app/models/commit_status.rb index 3ab44461179..5773545a602 100644 --- a/app/models/commit_status.rb +++ b/app/models/commit_status.rb @@ -74,13 +74,33 @@ class CommitStatus < ActiveRecord::Base around_transition any => [:success, :failed, :canceled] do |commit_status, block| block.call - commit_status.pipeline.process! if commit_status.pipeline + commit_status.pipeline.try(:process!) end - around_transition any => [:pending, :running] do |commit_status, block| - block.call + # Try to update the pipeline status + + after_transition any => :pending do |commit_status| + commit_status.pipeline.try(:start) + end + + after_transition any => :running do |commit_status| + commit_status.pipeline.try(:run) + end + + after_transition any => :success do |commit_status| + commit_status.pipeline.try(:succeed) + end + + after_transition any => :failed do |commit_status| + commit_status.pipeline.try(:drop) + end + + after_transition any => :skipped do |commit_status| + commit_status.pipeline.try(:skip) + end - commit_status.pipeline.reload_status! if commit_status.pipeline + after_transition any => :canceled do |commit_status| + commit_status.pipeline.try(:cancel) end end diff --git a/app/services/ci/create_pipeline_service.rb b/app/services/ci/create_pipeline_service.rb index 7398fd8e10a..cde856b0186 100644 --- a/app/services/ci/create_pipeline_service.rb +++ b/app/services/ci/create_pipeline_service.rb @@ -37,7 +37,8 @@ module Ci end if !ignore_skip_ci && skip_ci? - return error('Creation of pipeline is skipped', save: save_on_errors) + pipeline.skip if save_on_errors + return pipeline end unless pipeline.config_builds_attributes.present? @@ -93,7 +94,7 @@ module Ci def error(message, save: false) pipeline.errors.add(:base, message) - pipeline.reload_status! if save + pipeline.drop if save pipeline end end |