From b683f5db89f1c3c6c815ffffd08b4422c2d0a01c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Thu, 11 Apr 2019 16:09:34 +0200 Subject: Add global only/except --- lib/gitlab/ci/config/entry/global.rb | 9 ++++++++- lib/gitlab/ci/config/entry/job.rb | 10 +++++++--- lib/gitlab/ci/config/entry/policy.rb | 2 +- lib/gitlab/ci/pipeline/seed/build.rb | 8 +++++++- lib/gitlab/ci/yaml_processor.rb | 2 ++ spec/lib/gitlab/ci/yaml_processor_spec.rb | 4 ++++ 6 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/gitlab/ci/config/entry/global.rb b/lib/gitlab/ci/config/entry/global.rb index dbad77e59c4..c85f68c0911 100644 --- a/lib/gitlab/ci/config/entry/global.rb +++ b/lib/gitlab/ci/config/entry/global.rb @@ -43,8 +43,15 @@ module Gitlab description: 'Configure caching between build jobs.', inherit: true + entry :only, Entry::Policy, + description: 'Refs policy this job will be executed for.', + default: Entry::Policy::DEFAULT_GLOBAL_ONLY + + entry :except, Entry::Policy, + description: 'Refs policy this job will be executed for.' + helpers :before_script, :image, :services, :after_script, - :variables, :stages, :types, :cache + :variables, :stages, :types, :cache, :only, :except def compose!(deps = nil) super(self) do diff --git a/lib/gitlab/ci/config/entry/job.rb b/lib/gitlab/ci/config/entry/job.rb index 623ce94e49c..f46083935cf 100644 --- a/lib/gitlab/ci/config/entry/job.rb +++ b/lib/gitlab/ci/config/entry/job.rb @@ -71,8 +71,7 @@ module Gitlab inherit: true entry :only, Entry::Policy, - description: 'Refs policy this job will be executed for.', - default: Entry::Policy::DEFAULT_ONLY + description: 'Refs policy this job will be executed for.' entry :except, Entry::Policy, description: 'Refs policy this job will be executed for.' @@ -137,10 +136,13 @@ module Gitlab def inherit!(deps) return unless deps + @global_only = deps.global[:only] + @global_except = deps.global[:except] + self.class.nodes.each do |key, factory| next unless factory.inheritable? - global_entry = deps[:global][key] + global_entry = deps.global[key] job_entry = self[key] if global_entry.specified? && !job_entry.specified? @@ -157,6 +159,8 @@ module Gitlab services: services_value, stage: stage_value, cache: cache_value, + global_only: @global_only.value, + global_except: @global_except.value, only: only_value, except: except_value, variables: variables_defined? ? variables_value : nil, diff --git a/lib/gitlab/ci/config/entry/policy.rb b/lib/gitlab/ci/config/entry/policy.rb index 7b14218d3ea..4a408e92645 100644 --- a/lib/gitlab/ci/config/entry/policy.rb +++ b/lib/gitlab/ci/config/entry/policy.rb @@ -11,7 +11,7 @@ module Gitlab strategy :RefsPolicy, if: -> (config) { config.is_a?(Array) } strategy :ComplexPolicy, if: -> (config) { config.is_a?(Hash) } - DEFAULT_ONLY = { refs: %w[branches tags] }.freeze + DEFAULT_GLOBAL_ONLY = { refs: %w[branches tags] }.freeze class RefsPolicy < ::Gitlab::Config::Entry::Node include ::Gitlab::Config::Entry::Validatable diff --git a/lib/gitlab/ci/pipeline/seed/build.rb b/lib/gitlab/ci/pipeline/seed/build.rb index d8296940a04..db239de0f34 100644 --- a/lib/gitlab/ci/pipeline/seed/build.rb +++ b/lib/gitlab/ci/pipeline/seed/build.rb @@ -13,6 +13,10 @@ module Gitlab @pipeline = pipeline @attributes = attributes + @global_only = Gitlab::Ci::Build::Policy + .fabricate(attributes.delete(:global_only)) + @global_except = Gitlab::Ci::Build::Policy + .fabricate(attributes.delete(:global_except)) @only = Gitlab::Ci::Build::Policy .fabricate(attributes.delete(:only)) @except = Gitlab::Ci::Build::Policy @@ -21,7 +25,9 @@ module Gitlab def included? strong_memoize(:inclusion) do - @only.all? { |spec| spec.satisfied_by?(@pipeline, self) } && + @global_only.all? { |spec| spec.satisfied_by?(@pipeline, self) } && + @global_except.none? { |spec| spec.satisfied_by?(@pipeline, self) } && + @only.all? { |spec| spec.satisfied_by?(@pipeline, self) } && @except.none? { |spec| spec.satisfied_by?(@pipeline, self) } end end diff --git a/lib/gitlab/ci/yaml_processor.rb b/lib/gitlab/ci/yaml_processor.rb index 07ba6f83d47..1ab0130ba39 100644 --- a/lib/gitlab/ci/yaml_processor.rb +++ b/lib/gitlab/ci/yaml_processor.rb @@ -70,6 +70,8 @@ module Gitlab job = @jobs.fetch(attributes[:name].to_sym) attributes + .merge(global_only: job.fetch(:global_only, {})) + .merge(global_except: job.fetch(:global_except, {})) .merge(only: job.fetch(:only, {})) .merge(except: job.fetch(:except, {})) end diff --git a/spec/lib/gitlab/ci/yaml_processor_spec.rb b/spec/lib/gitlab/ci/yaml_processor_spec.rb index 13113a4671a..2d24a11b52b 100644 --- a/spec/lib/gitlab/ci/yaml_processor_spec.rb +++ b/spec/lib/gitlab/ci/yaml_processor_spec.rb @@ -156,6 +156,8 @@ module Gitlab when: "on_success", yaml_variables: [], options: { script: ["rspec"] }, + global_only: { refs: ["branches", "tags"] }, + global_except: {}, only: { refs: ["branches"] }, except: {} }] }, { name: "deploy", @@ -168,6 +170,8 @@ module Gitlab when: "on_success", yaml_variables: [], options: { script: ["cap prod"] }, + global_only: { refs: ["branches", "tags"] }, + global_except: {}, only: { refs: ["tags"] }, except: {} }] }] end -- cgit v1.2.1