buildkite-builder 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +118 -14
  3. data/VERSION +1 -1
  4. data/lib/buildkite/builder.rb +12 -14
  5. data/lib/buildkite/builder/commands/abstract.rb +1 -1
  6. data/lib/buildkite/builder/commands/preview.rb +10 -8
  7. data/lib/buildkite/builder/commands/run.rb +11 -1
  8. data/lib/buildkite/builder/context.rb +70 -0
  9. data/lib/buildkite/builder/loaders/abstract.rb +6 -10
  10. data/lib/buildkite/builder/loaders/manifests.rb +1 -1
  11. data/lib/buildkite/builder/loaders/processors.rb +2 -2
  12. data/lib/buildkite/builder/loaders/templates.rb +1 -1
  13. data/lib/buildkite/builder/manifest.rb +1 -1
  14. data/lib/buildkite/builder/processors/abstract.rb +7 -7
  15. data/lib/buildkite/builder/rainbow.rb +1 -1
  16. data/lib/buildkite/builder/runner.rb +25 -63
  17. data/lib/buildkite/pipelines.rb +1 -0
  18. data/lib/buildkite/pipelines/pipeline.rb +20 -9
  19. data/lib/buildkite/pipelines/step_context.rb +23 -0
  20. data/lib/buildkite/pipelines/steps/abstract.rb +4 -3
  21. data/lib/buildkite/pipelines/steps/block.rb +1 -0
  22. metadata +104 -21
  23. data/lib/vendor/rainbow/Changelog.md +0 -101
  24. data/lib/vendor/rainbow/Gemfile +0 -30
  25. data/lib/vendor/rainbow/LICENSE +0 -20
  26. data/lib/vendor/rainbow/README.markdown +0 -225
  27. data/lib/vendor/rainbow/Rakefile +0 -11
  28. data/lib/vendor/rainbow/lib/rainbow.rb +0 -13
  29. data/lib/vendor/rainbow/lib/rainbow/color.rb +0 -150
  30. data/lib/vendor/rainbow/lib/rainbow/ext/string.rb +0 -64
  31. data/lib/vendor/rainbow/lib/rainbow/global.rb +0 -25
  32. data/lib/vendor/rainbow/lib/rainbow/null_presenter.rb +0 -100
  33. data/lib/vendor/rainbow/lib/rainbow/presenter.rb +0 -144
  34. data/lib/vendor/rainbow/lib/rainbow/refinement.rb +0 -14
  35. data/lib/vendor/rainbow/lib/rainbow/string_utils.rb +0 -22
  36. data/lib/vendor/rainbow/lib/rainbow/version.rb +0 -5
  37. data/lib/vendor/rainbow/lib/rainbow/wrapper.rb +0 -22
  38. data/lib/vendor/rainbow/lib/rainbow/x11_color_names.rb +0 -153
  39. data/lib/vendor/rainbow/rainbow.gemspec +0 -23
@@ -7,12 +7,12 @@ module Buildkite
7
7
  include LoggingUtils
8
8
  using Rainbow
9
9
 
10
- def self.process(runner)
11
- new(runner).run
10
+ def self.process(context)
11
+ new(context).run
12
12
  end
13
13
 
14
- def initialize(runner)
15
- @runner = runner
14
+ def initialize(context)
15
+ @context = context
16
16
  end
17
17
 
18
18
  def run
@@ -21,18 +21,18 @@ module Buildkite
21
21
 
22
22
  private
23
23
 
24
- attr_reader :runner
24
+ attr_reader :context
25
25
 
26
26
  def process
27
27
  raise NotImplementedError
28
28
  end
29
29
 
30
30
  def log
31
- runner.log
31
+ context.logger
32
32
  end
33
33
 
34
34
  def pipeline
35
- runner.pipeline
35
+ context.pipeline
36
36
  end
37
37
 
38
38
  def buildkite
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../../vendor/rainbow/lib/rainbow/refinement'
3
+ require 'rainbow/refinement'
4
4
 
5
5
  module Buildkite
6
6
  module Builder
@@ -8,72 +8,47 @@ require 'logger'
8
8
  module Buildkite
9
9
  module Builder
10
10
  class Runner
11
- include Definition::Helper
12
11
  include LoggingUtils
13
12
  using Rainbow
14
13
 
15
- PIPELINES_PATH = Pathname.new('.buildkite/pipelines').freeze
16
- PIPELINE_DEFINITION_PATH = Pathname.new('pipeline.rb').freeze
14
+ PIPELINES_PATH = Pathname.new('pipelines').freeze
17
15
 
18
16
  attr_reader :options
19
17
 
20
- # This entrypoint is for running on CI. It expects certain environment variables to
21
- # be set.
22
- def self.run
23
- new(
24
- upload: true,
25
- pipeline: Buildkite.env.pipeline_slug
26
- ).run
27
- end
28
-
29
18
  def initialize(**options)
30
- @options = {
31
- verbose: true,
32
- }.merge(options)
19
+ @options = options
33
20
  end
34
21
 
35
22
  def run
36
- log.info "#{'+++ ' if Buildkite.env}🧰 " + 'Buildkite-builder'.color(:springgreen) + " ─ #{@options[:pipeline].yellow}"
23
+ log.info "#{'+++ ' if Buildkite.env}🧰 " + 'Buildkite Builder'.color(:springgreen) + " ─ #{@options[:pipeline].yellow}"
24
+ context = Context.new(root, logger: log)
37
25
 
38
26
  results = benchmark("\nDone (%s)".color(:springgreen)) do
39
- load_manifests
40
- load_templates
41
- load_processors
42
- load_pipeline
43
- run_processors
27
+ context.build
44
28
  end
45
- log.info results
46
-
47
- upload! if options[:upload]
48
- # Always return the pipeline.
29
+ log.info(results)
49
30
 
50
- pipeline
51
- end
31
+ if options[:upload]
32
+ upload(context.pipeline)
33
+ end
52
34
 
53
- def pipeline
54
- @pipeline ||= Buildkite::Pipelines::Pipeline.new
35
+ # Always return the pipeline.
36
+ context.pipeline
55
37
  end
56
38
 
57
- def pipeline_definition
58
- @pipeline_definition ||= begin
59
- expected = Definition::Pipeline
60
- load_definition(Buildkite::Builder.root.join(".buildkite/pipelines/#{options[:pipeline]}").join(PIPELINE_DEFINITION_PATH), expected)
61
- end
62
- end
39
+ private
63
40
 
64
41
  def log
65
42
  @log ||= begin
66
- Logger.new(options[:verbose] ? $stdout : StringIO.new).tap do |lgr|
67
- lgr.formatter = proc do |_severity, _datetime, _progname, msg|
43
+ Logger.new($stdout).tap do |logger|
44
+ logger.formatter = proc do |_severity, _datetime, _progname, msg|
68
45
  "#{msg}\n"
69
46
  end
70
47
  end
71
48
  end
72
49
  end
73
50
 
74
- private
75
-
76
- def upload!
51
+ def upload(pipeline)
77
52
  Tempfile.create(['pipeline', '.yml']) do |file|
78
53
  file.sync = true
79
54
  file.write(pipeline.to_yaml)
@@ -85,31 +60,18 @@ module Buildkite
85
60
  end
86
61
  end
87
62
 
88
- def load_manifests
89
- Loaders::Manifests.load(options[:pipeline]).each do |name, asset|
90
- Manifest[name] = asset
91
- end
92
- end
93
-
94
- def load_templates
95
- Loaders::Templates.load(options[:pipeline]).each do |name, asset|
96
- pipeline.template(name, &asset)
97
- end
98
- end
99
-
100
- def load_processors
101
- Loaders::Processors.load(options[:pipeline])
102
- end
103
-
104
- def run_processors
105
- pipeline.processors.each do |processor|
106
- processor.process(self)
63
+ def root
64
+ @root ||= begin
65
+ path = Builder.root.join(Builder::BUILDKITE_DIRECTORY_NAME)
66
+ if options[:pipeline]
67
+ pipeline_path = path.join(PIPELINES_PATH).join(options[:pipeline])
68
+ if pipeline_path.directory?
69
+ path = pipeline_path
70
+ end
71
+ end
72
+ path
107
73
  end
108
74
  end
109
-
110
- def load_pipeline
111
- pipeline.instance_eval(&pipeline_definition)
112
- end
113
75
  end
114
76
  end
115
77
  end
@@ -8,6 +8,7 @@ module Buildkite
8
8
  autoload :Helpers, File.expand_path('pipelines/helpers', __dir__)
9
9
  autoload :Pipeline, File.expand_path('pipelines/pipeline', __dir__)
10
10
  autoload :Plugin, File.expand_path('pipelines/plugin', __dir__)
11
+ autoload :StepContext, File.expand_path('pipelines/step_context', __dir__)
11
12
  autoload :Steps, File.expand_path('pipelines/steps', __dir__)
12
13
  end
13
14
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'yaml'
4
+ require 'pathname'
4
5
 
5
6
  module Buildkite
6
7
  module Pipelines
@@ -15,6 +16,7 @@ module Buildkite
15
16
  @plugins = {}
16
17
  @templates = {}
17
18
  @processors = []
19
+ @notify = []
18
20
 
19
21
  instance_eval(&definition) if definition
20
22
  instance_eval(&block) if block_given?
@@ -26,8 +28,18 @@ module Buildkite
26
28
  Steps::Input,
27
29
  Steps::Trigger,
28
30
  ].each do |type|
29
- define_method(type.to_sym) do |template = nil, &block|
30
- add(type, template, &block)
31
+ define_method(type.to_sym) do |template = nil, **args, &block|
32
+ add(type, template, **args, &block)
33
+ end
34
+ end
35
+
36
+ def notify(*args)
37
+ if args.empty?
38
+ @notify
39
+ elsif args.first.is_a?(Hash)
40
+ @notify.push(args.first.transform_keys(&:to_s))
41
+ else
42
+ raise ArgumentError, 'value must be hash'
31
43
  end
32
44
  end
33
45
 
@@ -41,8 +53,8 @@ module Buildkite
41
53
  end
42
54
  end
43
55
 
44
- def skip(template = nil, &block)
45
- step = add(Steps::Skip, template, &block)
56
+ def skip(template = nil, **args, &block)
57
+ step = add(Steps::Skip, template, **args, &block)
46
58
  # A skip step has a nil/noop command.
47
59
  step.command(nil)
48
60
  # Always set the skip attribute if it's in a falsey state.
@@ -99,9 +111,8 @@ module Buildkite
99
111
 
100
112
  def to_h
101
113
  pipeline = {}
102
- if env.any?
103
- pipeline[:env] = env
104
- end
114
+ pipeline[:env] = env if env.any?
115
+ pipeline[:notify] = notify if notify.any?
105
116
  pipeline[:steps] = steps.map(&:to_h)
106
117
 
107
118
  Helpers.sanitize(pipeline)
@@ -113,8 +124,8 @@ module Buildkite
113
124
 
114
125
  private
115
126
 
116
- def add(step_class, template = nil, &block)
117
- steps.push(step_class.new(self, find_template(template), &block)).last
127
+ def add(step_class, template = nil, **args, &block)
128
+ steps.push(step_class.new(self, find_template(template), **args, &block)).last
118
129
  end
119
130
 
120
131
  def find_template(name)
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Buildkite
4
+ module Pipelines
5
+ class StepContext
6
+ attr_reader :step
7
+ attr_reader :args
8
+
9
+ def initialize(step, **args)
10
+ @step = step
11
+ @args = args
12
+ end
13
+
14
+ def pipeline
15
+ step.pipeline
16
+ end
17
+
18
+ def [](key)
19
+ args[key]
20
+ end
21
+ end
22
+ end
23
+ end
@@ -13,12 +13,13 @@ module Buildkite
13
13
  name.split('::').last.downcase.to_sym
14
14
  end
15
15
 
16
- def initialize(pipeline, template = nil, &block)
16
+ def initialize(pipeline, template = nil, **args, &block)
17
17
  @pipeline = pipeline
18
18
  @template = template
19
+ context = StepContext.new(self, **args)
19
20
 
20
- instance_eval(&template) if template
21
- instance_eval(&block) if block_given?
21
+ instance_exec(context, &template) if template
22
+ instance_exec(context, &block) if block_given?
22
23
  end
23
24
  end
24
25
  end
@@ -14,6 +14,7 @@ module Buildkite
14
14
  attribute :allow_dependency_failure
15
15
  attribute :branches
16
16
  attribute :fields
17
+ attribute :blocked_state
17
18
  end
18
19
  end
19
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buildkite-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ngan Pham
@@ -9,8 +9,106 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2020-12-01 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2021-02-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sorted_set
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rainbow
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '3'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '3'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: byebug
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: pry-byebug
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rspec
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: webmock
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
14
112
  description: Buildkite Builder is a tool that provides projects using Buildkite to
15
113
  have dynamic pipeline functionality.
16
114
  email:
@@ -35,6 +133,7 @@ files:
35
133
  - lib/buildkite/builder/commands/files.rb
36
134
  - lib/buildkite/builder/commands/preview.rb
37
135
  - lib/buildkite/builder/commands/run.rb
136
+ - lib/buildkite/builder/context.rb
38
137
  - lib/buildkite/builder/definition.rb
39
138
  - lib/buildkite/builder/file_resolver.rb
40
139
  - lib/buildkite/builder/github.rb
@@ -68,6 +167,7 @@ files:
68
167
  - lib/buildkite/pipelines/helpers/timeout_in_minutes.rb
69
168
  - lib/buildkite/pipelines/pipeline.rb
70
169
  - lib/buildkite/pipelines/plugin.rb
170
+ - lib/buildkite/pipelines/step_context.rb
71
171
  - lib/buildkite/pipelines/steps.rb
72
172
  - lib/buildkite/pipelines/steps/abstract.rb
73
173
  - lib/buildkite/pipelines/steps/block.rb
@@ -76,23 +176,6 @@ files:
76
176
  - lib/buildkite/pipelines/steps/skip.rb
77
177
  - lib/buildkite/pipelines/steps/trigger.rb
78
178
  - lib/buildkite/pipelines/steps/wait.rb
79
- - lib/vendor/rainbow/Changelog.md
80
- - lib/vendor/rainbow/Gemfile
81
- - lib/vendor/rainbow/LICENSE
82
- - lib/vendor/rainbow/README.markdown
83
- - lib/vendor/rainbow/Rakefile
84
- - lib/vendor/rainbow/lib/rainbow.rb
85
- - lib/vendor/rainbow/lib/rainbow/color.rb
86
- - lib/vendor/rainbow/lib/rainbow/ext/string.rb
87
- - lib/vendor/rainbow/lib/rainbow/global.rb
88
- - lib/vendor/rainbow/lib/rainbow/null_presenter.rb
89
- - lib/vendor/rainbow/lib/rainbow/presenter.rb
90
- - lib/vendor/rainbow/lib/rainbow/refinement.rb
91
- - lib/vendor/rainbow/lib/rainbow/string_utils.rb
92
- - lib/vendor/rainbow/lib/rainbow/version.rb
93
- - lib/vendor/rainbow/lib/rainbow/wrapper.rb
94
- - lib/vendor/rainbow/lib/rainbow/x11_color_names.rb
95
- - lib/vendor/rainbow/rainbow.gemspec
96
179
  homepage: https://github.com/Gusto/buildkite-builder
97
180
  licenses:
98
181
  - MIT
@@ -116,7 +199,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
199
  - !ruby/object:Gem::Version
117
200
  version: '0'
118
201
  requirements: []
119
- rubygems_version: 3.1.2
202
+ rubygems_version: 3.2.2
120
203
  signing_key:
121
204
  specification_version: 4
122
205
  summary: A gem for programmatically creating Buildkite pipelines.