From a2549b1f6098f86686fef5cf338edb0dc072740f Mon Sep 17 00:00:00 2001 From: Donal McBreen Date: Tue, 3 Sep 2024 14:33:25 +0100 Subject: [PATCH] Allow disabling of local builds To disable local builds set: ``` builder: local: false remote: ssh://docker@docker-builder ``` --- lib/kamal/configuration/builder.rb | 10 ++++++++-- lib/kamal/configuration/docs/builder.yml | 5 +++++ lib/kamal/configuration/validator/builder.rb | 2 ++ test/commands/builder_test.rb | 8 ++++++++ test/configuration/builder_test.rb | 17 +++++++++++++++++ 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/kamal/configuration/builder.rb b/lib/kamal/configuration/builder.rb index 0daf7e74..bad3b386 100644 --- a/lib/kamal/configuration/builder.rb +++ b/lib/kamal/configuration/builder.rb @@ -28,7 +28,9 @@ class Kamal::Configuration::Builder end def local_arches - @local_arches ||= if remote + @local_arches ||= if local_disabled? + [] + elsif remote arches & [ Kamal::Utils.docker_arch ] else arches @@ -48,7 +50,7 @@ class Kamal::Configuration::Builder end def local? - arches.empty? || local_arches.any? + !local_disabled? && (arches.empty? || local_arches.any?) end def cached? @@ -79,6 +81,10 @@ class Kamal::Configuration::Builder builder_config.fetch("driver", "docker-container") end + def local_disabled? + builder_config["local"] == false + end + def cache_from if cached? case builder_config["cache"]["type"] diff --git a/lib/kamal/configuration/docs/builder.yml b/lib/kamal/configuration/docs/builder.yml index 5455f1a2..6209a4a6 100644 --- a/lib/kamal/configuration/docs/builder.yml +++ b/lib/kamal/configuration/docs/builder.yml @@ -30,6 +30,11 @@ builder: # If you have a remote builder, you can configure it here remote: ssh://docker@docker-builder + # Whether to allow local builds + # + # Defaults to true + local: true + # Builder cache # # The type must be either 'gha' or 'registry' diff --git a/lib/kamal/configuration/validator/builder.rb b/lib/kamal/configuration/validator/builder.rb index 4bd815bf..8c8fe1f0 100644 --- a/lib/kamal/configuration/validator/builder.rb +++ b/lib/kamal/configuration/validator/builder.rb @@ -7,5 +7,7 @@ class Kamal::Configuration::Validator::Builder < Kamal::Configuration::Validator end error "Builder arch not set" unless config["arch"].present? + + error "Cannot disable local builds, no remote is set" if config["local"] == false && config["remote"].blank? end end diff --git a/test/commands/builder_test.rb b/test/commands/builder_test.rb index f44f00e0..45c40388 100644 --- a/test/commands/builder_test.rb +++ b/test/commands/builder_test.rb @@ -37,6 +37,14 @@ class CommandsBuilderTest < ActiveSupport::TestCase builder.push.join(" ") end + test "remote build if remote is set and local disabled" do + builder = new_builder_command(builder: { "arch" => [ "amd64", "arm64" ], "remote" => "ssh://app@127.0.0.1", "cache" => { "type" => "gha" }, "local" => false }) + assert_equal "remote", builder.name + assert_equal \ + "docker buildx build --push --platform linux/amd64,linux/arm64 --builder kamal-remote-ssh---app-127-0-0-1 -t dhh/app:123 -t dhh/app:latest --cache-to type=gha --cache-from type=gha --label service=\"app\" --file Dockerfile .", + builder.push.join(" ") + end + test "target remote when remote set and arch is non local" do builder = new_builder_command(builder: { "arch" => [ "#{remote_arch}" ], "remote" => "ssh://app@host", "cache" => { "type" => "gha" } }) assert_equal "remote", builder.name diff --git a/test/configuration/builder_test.rb b/test/configuration/builder_test.rb index 3df477c9..a4fa7fbb 100644 --- a/test/configuration/builder_test.rb +++ b/test/configuration/builder_test.rb @@ -132,6 +132,23 @@ class ConfigurationBuilderTest < ActiveSupport::TestCase assert_equal "default=$SSH_AUTH_SOCK", config.builder.ssh end + test "local disabled but no remote set" do + @deploy[:builder]["local"] = false + + assert_raises(Kamal::ConfigurationError) do + config.builder + end + end + + test "local disabled all arches are remote" do + @deploy[:builder]["local"] = false + @deploy[:builder]["remote"] = "ssh://root@192.168.0.1" + @deploy[:builder]["arch"] = [ "amd64", "arm64" ] + + assert_equal [], config.builder.local_arches + assert_equal [ "amd64", "arm64" ], config.builder.remote_arches + end + private def config Kamal::Configuration.new(@deploy)