buildkite-builder 3.0.0 → 3.1.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: b19e2a9c84be58408585027ddc221dd1b8d6b5f9303085f874a97b460eaf4b7f
4
- data.tar.gz: 939afa112be8163360618af03a4d21241b3cdceebef90fee3501052d1258e2cc
3
+ metadata.gz: bef719c816c5d64756bbcd041ca2a3fabe8fe59bff47cc154948207ce64a672f
4
+ data.tar.gz: f25762b1422e540f395526df9d6082e1d5908ea0b9b290bf025a365b56c317ff
5
5
  SHA512:
6
- metadata.gz: 54cb436ddc5a4d68da270ae84e62af8902d15388d2d44edaf6f9cc0473bec0f7744286a8e8d9ed1ed5a9d15e8c46e23928a70a51c3078bc1426adb1a1a54c685
7
- data.tar.gz: f67035e4e34f940c2a5b5c2fae14cda001e8185e20e2aa17db7d38574bf3ddcd5bccf47bd6ccf3a96e6325582a88b52cd8594df4a30f080d3eb712863b51ee6f
6
+ metadata.gz: c00f3fba91b34cf907c99ff839612270ef8aecb754c988af52226e979d5e743a508963d1d06d9957b529a2e3ba47cf6ba0951ec33290b8581dc9937b4178033a
7
+ data.tar.gz: 6e62bff8ecdb50b72bdb3afb8fd6fa1385f17ef1852b607f5273570cab1d396ee65d8908a78604d48ccb7a564ad4cb0f9dbff6b94b7a016cfd9ee881e1d4098a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 3.1.0
2
+ * Add subpipeline support to save triggered pipeline's YML definition beforehand to artifacts and pass down the file to an ENV for pipeline setup.
3
+
1
4
  ## 3.0.0
2
5
  * Remove manifest features to prevent Github API dependency and simplify the gem to focus on Buildkite features.
3
6
 
data/README.md CHANGED
@@ -48,7 +48,7 @@ At its core, BKB is really just a YAML builder. This tool allows you to scale yo
48
48
  - Perform pre-build code/diff analysis to determine whether or not to to add a step to the pipeline.
49
49
  - Reorder pipeline steps dynamically.
50
50
  - Augment your pipeline steps with BKB processors.
51
-
51
+
52
52
  ### Pipeline Files
53
53
 
54
54
  Your repo can contain as many pipeline definitions as you'd like. By convention, pipeline file structure are as such:
@@ -74,9 +74,9 @@ Buildkite::Builder.pipeline do
74
74
  label "Rspec", emoji: :rspec
75
75
  command "bundle exec rspec"
76
76
  end
77
-
77
+
78
78
  wait
79
-
79
+
80
80
  trigger do
81
81
  trigger "deploy-pipeline"
82
82
  end
@@ -127,7 +127,7 @@ You can then include the template into the the pipeline once or as many time as
127
127
  ```ruby
128
128
  Buildkite::Builder.pipeline do
129
129
  command(:rspec)
130
-
130
+
131
131
  # Reuse and agument templates on the fly.
132
132
  command(:rspec) do
133
133
  label "Run RSpec again!"
@@ -135,6 +135,36 @@ Buildkite::Builder.pipeline do
135
135
  end
136
136
  ```
137
137
 
138
+ ### Subpipeline
139
+
140
+ While triggering another pipeline, you can predefine subpipeline's steps using `pipeline(NAME_OF_SUBPIPELINE)` in the main pipeline's `pipeline.rb` file and share with main pipeline's plugins and templates definition.
141
+
142
+ `.buildkite/pipelines/pipeline-triggerer/pipeline.rb`
143
+
144
+ ```ruby
145
+ Buildkite::Builder.pipeline do
146
+ pipeline('rspec-pipeline') do
147
+ command do
148
+ label "Run RSpec in separate pipeline"
149
+ end
150
+ end
151
+ end
152
+ ```
153
+
154
+ Inside your Buildkite pipeline setup, you can do the following:
155
+
156
+ In `https://buildkite.com/your-org/rspec-pipeline/steps`
157
+
158
+ ```yaml
159
+ steps:
160
+ - label: ":pipeline:"
161
+ commands:
162
+ - buildkite-agent artifact download $BKB_SUBPIPELINE_FILE . --build $BUILDKITE_TRIGGERED_FROM_BUILD_ID
163
+ - buildkite-agent pipeline upload $BKB_SUBPIPELINE_FILE
164
+ ```
165
+
166
+ This will upload the pregenerated `pipeline.yml` to `rspec-pipeline`.
167
+
138
168
  ## Development
139
169
 
140
170
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.0
1
+ 3.1.0
@@ -9,7 +9,7 @@ module Buildkite
9
9
  @data.each_with_object({}) do |(key, value), hash|
10
10
  value = value.respond_to?(:to_definition) ? value.to_definition : value
11
11
 
12
- next if value.empty?
12
+ next if value.nil? || value.empty?
13
13
 
14
14
  hash[key] = value
15
15
  end
@@ -0,0 +1,96 @@
1
+ require 'securerandom'
2
+
3
+ module Buildkite
4
+ module Builder
5
+ module Extensions
6
+ class SubPipelines < Extension
7
+ class Pipeline
8
+ include Buildkite::Pipelines::Attributes
9
+
10
+ attr_reader :data, :name
11
+
12
+ attribute :depends_on, append: true
13
+ attribute :key
14
+
15
+ def self.to_sym
16
+ name.split('::').last.downcase.to_sym
17
+ end
18
+
19
+ def initialize(name, steps, &block)
20
+ @name = name
21
+ @data = Data.new
22
+ @data.steps = StepCollection.new(
23
+ steps.templates,
24
+ steps.plugins
25
+ )
26
+ @data.notify = []
27
+ @data.env = {}
28
+
29
+ @dsl = Dsl.new(self)
30
+ @dsl.extend(Extensions::Steps)
31
+ @dsl.extend(Extensions::Notify)
32
+ @dsl.extend(Extensions::Env)
33
+ instance_eval(&block) if block_given?
34
+ self
35
+ end
36
+
37
+ def to_h
38
+ attributes = super
39
+ attributes.merge(data.to_definition)
40
+ end
41
+
42
+ def method_missing(method_name, *args, **kwargs, &_block)
43
+ @dsl.public_send(method_name, *args, **kwargs, &_block)
44
+ end
45
+
46
+ def pipeline_yml
47
+ @pipeline_yml ||= "tmp/buildkite-builder/#{SecureRandom.urlsafe_base64}.yml"
48
+ end
49
+ end
50
+
51
+ def prepare
52
+ context.data.pipelines = PipelineCollection.new(context.artifacts)
53
+ end
54
+
55
+ dsl do
56
+ def pipeline(name, template = nil, &block)
57
+ raise "Subpipeline must have a name" if name.empty?
58
+ raise "Subpipeline does not allow nested in another Subpipeline" if context.is_a?(Buildkite::Builder::Extensions::SubPipelines::Pipeline)
59
+ sub_pipeline = Buildkite::Builder::Extensions::SubPipelines::Pipeline.new(name, context.data.steps, &block)
60
+
61
+ context.data.pipelines.add(sub_pipeline)
62
+
63
+ if template
64
+ # Use predefined template
65
+ step = context.data.steps.add(Pipelines::Steps::Trigger, template)
66
+
67
+ if step.build.nil?
68
+ step.build(env: { BKB_SUBPIPELINE_FILE: sub_pipeline.pipeline_yml })
69
+ else
70
+ step.build[:env].merge!(BKB_SUBPIPELINE_FILE: sub_pipeline.pipeline_yml)
71
+ end
72
+ else
73
+ # Generic trigger step
74
+ context.data.steps.add(Pipelines::Steps::Trigger, key: "subpipeline_#{name}_#{context.data.pipelines.count}") do |context|
75
+ key context[:key]
76
+ label name.capitalize
77
+ trigger name
78
+ build(
79
+ message: '${BUILDKITE_MESSAGE}',
80
+ commit: '${BUILDKITE_COMMIT}',
81
+ branch: '${BUILDKITE_BRANCH}',
82
+ env: {
83
+ BUILDKITE_PULL_REQUEST: '${BUILDKITE_PULL_REQUEST}',
84
+ BUILDKITE_PULL_REQUEST_BASE_BRANCH: '${BUILDKITE_PULL_REQUEST_BASE_BRANCH}',
85
+ BUILDKITE_PULL_REQUEST_REPO: '${BUILDKITE_PULL_REQUEST_REPO}',
86
+ BKB_SUBPIPELINE_FILE: sub_pipeline.pipeline_yml
87
+ }
88
+ )
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -6,6 +6,7 @@ module Buildkite
6
6
  autoload :Env, File.expand_path('extensions/env', __dir__)
7
7
  autoload :Lib, File.expand_path('extensions/lib', __dir__)
8
8
  autoload :Notify, File.expand_path('extensions/notify', __dir__)
9
+ autoload :SubPipelines, File.expand_path('extensions/sub_pipelines', __dir__)
9
10
  autoload :Steps, File.expand_path('extensions/steps', __dir__)
10
11
  autoload :Use, File.expand_path('extensions/use', __dir__)
11
12
  end
@@ -37,6 +37,7 @@ module Buildkite
37
37
  use(Extensions::Env)
38
38
  use(Extensions::Notify)
39
39
  use(Extensions::Steps)
40
+ use(Extensions::SubPipelines)
40
41
  end
41
42
 
42
43
  def upload
@@ -0,0 +1,39 @@
1
+ require "forwardable"
2
+
3
+ module Buildkite
4
+ module Builder
5
+ class PipelineCollection
6
+ extend Forwardable
7
+
8
+ attr_reader :pipelines
9
+
10
+ def_delegator :@pipelines, :count
11
+
12
+ def initialize(artifacts)
13
+ @artifacts = artifacts
14
+ @pipelines = []
15
+ end
16
+
17
+ def add(pipeline)
18
+ unless pipeline.is_a?(Buildkite::Builder::Extensions::SubPipelines::Pipeline)
19
+ raise "`#{pipeline}` must be a Buildkite::Builder::Extensions::SubPipelines::Pipeline"
20
+ end
21
+
22
+ pipelines << pipeline
23
+ end
24
+
25
+ def to_definition
26
+ # Instead of generates pipeline.yml, subpipelines save generated file to artifacts
27
+ pipelines.each do |pipeline|
28
+ file = Pathname.new(pipeline.pipeline_yml)
29
+ file.dirname.mkpath
30
+ file.write(YAML.dump(Pipelines::Helpers.sanitize(pipeline.to_h)))
31
+
32
+ @artifacts << file
33
+ end
34
+
35
+ nil
36
+ end
37
+ end
38
+ end
39
+ end
@@ -20,6 +20,7 @@ module Buildkite
20
20
  autoload :Plugin, File.expand_path('builder/plugin', __dir__)
21
21
  autoload :PluginCollection, File.expand_path('builder/plugin_collection', __dir__)
22
22
  autoload :StepCollection, File.expand_path('builder/step_collection', __dir__)
23
+ autoload :PipelineCollection, File.expand_path('builder/pipeline_collection', __dir__)
23
24
  autoload :TemplateManager, File.expand_path('builder/template_manager', __dir__)
24
25
  autoload :PluginManager, File.expand_path('builder/plugin_manager', __dir__)
25
26
 
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: 3.0.0
4
+ version: 3.1.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: 2022-05-31 00:00:00.000000000 Z
12
+ date: 2022-06-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rainbow
@@ -128,6 +128,7 @@ files:
128
128
  - lib/buildkite/builder/extensions/lib.rb
129
129
  - lib/buildkite/builder/extensions/notify.rb
130
130
  - lib/buildkite/builder/extensions/steps.rb
131
+ - lib/buildkite/builder/extensions/sub_pipelines.rb
131
132
  - lib/buildkite/builder/extensions/use.rb
132
133
  - lib/buildkite/builder/group.rb
133
134
  - lib/buildkite/builder/loaders.rb
@@ -136,6 +137,7 @@ files:
136
137
  - lib/buildkite/builder/loaders/templates.rb
137
138
  - lib/buildkite/builder/logging_utils.rb
138
139
  - lib/buildkite/builder/pipeline.rb
140
+ - lib/buildkite/builder/pipeline_collection.rb
139
141
  - lib/buildkite/builder/plugin.rb
140
142
  - lib/buildkite/builder/plugin_collection.rb
141
143
  - lib/buildkite/builder/plugin_manager.rb
@@ -190,7 +192,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
190
192
  - !ruby/object:Gem::Version
191
193
  version: '0'
192
194
  requirements: []
193
- rubygems_version: 3.3.13
195
+ rubygems_version: 3.2.32
194
196
  signing_key:
195
197
  specification_version: 4
196
198
  summary: A gem for programmatically creating Buildkite pipelines.