From d2a719998af73993228a8852d508f513429e2d94 Mon Sep 17 00:00:00 2001 From: Donal McBreen Date: Wed, 3 Apr 2024 12:39:40 +0100 Subject: [PATCH] Building doesn't need a deploy lock --- lib/kamal/cli/build.rb | 116 +++++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 62 deletions(-) diff --git a/lib/kamal/cli/build.rb b/lib/kamal/cli/build.rb index 1a50f8ff..0e0a2640 100644 --- a/lib/kamal/cli/build.rb +++ b/lib/kamal/cli/build.rb @@ -5,87 +5,81 @@ class Kamal::Cli::Build < Kamal::Cli::Base desc "deliver", "Build app and push app image to registry then pull image on servers" def deliver - mutating do - push - pull - end + push + pull end desc "push", "Build and push app image to registry" def push - mutating do - cli = self + cli = self - verify_local_dependencies - run_hook "pre-build" + verify_local_dependencies + run_hook "pre-build" - uncommitted_changes = Kamal::Git.uncommitted_changes + uncommitted_changes = Kamal::Git.uncommitted_changes - if KAMAL.config.builder.git_clone? - if uncommitted_changes.present? - say "Building from a local git clone, so ignoring these uncommitted changes:\n #{uncommitted_changes}", :yellow - end - - prepare_clone - elsif uncommitted_changes.present? - say "Building with uncommitted changes:\n #{uncommitted_changes}", :yellow + if KAMAL.config.builder.git_clone? + if uncommitted_changes.present? + say "Building from a local git clone, so ignoring these 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 + prepare_clone + elsif uncommitted_changes.present? + say "Building with uncommitted changes:\n #{uncommitted_changes}", :yellow + end - run_locally do - begin - KAMAL.with_verbosity(:debug) do - Dir.chdir(KAMAL.config.builder.build_directory) { execute *push } - end - rescue SSHKit::Command::Failed => e - if e.message =~ /(no builder)|(no such file or directory)/ - warn "Missing compatible builder, so creating a new one first" + # Get the command here to ensure the Dir.chdir doesn't interfere with it + push = KAMAL.builder.push - if cli.create - KAMAL.with_verbosity(:debug) do - Dir.chdir(KAMAL.config.builder.build_directory) { execute *push } - end - end - else - raise - end + run_locally do + begin + KAMAL.with_verbosity(:debug) do + Dir.chdir(KAMAL.config.builder.build_directory) { execute *push } end + rescue SSHKit::Command::Failed => e + if e.message =~ /(no builder)|(no such file or directory)/ + warn "Missing compatible builder, so creating a new one first" + + if cli.create + KAMAL.with_verbosity(:debug) do + Dir.chdir(KAMAL.config.builder.build_directory) { execute *push } + end + end + else + raise + end + else + raise end end end desc "pull", "Pull app image from registry onto servers" def pull - mutating do - on(KAMAL.hosts) do - execute *KAMAL.auditor.record("Pulled image with version #{KAMAL.config.version}"), verbosity: :debug - execute *KAMAL.builder.clean, raise_on_non_zero_exit: false - execute *KAMAL.builder.pull - execute *KAMAL.builder.validate_image - end + on(KAMAL.hosts) do + execute *KAMAL.auditor.record("Pulled image with version #{KAMAL.config.version}"), verbosity: :debug + execute *KAMAL.builder.clean, raise_on_non_zero_exit: false + execute *KAMAL.builder.pull + execute *KAMAL.builder.validate_image end end desc "create", "Create a build setup" def create - mutating do - if (remote_host = KAMAL.config.builder.remote_host) - connect_to_remote_host(remote_host) - end + if (remote_host = KAMAL.config.builder.remote_host) + connect_to_remote_host(remote_host) + end - run_locally do - begin - debug "Using builder: #{KAMAL.builder.name}" - execute *KAMAL.builder.create - rescue SSHKit::Command::Failed => e - if e.message =~ /stderr=(.*)/ - error "Couldn't create remote builder: #{$1}" - false - else - raise - end + run_locally do + begin + debug "Using builder: #{KAMAL.builder.name}" + execute *KAMAL.builder.create + rescue SSHKit::Command::Failed => e + if e.message =~ /stderr=(.*)/ + error "Couldn't create remote builder: #{$1}" + false + else + raise end end end @@ -93,11 +87,9 @@ class Kamal::Cli::Build < Kamal::Cli::Base desc "remove", "Remove build setup" def remove - mutating do - run_locally do - debug "Using builder: #{KAMAL.builder.name}" - execute *KAMAL.builder.remove - end + run_locally do + debug "Using builder: #{KAMAL.builder.name}" + execute *KAMAL.builder.remove end end