Introduction
Buildkite Builder (BKB) is a Buildkite pipeline builder written in Ruby. It allows you to build your pipeline with a Ruby DSL for dynamically generated pipeline steps.
Gem Installation (optional)
There are 2 components to this toolkit. The buildkite-builder RubyGem and the buildkite-builder Docker image. You technically only need the image to use Buildkite Builder, but installing the gem in your repo helps you preview your pipeline during development.
To install the gem, add this line to your application's Gemfile:
gem 'buildkite-builder'The gem provides a command line tool that lets you perform various operations on your pipelines:
buildkite-builder helpPipeline Installation
As with every Buildkite pipeline, you'll need to define the initial pipeline step. You can do this directly in the Pipeline Settings or with a .buildkite/pipeline.yml file in your repository. You'll need to define a single step to kick off Buildkite Builder:
steps:
- label: ":toolbox:"
key: "buildkite-builder"
retry:
automatic:
- exit_status: -1 # Agent was lost
limit: 2
- exit_status: 255 # Forced agent shutdown
limit: 2
plugins:
- docker#v5.12.0:
image: "gusto/buildkite-builder:4.13.0"
mount-buildkite-agent: true
propagate-environment: trueSome things to note:
- The
labelcan be whatever you like. - You'll want to update the
dockerplugin version from time to time. - You can update the
buildkite-builderversion by bumping the Docker image tag.
Usage
💡 We have a Showcase pipeline (defined in .buildkite/pipelines/showcase/pipeline.rb) that, well, showcases some of the features and possibilities with Buildkite Builder. Sometimes the best way to learning something is seeing how it's used.
At its core, BKB is really just a YAML builder. This tool allows you to scale your needs when it comes to building a Buildkite pipeline. Your pipeline can be as straight forward as you'd like, or as complex as you need. Since you have Ruby at your disposal, you can do some cool things like:
- Perform pre-build code/diff analysis to determine whether or not to to add a step to the pipeline.
- Reorder pipeline steps dynamically.
- Augment your pipeline steps with BKB processors.
Pipeline Files
Your repo can contain as many pipeline definitions as you'd like. By convention, pipeline file structure are as such:
.buildkite/
pipelines/
<your-pipeline1-slug>/
pipeline.rb
<your-pipeline2-slug>/
pipeline.rbFor an example, refer to the dummy pipeline in the fixtures directory.
Defining Steps
Buildkite Builder was designed to be as intuitive as possible by making DSL match Buildkite's attributes and step types. Here's a basic pipeline:
Buildkite::Builder.pipeline do
command do
label "Rspec", emoji: :rspec
command "bundle exec rspec"
end
wait
trigger do
trigger "deploy-pipeline"
end
endWhich generates:
steps:
- label: ":rspec: RSpec"
command: "bundle exec rspec"
- wait
- trigger: deploy-pipelineIf the step type or attribute exists in Buildkite docs, then it should exist in the DSL. The only exception is the if attribute. Since if is a ruby keyword, we've mapped it to condition.
Step Templates
If your pipeline has a lot of steps, you should consider using Step Templates. Templates allow you to break out your build steps into reusable template files.
.buildkite/
pipelines/
foobar-widget/
pipeline.rb
templates/
rspec.rb
rubocop.rbA template is basically a step that was extracted from the pipeline:
.buildkite/pipelines/foobar-widget/templates/rspec.rb
Buildkite::Builder.template do
label "Rspec", emoji: :rspec
commmand "bundle exec rspec"
endYou can then include the template into the the pipeline once or as many time as you need. The template name will be the name of the file (without the extension).
.buildkite/pipelines/foobar-widget/pipeline.rb
Buildkite::Builder.pipeline do
command(:rspec)
# Reuse and agument templates on the fly.
command(:rspec) do
label "Run RSpec again!"
end
endExtensions
Extensions provide additional flexibility to run code and encapsulate reusable patterns in your pipelines. Think of extensions as Ruby modules that let you define custom DSL, step templates, and shared logic that can be used across multiple pipelines.
Extensions are useful when you want to standardize how certain steps are defined, or when you want to use or share complex logic (like deployment, notifications, or test orchestration).
.buildkite/pipelines/foobar-widget/extensions/deploy_extension.rb
class DeployExtension < Buildkite::Builder::Extension
dsl do
def deploy_step(&block)
command(:deploy, &block)
end
end
end.buildkite/pipelines/foobar-widget/pipeline.rb
Buildkite::Builder.pipeline do
deploy_step do
label "Deploy to production (EU)"
command "bundle exec deploy --env production --region eu"
end
endExtension Templates
Extensions can also provide multiple templates for different scenarios:
class TestExtension < Buildkite::Builder::Extension
template :default do
command "bundle exec rspec"
end
template :rubocop do
command "bundle exec rubocop"
end
endAnd used like this in the pipeline file:
command(TestExtension) # Uses the default template
command(TestExtension.template(:rubocop)) do
label "Custom Rubocop label"
endDevelopment
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.
To install this gem onto your local machine, run bundle exec rake install.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/gusto/buildkite-builder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the Buildkite::Builder project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.