1- require "uri"
2-
31class Kamal ::Cli ::Build < Kamal ::Cli ::Base
42 class BuildError < StandardError ; end
53
@@ -38,29 +36,31 @@ def push
3836 say "Building with uncommitted changes:\n #{ uncommitted_changes } " , :yellow
3937 end
4038
41- with_env ( KAMAL . config . builder . secrets ) do
42- run_locally do
43- begin
44- execute *KAMAL . builder . inspect_builder
45- rescue SSHKit ::Command ::Failed => e
46- if e . message =~ /(context not found|no builder|no compatible builder|does not exist)/
47- warn "Missing compatible builder, so creating a new one first"
48- begin
49- cli . remove
50- rescue SSHKit ::Command ::Failed
51- raise unless e . message =~ /(context not found|no builder|does not exist)/
39+ forward_local_registry_port_for_remote_builder do
40+ with_env ( KAMAL . config . builder . secrets ) do
41+ run_locally do
42+ begin
43+ execute *KAMAL . builder . inspect_builder
44+ rescue SSHKit ::Command ::Failed => e
45+ if e . message =~ /(context not found|no builder|no compatible builder|does not exist)/
46+ warn "Missing compatible builder, so creating a new one first"
47+ begin
48+ cli . remove
49+ rescue SSHKit ::Command ::Failed
50+ raise unless e . message =~ /(context not found|no builder|does not exist)/
51+ end
52+ cli . create
53+ else
54+ raise
5255 end
53- cli . create
54- else
55- raise
5656 end
57- end
5857
59- # Get the command here to ensure the Dir.chdir doesn't interfere with it
60- push = KAMAL . builder . push ( cli . options [ :output ] , no_cache : cli . options [ :no_cache ] )
58+ # Get the command here to ensure the Dir.chdir doesn't interfere with it
59+ push = KAMAL . builder . push ( cli . options [ :output ] , no_cache : cli . options [ :no_cache ] )
6160
62- KAMAL . with_verbosity ( :debug ) do
63- Dir . chdir ( KAMAL . config . builder . build_directory ) { execute *push , env : KAMAL . builder . push_env }
61+ KAMAL . with_verbosity ( :debug ) do
62+ Dir . chdir ( KAMAL . config . builder . build_directory ) { execute *push , env : KAMAL . builder . push_env }
63+ end
6464 end
6565 end
6666 end
@@ -70,7 +70,7 @@ def push
7070 def pull
7171 login_to_registry_remotely unless KAMAL . registry . local?
7272
73- forward_local_registry_port do
73+ forward_local_registry_port ( KAMAL . hosts ) do
7474 if ( first_hosts = mirror_hosts ) . any?
7575 # Pull on a single host per mirror first to seed them
7676 say "Pulling image on #{ first_hosts . join ( ", " ) } to seed the #{ "mirror" . pluralize ( first_hosts . count ) } ..." , :magenta
@@ -210,11 +210,18 @@ def login_to_registry_remotely
210210 end
211211 end
212212
213- def forward_local_registry_port ( &block )
213+ def forward_local_registry_port_for_remote_builder ( &block )
214+ if KAMAL . builder . remote?
215+ remote_uri = URI ( KAMAL . config . builder . remote )
216+ forward_local_registry_port ( [ remote_uri . host ] , user : remote_uri . user , proxy : nil , ssh_port : remote_uri . port , &block )
217+ else
218+ yield
219+ end
220+ end
221+
222+ def forward_local_registry_port ( hosts , user : KAMAL . config . ssh . user , proxy : KAMAL . config . ssh . proxy , ssh_port : nil , &block )
214223 if KAMAL . config . registry . local?
215- Kamal ::Cli ::PortForwarding .
216- new ( KAMAL . hosts , KAMAL . config . registry . local_port ) .
217- forward ( &block )
224+ PortForwarding . new ( hosts , KAMAL . config . registry . local_port , user : user , proxy : proxy , ssh_port : ssh_port ) . forward ( &block )
218225 else
219226 yield
220227 end
0 commit comments