Skip to content

Commit

Permalink
Supports Passing SSH Agent Socket to Build Options
Browse files Browse the repository at this point in the history
  • Loading branch information
rience committed Aug 26, 2023
1 parent 9363b6a commit 5780603
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/kamal/commands/builder/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, to: :builder_config
delegate :args, :secrets, :dockerfile, :local_arch, :local_host, :remote_arch, :remote_host, :cache_from, :cache_to, :ssh_agent_socket?, to: :builder_config

def clean
docker :image, :rm, "--force", config.absolute_image
Expand All @@ -14,7 +14,7 @@ def pull
end

def build_options
[ *build_tags, *build_cache, *build_labels, *build_args, *build_secrets, *build_dockerfile ]
[ *build_tags, *build_cache, *build_labels, *build_args, *build_secrets, *build_dockerfile, *build_ssh_agent_socket ]
end

def build_context
Expand Down Expand Up @@ -54,6 +54,12 @@ def build_dockerfile
end
end

def build_ssh_agent_socket
if ssh_agent_socket?
[ "--ssh", "default=$SSH_AUTH_SOCK" ]
end
end

def builder_config
config.builder
end
Expand Down
4 changes: 4 additions & 0 deletions lib/kamal/configuration/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def cache_to
end
end

def ssh_agent_socket?
@options["ssh_agent_socket"] == true
end

private
def valid?
if @options["cache"] && @options["cache"]["type"]
Expand Down
8 changes: 8 additions & 0 deletions test/commands/builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ class CommandsBuilderTest < ActiveSupport::TestCase
builder.push.join(" ")
end

test "build with ssh agent socket" do
builder = new_builder_command(builder: { "ssh_agent_socket" => true })

assert_equal \
"-t dhh/app:123 -t dhh/app:latest --label service=\"app\" --file Dockerfile --ssh default=$SSH_AUTH_SOCK",
builder.target.build_options.join(" ")
end

private
def new_builder_command(additional_config = {})
Kamal::Commands::Builder.new(Kamal::Configuration.new(@config.merge(additional_config), version: "123"))
Expand Down
12 changes: 12 additions & 0 deletions test/configuration/builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,16 @@ class ConfigurationBuilderTest < ActiveSupport::TestCase

assert_equal "..", @config_with_builder_option.builder.context
end

test "ssh_agent_socket?" do
assert_not @config.builder.ssh_agent_socket?
end

test "setting ssh_agent_socket" do
@deploy_with_builder_option[:builder] = { "ssh_agent_socket" => true }

assert @config_with_builder_option.builder.ssh_agent_socket?
assert_equal "type=registry,ref=dhh/app-build-cache", @config_with_builder_option.builder.cache_from
# assert_equal "type=registry,mode=max,image-manifest=true,oci-mediatypes=true,ref=dhh/app-build-cache", @config_with_builder_option.builder.cache_to
end
end

0 comments on commit 5780603

Please sign in to comment.