From 947be0877f3ef7277297bdd20804dfb9eb3fbfab Mon Sep 17 00:00:00 2001 From: Jason Nochlin Date: Sat, 27 Apr 2024 10:24:34 -0600 Subject: [PATCH] add --target option for builder configuration --- lib/kamal/commands/builder/base.rb | 8 ++++++-- lib/kamal/configuration/builder.rb | 4 ++++ test/commands/builder_test.rb | 7 +++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/kamal/commands/builder/base.rb b/lib/kamal/commands/builder/base.rb index 95b079e1..5021de1c 100644 --- a/lib/kamal/commands/builder/base.rb +++ b/lib/kamal/commands/builder/base.rb @@ -3,7 +3,7 @@ class Kamal::Commands::Builder::Base < Kamal::Commands::Base class BuilderError < StandardError; end delegate :argumentize, to: Kamal::Utils - delegate :args, :secrets, :dockerfile, :local_arch, :local_host, :remote_arch, :remote_host, :cache_from, :cache_to, :ssh, :git_archive?, to: :builder_config + delegate :args, :secrets, :dockerfile, :target, :local_arch, :local_host, :remote_arch, :remote_host, :cache_from, :cache_to, :ssh, :git_archive?, to: :builder_config def clean docker :image, :rm, "--force", config.absolute_image @@ -24,7 +24,7 @@ class Kamal::Commands::Builder::Base < Kamal::Commands::Base end def build_options - [ *build_tags, *build_cache, *build_labels, *build_args, *build_secrets, *build_dockerfile, *build_ssh ] + [ *build_tags, *build_cache, *build_labels, *build_args, *build_secrets, *build_dockerfile, *build_target, *build_ssh ] end def build_context @@ -73,6 +73,10 @@ class Kamal::Commands::Builder::Base < Kamal::Commands::Base end end + def build_target + argumentize "--target", target if target.present? + end + def build_ssh argumentize "--ssh", ssh if ssh.present? end diff --git a/lib/kamal/configuration/builder.rb b/lib/kamal/configuration/builder.rb index ea4e71d9..cf69939b 100644 --- a/lib/kamal/configuration/builder.rb +++ b/lib/kamal/configuration/builder.rb @@ -38,6 +38,10 @@ class Kamal::Configuration::Builder def dockerfile @options["dockerfile"] || "Dockerfile" end + + def target + @options["target"] + end def context @options["context"] || (git_archive? ? "-" : ".") diff --git a/test/commands/builder_test.rb b/test/commands/builder_test.rb index b03b0c83..cc7cea0b 100644 --- a/test/commands/builder_test.rb +++ b/test/commands/builder_test.rb @@ -83,6 +83,13 @@ class CommandsBuilderTest < ActiveSupport::TestCase end end + test "build target" do + builder = new_builder_command(builder: { "target" => "prod" }) + assert_equal \ + "-t dhh/app:123 -t dhh/app:latest --label service=\"app\" --file Dockerfile --target prod", + builder.target.build_options.join(" ") + end + test "build context" do builder = new_builder_command(builder: { "context" => ".." }) assert_equal \