Gusto/buildkite-builder

Pass arguments to templates?

evandam opened this issue · 5 comments

Hi folks, love the idea of this project and curious to see if it would be helpful for managing several of my pipelines.

I don't see anything specifically in the docs/examples, but is it possible to do something like this?

Buildkite::Builder.template do |opts|
  label "Deploy", emoji: :shipit
  command "./buildkite/scripts/deploy.rb --job-name #{opts[:job_name]}"
end

Being able to template out the command a step runs would be super useful and make for some very clean pipelines! Otherwise as far as I can tell, I would have to build out the command every time I reference the template?

ngan commented

Hm, what would the call to the step look like if you're passing in options? Were you thinking something like...

Buildkite::Builder.pipeline do
  command(:my_template, arg1: "something", arg2: "something else")
end

Btw, you can re-open the template as well, like so:

Buildkite::Builder.pipeline do
  command(:my_template) do
    command "./buildkitescripts/deploy.rb --job-name job1234"
  end
end

So, in your template, it'd just be:

Buildkite::Builder.template do |opts|
  label "Deploy", emoji: :shipit
end

It might be useful to know that templates don't have to be valid steps. They can be partial data that can be used/reused for different step (even step types).

Hi @ngan, yeah I was thinking of something that would allow templating the command method itself. It's not a big deal, but with the pipelines I typically write, it's running similar scripts with different CLI options so it would be beneficial to template the command string itself. Just a thought!

ngan commented

Ok, after some thought I think I have a good API for this. All templates will now yield a Context variable. It's optional, so if you don't care for the variable, you don't have to declare it in your block. So something like this:

Buildkite::Builder.template do |context|
  label "Deploy", emoji: :shipit
  command "./buildkite/scripts/deploy.rb --job-name #{context[:job_name]}"
end

The context will also give you access to:

  • context.pipeline (not sure why you'd need this, maybe to check to see if there's an existing step that does something similar already?)
  • context.step (for the current step that you're in, not sure why you'd need this 🤷 )
  • context.args (context#[] is aliased to this)
  • Any future things we want to pass along.
ngan commented

@evandam I just release BKB v1.1.0 that has this feature.

Awesome, I'll give it a shot. Thanks!