Skip to content

Commit d963c43

Browse files
committed
Allow overriding compose env for local builds
Now we can write this very simple pipeline for debugging builds locally. ```ruby Buildkite::Builder.pipeline do require "buildkite_config" use Buildkite::Config::BuildContext use Buildkite::Config::DockerBuild use Buildkite::Config::RakeCommand use Buildkite::Config::RubyGroup plugin :docker_compose, "docker-compose#v4.16.0" plugin :artifacts, "artifacts#v1.9.3" build_context.setup_rubies %w(3.3) group do label "build" build_context.rubies.each do |ruby| builder ruby, compose: { "cli_version": "2", "image-name": "buildkite_base", "cache-from": ["buildkite_base"], "push": "", "image-repository": "", } end end build_context.rubies.each do |ruby| ruby_group ruby do rake "actioncable", task: "test:integration", retry_on: { exit_status: -1, limit: 3 }, compose: { "cli_version": "2", "pull": "", }, env: { "IMAGE_NAME": "buildkite_base", } end end end ``` :nail_care: whitespace
1 parent 13b395c commit d963c43

File tree

4 files changed

+143
-25
lines changed

4 files changed

+143
-25
lines changed

lib/buildkite/config/docker_build.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,23 @@ def prepare
4343
end
4444

4545
dsl do
46-
def builder(ruby)
46+
def builder(ruby, compose: nil)
4747
build_context = context.extensions.find(BuildContext)
4848
build_context.ruby = ruby
4949
return unless build_context.ruby.build?
5050

5151
command do
52+
compose_options = {
53+
build: "base",
54+
config: ".buildkite/docker-compose.yml",
55+
env: %w[PRE_STEPS RACK],
56+
"image-name" => build_context.ruby.image_name_for(build_context.build_id),
57+
"cache-from" => cache_from(build_context),
58+
push: build_push(build_context),
59+
"image-repository" => build_context.image_base,
60+
}
61+
compose_options.merge!(compose) if compose
62+
5263
label ":docker: #{build_context.ruby.prefix}#{build_context.ruby.version}"
5364
key "docker-image-#{build_context.ruby.image_key}"
5465
plugin :artifacts, {
@@ -66,16 +77,7 @@ def builder(ruby)
6677
compressed: ".buildkite.tgz"
6778
}
6879

69-
plugin :docker_compose, {
70-
build: "base",
71-
config: ".buildkite/docker-compose.yml",
72-
env: %w[PRE_STEPS RACK],
73-
"image-name" => build_context.ruby.image_name_for(build_context.build_id),
74-
"cache-from" => cache_from(build_context),
75-
push: build_push(build_context),
76-
"image-repository" => build_context.image_base,
77-
}
78-
80+
plugin :docker_compose, compose_options
7981
env({
8082
BUNDLER: build_context.bundler,
8183
RUBYGEMS: build_context.rubygems,

lib/buildkite/config/rake_command.rb

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def build_env(build_context, pre_steps, env)
2020
env ||= {}
2121
pre_steps ||= []
2222

23-
env[:IMAGE_NAME] = build_context.image_name_for(build_context.build_id, prefix: nil)
23+
env[:IMAGE_NAME] ||= build_context.image_name_for(build_context.build_id, prefix: nil)
2424

2525
if build_context.ruby.yjit_enabled?
2626
env[:RUBY_YJIT_ENABLE] = "1"
@@ -33,7 +33,17 @@ def build_env(build_context, pre_steps, env)
3333
env
3434
end
3535

36-
def install_plugins(service = "default", env = nil, dir = ".")
36+
def install_plugins(service = "default", env = nil, dir = ".", compose: nil)
37+
compose_options = {
38+
"env" => env,
39+
"run" => service,
40+
"pull" => service,
41+
"pull-retries" => 3,
42+
"config" => ".buildkite/docker-compose.yml",
43+
"shell" => ["runner", *dir],
44+
}
45+
compose_options.merge!(compose) if compose
46+
3747
plugin :artifacts, {
3848
download: ".dockerignore"
3949
}
@@ -49,14 +59,7 @@ def install_plugins(service = "default", env = nil, dir = ".")
4959
compressed: ".buildkite.tgz"
5060
}
5161

52-
plugin :docker_compose, {
53-
"env" => env,
54-
"run" => service,
55-
"pull" => service,
56-
"pull-retries" => 3,
57-
"config" => ".buildkite/docker-compose.yml",
58-
"shell" => ["runner", *dir],
59-
}.compact
62+
plugin :docker_compose, compose_options.compact
6063
end
6164
end
6265

@@ -65,15 +68,15 @@ def prepare
6568
end
6669

6770
dsl do
68-
def bundle(command, label:, env: nil)
71+
def bundle(command, label:, env: nil, compose: nil)
6972
build_context = context.extensions.find(BuildContext)
7073

7174
command do
7275
label label
7376
depends_on "docker-image-#{build_context.ruby.image_key}"
7477
command command
7578

76-
install_plugins
79+
install_plugins(compose: compose)
7780

7881
env build_env(build_context, nil, env)
7982

@@ -85,7 +88,7 @@ def bundle(command, label:, env: nil)
8588
end
8689
end
8790

88-
def rake(dir, task: "test", label: nil, service: "default", pre_steps: nil, env: nil, retry_on: nil, soft_fail: nil, parallelism: nil)
91+
def rake(dir, task: "test", label: nil, service: "default", pre_steps: nil, env: nil, retry_on: nil, soft_fail: nil, parallelism: nil, compose: nil)
8992
build_context = context.extensions.find(BuildContext)
9093

9194
if task.start_with?("mysql2:") || (build_context.rails_version >= Gem::Version.new("7.1.0.alpha") && task.start_with?("trilogy:"))
@@ -99,7 +102,7 @@ def rake(dir, task: "test", label: nil, service: "default", pre_steps: nil, env:
99102
depends_on "docker-image-#{build_context.ruby.image_key}"
100103
command "rake #{task}"
101104

102-
install_plugins(service, %w[PRE_STEPS RACK], dir)
105+
install_plugins(service, %w[PRE_STEPS RACK], dir, compose: compose)
103106

104107
env build_env(build_context, pre_steps, env)
105108

test/buildkite_config/test_docker_build.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,32 @@ def test_builder_gem_version
153153
assert_equal ["base:buildkite-config-base:ruby-1-9-3-br-main"], compose["cache-from"]
154154
assert_equal ["base:buildkite-config-base:ruby-1-9-3-br-"], compose["push"]
155155
end
156+
157+
def test_builder_compose_options
158+
pipeline = PipelineFixture.new do
159+
use Buildkite::Config::DockerBuild
160+
161+
build_context.stub(:rails_version, Gem::Version.new("7.1")) do
162+
builder Buildkite::Config::RubyConfig.new(version: "3.2"), compose: {
163+
"cli_version": "2",
164+
"image-name": "buildkite_base",
165+
"cache-from": ["buildkite_base"],
166+
"push": "",
167+
"image-repository": "",
168+
}
169+
end
170+
end
171+
172+
plugins = pipeline.to_h["steps"][0]["plugins"]
173+
174+
compose = plugins.find { |plugin|
175+
plugin.key?("docker-compose#v1.0")
176+
}.fetch("docker-compose#v1.0")
177+
178+
assert_equal "2", compose["cli_version"]
179+
assert_equal "buildkite_base", compose["image-name"]
180+
assert_equal ["buildkite_base"], compose["cache-from"]
181+
assert_equal "", compose["push"]
182+
assert_equal "", compose["image-repository"]
183+
end
156184
end

test/buildkite_config/test_rake_command.rb

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ def test_env
147147
assert_equal "buildkite-config-base:ruby-3-2-local", pipeline.to_h["steps"][0]["env"]["IMAGE_NAME"]
148148
end
149149

150+
def test_env_image_name
151+
pipeline = PipelineFixture.new do
152+
build_context.ruby = Buildkite::Config::RubyConfig.new(prefix: "ruby:", version: Gem::Version.new("3.2"))
153+
use Buildkite::Config::RakeCommand
154+
155+
build_context.stub(:rails_version, Gem::Version.new("7.1")) do
156+
rake "test", task: "test:all", env: { "IMAGE_NAME": "override-at-command-definition" }
157+
end
158+
end
159+
160+
assert_includes pipeline.to_h["steps"][0], "env"
161+
assert_includes pipeline.to_h["steps"][0]["env"], "IMAGE_NAME"
162+
assert_equal "override-at-command-definition", pipeline.to_h["steps"][0]["env"]["IMAGE_NAME"]
163+
end
164+
150165
def test_timeout_in_minutes
151166
pipeline = PipelineFixture.new do
152167
build_context.ruby = Buildkite::Config::RubyConfig.new(prefix: "ruby:", version: Gem::Version.new("3.2"))
@@ -220,6 +235,39 @@ def test_compose
220235
assert_equal ["runner", "test"], compose["shell"]
221236
end
222237

238+
def test_compose_options
239+
pipeline = PipelineFixture.new do
240+
build_context.ruby = Buildkite::Config::RubyConfig.new(prefix: "ruby:", version: Gem::Version.new("3.2"))
241+
use Buildkite::Config::RakeCommand
242+
243+
build_context.stub(:rails_version, Gem::Version.new("7.1")) do
244+
rake "test", task: "test:all", compose: {
245+
"cli_version": "2",
246+
"pull": "",
247+
}
248+
end
249+
end
250+
251+
plugins = pipeline.to_h["steps"][0]["plugins"]
252+
253+
compose = plugins.find { |plugin|
254+
plugin.key?("docker-compose#v1.0")
255+
}.fetch("docker-compose#v1.0")
256+
257+
%w[env run pull config shell].each do |key|
258+
assert_includes compose, key
259+
end
260+
261+
assert_includes compose["env"], "PRE_STEPS"
262+
assert_includes compose["env"], "RACK"
263+
264+
assert_equal "2", compose["cli_version"]
265+
assert_equal "default", compose["run"]
266+
assert_equal "", compose["pull"]
267+
assert_equal ".buildkite/docker-compose.yml", compose["config"]
268+
assert_equal ["runner", "test"], compose["shell"]
269+
end
270+
223271
def test_multiple
224272
pipeline = PipelineFixture.new do
225273
build_context.ruby = Buildkite::Config::RubyConfig.new(version: Gem::Version.new("3.2"))
@@ -462,4 +510,41 @@ def test_bundle_command
462510
assert_equal ["test-reports/*/*.xml"], step["artifact_paths"]
463511
assert_equal 30, step["timeout_in_minutes"]
464512
end
513+
514+
def test_bundle_command_options
515+
pipeline = PipelineFixture.new do
516+
build_context.ruby = Buildkite::Config::RubyConfig.new(prefix: "ruby:", version: Gem::Version.new("3.2"))
517+
use Buildkite::Config::RakeCommand
518+
519+
build_context.stub(:rails_version, Gem::Version.new("7.1")) do
520+
bundle "rubocop", label: "rubocop", compose: {
521+
"cli_version": "2",
522+
"pull": "",
523+
}, env: {
524+
"IMAGE_NAME": "override-at-command-definition",
525+
}
526+
end
527+
end
528+
529+
step = pipeline.to_h["steps"][0]
530+
assert_equal "override-at-command-definition", step["env"]["IMAGE_NAME"]
531+
532+
plugins = step["plugins"]
533+
534+
compose = plugins.find { |plugin|
535+
plugin.key?("docker-compose#v1.0")
536+
}.fetch("docker-compose#v1.0")
537+
538+
%w[run pull config shell].each do |key|
539+
assert_includes compose, key
540+
end
541+
542+
assert_predicate compose["env"], :nil?
543+
544+
assert_equal "2", compose["cli_version"]
545+
assert_equal "default", compose["run"]
546+
assert_equal "", compose["pull"]
547+
assert_equal ".buildkite/docker-compose.yml", compose["config"]
548+
assert_equal ["runner", "."], compose["shell"]
549+
end
465550
end

0 commit comments

Comments
 (0)