项目作者: Gusto

项目描述 :
A Ruby DSL for programmatically creating Buildkite pipelines.
高级语言: Ruby
项目地址: git://github.com/Gusto/buildkite-builder.git
创建时间: 2020-11-12T20:47:40Z
项目社区:https://github.com/Gusto/buildkite-builder

开源协议:MIT License

下载


Buildkite Builder Build status

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:

  1. gem 'buildkite-builder'

The gem provides a command line tool that lets you perform various operations on your pipelines:

  1. buildkite-builder help

Pipeline 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:

  1. steps:
  2. - label: ":toolbox:"
  3. key: "buildkite-builder"
  4. retry:
  5. automatic:
  6. - exit_status: -1 # Agent was lost
  7. limit: 2
  8. - exit_status: 255 # Forced agent shutdown
  9. limit: 2
  10. plugins:
  11. - docker#v5.12.0:
  12. image: "gusto/buildkite-builder:4.13.0"
  13. mount-buildkite-agent: true
  14. propagate-environment: true

Some things to note:

  • The label can be whatever you like.
  • You’ll want to update the docker plugin version from time to time.
  • You can update the buildkite-builder version 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:

  1. .buildkite/
  2. pipelines/
  3. <your-pipeline1-slug>/
  4. pipeline.rb
  5. <your-pipeline2-slug>/
  6. pipeline.rb

For 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:

  1. Buildkite::Builder.pipeline do
  2. command do
  3. label "Rspec", emoji: :rspec
  4. command "bundle exec rspec"
  5. end
  6. wait
  7. trigger do
  8. trigger "deploy-pipeline"
  9. end
  10. end

Which generates:

  1. steps:
  2. - label: ":rspec: RSpec"
  3. command: "bundle exec rspec"
  4. - wait
  5. - trigger: deploy-pipeline

If 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.

  1. .buildkite/
  2. pipelines/
  3. foobar-widget/
  4. pipeline.rb
  5. templates/
  6. rspec.rb
  7. rubocop.rb

A template is basically a step that was extracted from the pipeline:

.buildkite/pipelines/foobar-widget/templates/rspec.rb

  1. Buildkite::Builder.template do
  2. label "Rspec", emoji: :rspec
  3. commmand "bundle exec rspec"
  4. end

You 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

  1. Buildkite::Builder.pipeline do
  2. command(:rspec)
  3. # Reuse and agument templates on the fly.
  4. command(:rspec) do
  5. label "Run RSpec again!"
  6. end
  7. end

Extensions

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

  1. class DeployExtension < Buildkite::Builder::Extension
  2. dsl do
  3. def deploy_step(&block)
  4. command(:deploy, &block)
  5. end
  6. end
  7. end

.buildkite/pipelines/foobar-widget/pipeline.rb

  1. Buildkite::Builder.pipeline do
  2. deploy_step do
  3. label "Deploy to production (EU)"
  4. command "bundle exec deploy --env production --region eu"
  5. end
  6. end

Extension Templates

Extensions can also provide multiple templates for different scenarios:

  1. class TestExtension < Buildkite::Builder::Extension
  2. template :default do
  3. command "bundle exec rspec"
  4. end
  5. template :rubocop do
  6. command "bundle exec rubocop"
  7. end
  8. end

And used like this in the pipeline file:

  1. command(TestExtension) # Uses the default template
  2. command(TestExtension.template(:rubocop)) do
  3. label "Custom Rubocop label"
  4. end

Development

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.