Allow disabling of local builds

To disable local builds set:
```
builder:
   local: false
   remote: ssh://docker@docker-builder
```
This commit is contained in:
Donal McBreen
2024-09-03 14:33:25 +01:00
parent 9b9e60ec7f
commit a2549b1f60
5 changed files with 40 additions and 2 deletions

View File

@@ -28,7 +28,9 @@ class Kamal::Configuration::Builder
end end
def local_arches def local_arches
@local_arches ||= if remote @local_arches ||= if local_disabled?
[]
elsif remote
arches & [ Kamal::Utils.docker_arch ] arches & [ Kamal::Utils.docker_arch ]
else else
arches arches
@@ -48,7 +50,7 @@ class Kamal::Configuration::Builder
end end
def local? def local?
arches.empty? || local_arches.any? !local_disabled? && (arches.empty? || local_arches.any?)
end end
def cached? def cached?
@@ -79,6 +81,10 @@ class Kamal::Configuration::Builder
builder_config.fetch("driver", "docker-container") builder_config.fetch("driver", "docker-container")
end end
def local_disabled?
builder_config["local"] == false
end
def cache_from def cache_from
if cached? if cached?
case builder_config["cache"]["type"] case builder_config["cache"]["type"]

View File

@@ -30,6 +30,11 @@ builder:
# If you have a remote builder, you can configure it here # If you have a remote builder, you can configure it here
remote: ssh://docker@docker-builder remote: ssh://docker@docker-builder
# Whether to allow local builds
#
# Defaults to true
local: true
# Builder cache # Builder cache
# #
# The type must be either 'gha' or 'registry' # The type must be either 'gha' or 'registry'

View File

@@ -7,5 +7,7 @@ class Kamal::Configuration::Validator::Builder < Kamal::Configuration::Validator
end end
error "Builder arch not set" unless config["arch"].present? 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
end end

View File

@@ -37,6 +37,14 @@ class CommandsBuilderTest < ActiveSupport::TestCase
builder.push.join(" ") builder.push.join(" ")
end 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 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" } }) builder = new_builder_command(builder: { "arch" => [ "#{remote_arch}" ], "remote" => "ssh://app@host", "cache" => { "type" => "gha" } })
assert_equal "remote", builder.name assert_equal "remote", builder.name

View File

@@ -132,6 +132,23 @@ class ConfigurationBuilderTest < ActiveSupport::TestCase
assert_equal "default=$SSH_AUTH_SOCK", config.builder.ssh assert_equal "default=$SSH_AUTH_SOCK", config.builder.ssh
end 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 private
def config def config
Kamal::Configuration.new(@deploy) Kamal::Configuration.new(@deploy)