buildkite-builder 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 024ee5aff1b1a819fe74946becf1e3b1941c9f6e8ea86ba822a2d1411209e098
4
- data.tar.gz: d9716d08bb4512fd0c9490ad26b1618a2b5e4f896693d5f5bca76024500fef70
3
+ metadata.gz: 2fafb5222b3bb1b03be360545674b957e7ecd2ec8d21ea4ac9ff59620626c584
4
+ data.tar.gz: 0223fba9082408be91fc3a38fc2e2a6eaec74e4fb45ec9fa6b121edc10bff07f
5
5
  SHA512:
6
- metadata.gz: 3d8130054bd4276b9d1c4505798985b9032ca406c126de026fa36bbb0fa919541c4c7d4cdd4c510b52e6554febb66c2e8a8bcc8e63aa54061fb727e6cbe8b752
7
- data.tar.gz: 2492024dff09bfd31688c07ca712fed1b3191584f9b5917c4294775f687671decfcd3f0e0b20635bb3941c88b7097129e79700706a916b615ca11867e47a75e9
6
+ metadata.gz: 9fa3c8ca51711531338492b8b2072b2f62fac4bfee34e81594423f25bab218b373182014a72dbcf0b274a004ae4bedf703a62403c1dd1d6b27dbbd040fe0d4da
7
+ data.tar.gz: 8e33e6990c3c6c33a88d380a4d45fbd655848f790cce0a50758a960fab6ffb2cbc59bb359e083cbe072c51141e975bbeb528a7d4269b55dd5e87f52eb2541ce4
data/README.md CHANGED
@@ -28,7 +28,7 @@ steps:
28
28
  - label: ":toolbox:"
29
29
  plugins:
30
30
  - docker#v3.7.0:
31
- image: gusto/buildkite-builder:1.1.0
31
+ image: gusto/buildkite-builder:1.2.0
32
32
  propagate-environment: true
33
33
  ```
34
34
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.2.0
@@ -14,7 +14,6 @@ module Buildkite
14
14
  autoload :Manifest, File.expand_path('builder/manifest', __dir__)
15
15
  autoload :Processors, File.expand_path('builder/processors', __dir__)
16
16
  autoload :Rainbow, File.expand_path('builder/rainbow', __dir__)
17
- autoload :Runner, File.expand_path('builder/runner', __dir__)
18
17
 
19
18
  BUILDKITE_DIRECTORY_NAME = Pathname.new('.buildkite').freeze
20
19
 
@@ -6,6 +6,18 @@ module Buildkite
6
6
  module Builder
7
7
  module Commands
8
8
  class Abstract
9
+ PIPELINES_DIRECTORY = 'pipelines'
10
+ POSSIBLE_PIPELINE_PATHS = [
11
+ File.join('.buildkite', Context::PIPELINE_DEFINITION_FILE),
12
+ File.join('buildkite', Context::PIPELINE_DEFINITION_FILE),
13
+ File.join(Context::PIPELINE_DEFINITION_FILE)
14
+ ].freeze
15
+ POSSIBLE_PIPELINES_PATHS = [
16
+ File.join('.buildkite', PIPELINES_DIRECTORY),
17
+ File.join('buildkite', PIPELINES_DIRECTORY),
18
+ File.join(PIPELINES_DIRECTORY)
19
+ ].freeze
20
+
9
21
  class << self
10
22
  attr_accessor :description
11
23
 
@@ -35,6 +47,8 @@ module Buildkite
35
47
  if options[:help]
36
48
  puts options[:help]
37
49
  return
50
+ elsif !pipeline_path
51
+ abort "Unable to find pipeline"
38
52
  end
39
53
 
40
54
  run
@@ -42,6 +56,10 @@ module Buildkite
42
56
 
43
57
  private
44
58
 
59
+ def pipeline_slug
60
+ ARGV.last || Buildkite.env&.pipeline_slug
61
+ end
62
+
45
63
  def command_name
46
64
  Commands::COMMANDS.key(self.class.name.split('::').last.to_sym)
47
65
  end
@@ -51,12 +69,39 @@ module Buildkite
51
69
  # Subclasses should override to parse options.
52
70
  end
53
71
 
54
- def available_pipelines
55
- @available_pipelines ||= pipelines_path.children.select(&:directory?).map { |dir| dir.basename.to_s }
72
+ def log
73
+ @log ||= begin
74
+ Logger.new($stdout).tap do |logger|
75
+ logger.formatter = proc do |_severity, _datetime, _progname, msg|
76
+ "#{msg}\n"
77
+ end
78
+ end
79
+ end
56
80
  end
57
81
 
58
- def pipelines_path
59
- Builder.root.join(Builder::BUILDKITE_DIRECTORY_NAME).join(Runner::PIPELINES_PATH)
82
+ def pipeline_path
83
+ @pipeline_path ||=
84
+ find_root_by_main_pipeline ||
85
+ find_root_by_multi_pipeline
86
+ end
87
+
88
+ def find_root_by_main_pipeline
89
+ POSSIBLE_PIPELINE_PATHS.map { |path| Builder.root.join(path) }.find(&:exist?)&.dirname
90
+ end
91
+
92
+ def find_root_by_multi_pipeline
93
+ pipelines_path = POSSIBLE_PIPELINES_PATHS.map { |path| Builder.root.join(path) }.find(&:directory?)
94
+
95
+ if pipelines_path
96
+ if pipeline_slug
97
+ path = pipelines_path.join(pipeline_slug)
98
+ path if path.directory?
99
+ elsif pipelines_path.children.one?
100
+ pipelines_path.children.first
101
+ else
102
+ raise 'Your project has multiple pipelines, please specify one.'
103
+ end
104
+ end
60
105
  end
61
106
  end
62
107
  end
@@ -9,21 +9,7 @@ module Buildkite
9
9
  self.description = 'Outputs the pipeline YAML.'
10
10
 
11
11
  def run
12
- pipeline = ARGV.last
13
-
14
- if !pipeline && !root_pipeline?
15
- if available_pipelines.one?
16
- pipeline = available_pipelines.first
17
- else
18
- raise 'You must specify a pipeline'
19
- end
20
- end
21
-
22
- puts Runner.new(pipeline: pipeline).run.to_yaml
23
- end
24
-
25
- def root_pipeline?
26
- pipelines_path.join(Context::PIPELINE_DEFINITION_FILE).exist?
12
+ puts Context.build(pipeline_path).pipeline.to_yaml
27
13
  end
28
14
  end
29
15
  end
@@ -1,25 +1,57 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'tempfile'
4
+
3
5
  module Buildkite
4
6
  module Builder
5
7
  module Commands
6
8
  class Run < Abstract
7
9
  private
10
+ include LoggingUtils
11
+ using Rainbow
8
12
 
9
13
  self.description = 'Builds and uploads the generated pipeline.'
10
14
 
11
15
  def run
16
+ relative_pipeline_path = pipeline_path.relative_path_from(Builder.root)
17
+
12
18
  # This entrypoint is for running on CI. It expects certain environment
13
- # variables to be set.
14
- options = {
15
- upload: true
16
- }
19
+ # variables to be set. It also uploads the pipeline to Buildkite.
20
+ log.info "#{'+++ ' if Buildkite.env}🧰 " + 'Buildkite Builder'.color(:springgreen) + " ─ #{relative_pipeline_path.to_s.yellow}"
21
+ context = Context.new(pipeline_path, logger: log)
22
+
23
+ results = benchmark("\nDone (%s)".color(:springgreen)) do
24
+ context.build
25
+ end
26
+ log.info(results)
27
+
28
+ upload(context.pipeline)
29
+ end
30
+
31
+ private
17
32
 
18
- if available_pipelines.include?(Buildkite.env.pipeline_slug)
19
- options[:pipeline] = Buildkite.env.pipeline_slug
33
+ def pipeline_path
34
+ pipeline_path_override || super
35
+ end
36
+
37
+ def pipeline_path_override
38
+ if ENV['BUILDKITE_BUILDER_PIPELINE_PATH']
39
+ path = Pathname.new(ENV['BUILDKITE_BUILDER_PIPELINE_PATH'])
40
+ path.absolute? ? path : Builder.root.join(path)
20
41
  end
42
+ end
21
43
 
22
- Builder::Runner.new(**options).run
44
+ def upload(pipeline)
45
+ # Upload the pipeline.
46
+ Tempfile.create(['pipeline', '.yml']) do |file|
47
+ file.sync = true
48
+ file.write(pipeline.to_yaml)
49
+
50
+ log.info '+++ :paperclip: Uploading artifact'
51
+ Buildkite::Pipelines::Command.artifact!(:upload, file.path)
52
+ log.info '+++ :pipeline: Uploading pipeline'
53
+ Buildkite::Pipelines::Command.pipeline!(:upload, file.path)
54
+ end
23
55
  end
24
56
  end
25
57
  end
@@ -1,3 +1,5 @@
1
+ require 'logger'
2
+
1
3
  module Buildkite
2
4
  module Builder
3
5
  class Context
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.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ngan Pham
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2021-02-16 00:00:00.000000000 Z
12
+ date: 2021-02-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sorted_set
@@ -148,7 +148,6 @@ files:
148
148
  - lib/buildkite/builder/processors.rb
149
149
  - lib/buildkite/builder/processors/abstract.rb
150
150
  - lib/buildkite/builder/rainbow.rb
151
- - lib/buildkite/builder/runner.rb
152
151
  - lib/buildkite/env.rb
153
152
  - lib/buildkite/pipelines.rb
154
153
  - lib/buildkite/pipelines/api.rb
@@ -1,77 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler'
4
- require 'pathname'
5
- require 'tempfile'
6
- require 'logger'
7
-
8
- module Buildkite
9
- module Builder
10
- class Runner
11
- include LoggingUtils
12
- using Rainbow
13
-
14
- PIPELINES_PATH = Pathname.new('pipelines').freeze
15
-
16
- attr_reader :options
17
-
18
- def initialize(**options)
19
- @options = options
20
- end
21
-
22
- def run
23
- log.info "#{'+++ ' if Buildkite.env}🧰 " + 'Buildkite Builder'.color(:springgreen) + " ─ #{@options[:pipeline].yellow}"
24
- context = Context.new(root, logger: log)
25
-
26
- results = benchmark("\nDone (%s)".color(:springgreen)) do
27
- context.build
28
- end
29
- log.info(results)
30
-
31
- if options[:upload]
32
- upload(context.pipeline)
33
- end
34
-
35
- # Always return the pipeline.
36
- context.pipeline
37
- end
38
-
39
- private
40
-
41
- def log
42
- @log ||= begin
43
- Logger.new($stdout).tap do |logger|
44
- logger.formatter = proc do |_severity, _datetime, _progname, msg|
45
- "#{msg}\n"
46
- end
47
- end
48
- end
49
- end
50
-
51
- def upload(pipeline)
52
- Tempfile.create(['pipeline', '.yml']) do |file|
53
- file.sync = true
54
- file.write(pipeline.to_yaml)
55
-
56
- log.info '+++ :paperclip: Uploading artifact'
57
- Buildkite::Pipelines::Command.artifact!(:upload, file.path)
58
- log.info '+++ :pipeline: Uploading pipeline'
59
- Buildkite::Pipelines::Command.pipeline!(:upload, file.path)
60
- end
61
- end
62
-
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
73
- end
74
- end
75
- end
76
- end
77
- end