diff --git a/lib/kamal/cli/build.rb b/lib/kamal/cli/build.rb index c655c6ae..eb431b33 100644 --- a/lib/kamal/cli/build.rb +++ b/lib/kamal/cli/build.rb @@ -30,18 +30,9 @@ class Kamal::Cli::Build < Kamal::Cli::Base say "Building with uncommitted changes:\n #{uncommitted_changes}", :yellow end - # Get the command here to ensure the Dir.chdir doesn't interfere with it - push = KAMAL.builder.push - run_locally do begin - context_hosts = capture_with_info(*KAMAL.builder.context_hosts).split("\n") - - if context_hosts != KAMAL.builder.config_context_hosts - warn "Context hosts have changed, so re-creating builder, was: #{context_hosts.join(", ")}], now: #{KAMAL.builder.config_context_hosts.join(", ")}" - cli.remove - cli.create - end + execute *KAMAL.builder.buildx_inspect rescue SSHKit::Command::Failed => e if e.message =~ /(context not found|no builder|does not exist)/ warn "Missing compatible builder, so creating a new one first" @@ -51,6 +42,9 @@ class Kamal::Cli::Build < Kamal::Cli::Base end end + # Get the command here to ensure the Dir.chdir doesn't interfere with it + push = KAMAL.builder.push + KAMAL.with_verbosity(:debug) do Dir.chdir(KAMAL.config.builder.build_directory) { execute *push } end diff --git a/lib/kamal/commands/builder.rb b/lib/kamal/commands/builder.rb index c34b6492..1e764154 100644 --- a/lib/kamal/commands/builder.rb +++ b/lib/kamal/commands/builder.rb @@ -1,8 +1,7 @@ require "active_support/core_ext/string/filters" class Kamal::Commands::Builder < Kamal::Commands::Base - delegate :create, :remove, :push, :clean, :pull, :info, :context_hosts, :config_context_hosts, :validate_image, - :first_mirror, to: :target + delegate :create, :remove, :push, :clean, :pull, :info, :buildx_inspect, :validate_image, :first_mirror, to: :target include Clone diff --git a/lib/kamal/commands/builder/base.rb b/lib/kamal/commands/builder/base.rb index a59c5561..763b3142 100644 --- a/lib/kamal/commands/builder/base.rb +++ b/lib/kamal/commands/builder/base.rb @@ -1,4 +1,3 @@ - class Kamal::Commands::Builder::Base < Kamal::Commands::Base class BuilderError < StandardError; end @@ -14,10 +13,29 @@ class Kamal::Commands::Builder::Base < Kamal::Commands::Base docker :image, :rm, "--force", config.absolute_image end + def push + docker :buildx, :build, + "--push", + *platform_options, + "--builder", builder_name, + *build_options, + build_context + end + def pull docker :pull, config.absolute_image end + def info + combine \ + docker(:context, :ls), + docker(:buildx, :ls) + end + + def buildx_inspect + docker :buildx, :inspect, builder_name + end + def build_options [ *build_tags, *build_cache, *build_labels, *build_args, *build_secrets, *build_dockerfile, *build_target, *build_ssh ] end @@ -35,14 +53,6 @@ class Kamal::Commands::Builder::Base < Kamal::Commands::Base ) end - def context_hosts - :true - end - - def config_context_hosts - [] - end - def first_mirror docker(:info, "--format '{{index .RegistryConfig.Mirrors 0}}'") end diff --git a/lib/kamal/commands/builder/hybrid.rb b/lib/kamal/commands/builder/hybrid.rb index 3db6a10c..ac1d2e6c 100644 --- a/lib/kamal/commands/builder/hybrid.rb +++ b/lib/kamal/commands/builder/hybrid.rb @@ -1,4 +1,4 @@ -class Kamal::Commands::Builder::Hybrid < Kamal::Commands::Builder::Base +class Kamal::Commands::Builder::Hybrid < Kamal::Commands::Builder::Remote def create combine \ create_local_buildx, @@ -6,18 +6,9 @@ class Kamal::Commands::Builder::Hybrid < Kamal::Commands::Builder::Base append_remote_buildx end - def context_hosts - chain \ - context_host(builder_name) - end - - def config_context_hosts - [ remote_host ].compact - end - private def builder_name - "kamal-hybrid-#{remote_host.gsub(/[^a-z0-9_-]/, "-")}-#{local_arch}-#{remote_arch}" + "kamal-hybrid-#{local_arch}-#{remote_arch}-#{remote_host.gsub(/[^a-z0-9_-]/, "-")}" end def create_local_buildx @@ -28,7 +19,7 @@ class Kamal::Commands::Builder::Hybrid < Kamal::Commands::Builder::Base docker :buildx, :create, "--append", "--name", builder_name, builder_name, "--platform", "linux/#{remote_arch}" end - def platform_options - [ "--platform", "linux/#{local_arch},linux/#{remote_arch}" ] + def platform + "linux/#{local_arch},linux/#{remote_arch}" end end diff --git a/lib/kamal/commands/builder/local.rb b/lib/kamal/commands/builder/local.rb index 7af8bc06..955143e0 100644 --- a/lib/kamal/commands/builder/local.rb +++ b/lib/kamal/commands/builder/local.rb @@ -7,25 +7,6 @@ class Kamal::Commands::Builder::Local < Kamal::Commands::Builder::Base docker :buildx, :rm, builder_name end - def info - combine \ - docker(:context, :ls), - docker(:buildx, :ls) - end - - def push - docker :buildx, :build, - "--push", - *platform_options, - "--builder", builder_name, - *build_options, - build_context - end - - def context_hosts - docker :buildx, :inspect, builder_name, "> /dev/null" - end - private def builder_name "kamal-local" diff --git a/lib/kamal/commands/builder/remote.rb b/lib/kamal/commands/builder/remote.rb index cad8cd0b..ed34f209 100644 --- a/lib/kamal/commands/builder/remote.rb +++ b/lib/kamal/commands/builder/remote.rb @@ -26,18 +26,9 @@ class Kamal::Commands::Builder::Remote < Kamal::Commands::Builder::Base build_context end - def context_hosts - context_host(builder_name) - end - - def config_context_hosts - [ remote_host ] - end - - private def builder_name - "kamal-remote-#{remote_host.gsub(/[^a-z0-9_-]/, "-")}-#{remote_arch}" + "kamal-remote-#{remote_arch}-#{remote_host.gsub(/[^a-z0-9_-]/, "-")}" end def create_remote_context @@ -57,6 +48,10 @@ class Kamal::Commands::Builder::Remote < Kamal::Commands::Builder::Base end def platform_options - [ "--platform", "linux/#{remote_arch}" ] + [ "--platform", platform ] + end + + def platform + "linux/#{remote_arch}" end end diff --git a/test/commands/builder_test.rb b/test/commands/builder_test.rb index 17e4f4c4..f04e53b6 100644 --- a/test/commands/builder_test.rb +++ b/test/commands/builder_test.rb @@ -158,48 +158,6 @@ class CommandsBuilderTest < ActiveSupport::TestCase builder.push.join(" ") end - test "multiarch context hosts" do - command = new_builder_command - assert_equal "docker buildx inspect kamal-local > /dev/null", command.context_hosts.join(" ") - assert_equal "", command.config_context_hosts.join(" ") - end - - test "native context hosts" do - command = new_builder_command(builder: { "multiarch" => false }) - assert_equal "docker buildx inspect kamal-local > /dev/null", command.context_hosts.join(" ") - assert_equal "", command.config_context_hosts.join(" ") - end - - test "native cached context hosts" do - command = new_builder_command(builder: { "multiarch" => false, "cache" => { "type" => "registry" } }) - assert_equal "docker buildx inspect kamal-local > /dev/null", command.context_hosts.join(" ") - assert_equal "", command.config_context_hosts.join(" ") - end - - test "native remote context hosts" do - command = new_builder_command(builder: { "remote" => { "arch" => "amd64", "host" => "ssh://host" } }) - assert_equal "docker context inspect kamal-remote-amd64 --format '{{.Endpoints.docker.Host}}'", command.context_hosts.join(" ") - assert_equal [ "ssh://host" ], command.config_context_hosts - end - - test "multiarch remote context hosts" do - command = new_builder_command(builder: { - "remote" => { "arch" => "amd64", "host" => "ssh://host" }, - "local" => { "arch" => "arm64" } - }) - assert_equal "docker context inspect kamal-app-multiarch-remote-arm64 --format '{{.Endpoints.docker.Host}}' ; docker context inspect kamal-app-multiarch-remote-amd64 --format '{{.Endpoints.docker.Host}}'", command.context_hosts.join(" ") - assert_equal [ "ssh://host" ], command.config_context_hosts - end - - test "multiarch remote context hosts with local host" do - command = new_builder_command(builder: { - "remote" => { "arch" => "amd64", "host" => "ssh://host" }, - "local" => { "arch" => "arm64", "host" => "unix:///var/run/docker.sock" } - }) - assert_equal "docker context inspect kamal-app-multiarch-remote-arm64 --format '{{.Endpoints.docker.Host}}' ; docker context inspect kamal-app-multiarch-remote-amd64 --format '{{.Endpoints.docker.Host}}'", command.context_hosts.join(" ") - assert_equal [ "unix:///var/run/docker.sock", "ssh://host" ], command.config_context_hosts - end - test "mirror count" do command = new_builder_command assert_equal "docker info --format '{{index .RegistryConfig.Mirrors 0}}'", command.first_mirror.join(" ")