diff --git a/lib/mrsk/cli/build.rb b/lib/mrsk/cli/build.rb index 3e8912d7..ec39282e 100644 --- a/lib/mrsk/cli/build.rb +++ b/lib/mrsk/cli/build.rb @@ -1,7 +1,8 @@ class Mrsk::Cli::Build < Mrsk::Cli::Base desc "deliver", "Build app and push app image to registry then pull image on servers" + option :skip_push, aliases: "-P", type: :boolean, default: false, desc: "Skip image build and push" def deliver - push + push unless options[:skip_push] pull end diff --git a/lib/mrsk/cli/main.rb b/lib/mrsk/cli/main.rb index 73268f50..163c0327 100644 --- a/lib/mrsk/cli/main.rb +++ b/lib/mrsk/cli/main.rb @@ -9,27 +9,28 @@ class Mrsk::Cli::Main < Mrsk::Cli::Base end desc "deploy", "Deploy app to servers" + option :skip_push, aliases: "-P", type: :boolean, default: false, desc: "Skip image build and push" def deploy runtime = print_runtime do say "Ensure curl and Docker are installed...", :magenta - invoke "mrsk:cli:server:bootstrap" + invoke "mrsk:cli:server:bootstrap", [], options.without(:skip_push) say "Log into image registry...", :magenta - invoke "mrsk:cli:registry:login" + invoke "mrsk:cli:registry:login", [], options.without(:skip_push) say "Build and push app image...", :magenta invoke "mrsk:cli:build:deliver" say "Ensure Traefik is running...", :magenta - invoke "mrsk:cli:traefik:boot" + invoke "mrsk:cli:traefik:boot", [], options.without(:skip_push) say "Ensure app can pass healthcheck...", :magenta - invoke "mrsk:cli:healthcheck:perform" + invoke "mrsk:cli:healthcheck:perform", [], options.without(:skip_push) - invoke "mrsk:cli:app:boot" + invoke "mrsk:cli:app:boot", [], options.without(:skip_push) say "Prune old containers and images...", :magenta - invoke "mrsk:cli:prune:all" + invoke "mrsk:cli:prune:all", [], options.without(:skip_push) end audit_broadcast "Deployed app in #{runtime.to_i} seconds" unless options[:skip_broadcast] diff --git a/test/cli/build_test.rb b/test/cli/build_test.rb index 3d5f4431..2ded0193 100644 --- a/test/cli/build_test.rb +++ b/test/cli/build_test.rb @@ -1,10 +1,31 @@ require_relative "cli_test_case" class CliBuildTest < CliTestCase + test "deliver" do + run_command("deliver").tap do |output| + assert_match /docker buildx build --push --platform linux\/amd64,linux\/arm64 --builder mrsk-app-multiarch -t dhh\/app:999 -t dhh\/app:latest --label service="app" --file Dockerfile \. as .*\@localhost/, output + assert_match /docker image rm --force dhh\/app:999 on 1\.1\.1\.2/, output + assert_match /docker pull dhh\/app:999 on 1\.1\.1\.1/, output + end + end + + test "deliver without push" do + run_command("deliver", "--skip-push").tap do |output| + assert_match /docker image rm --force dhh\/app:999 on 1\.1\.1\.2/, output + assert_match /docker pull dhh\/app:999 on 1\.1\.1\.1/, output + end + end + + test "push" do + run_command("push").tap do |output| + assert_match /docker buildx build --push --platform linux\/amd64,linux\/arm64 --builder mrsk-app-multiarch -t dhh\/app:999 -t dhh\/app:latest --label service="app" --file Dockerfile \. as .*\@localhost/, output + end + end + test "pull" do run_command("pull").tap do |output| - assert_match /docker image rm --force dhh\/app:999/, output - assert_match /docker pull dhh\/app:999/, output + assert_match /docker image rm --force dhh\/app:999 on 1\.1\.1\.2/, output + assert_match /docker pull dhh\/app:999 on 1\.1\.1\.1/, output end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 4c381df2..6f29420b 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,7 +9,17 @@ require "mrsk" ActiveSupport::LogSubscriber.logger = ActiveSupport::Logger.new(STDOUT) if ENV["VERBOSE"] +# Applies to remote commands only SSHKit.config.backend = SSHKit::Backend::Printer +# Ensure local commands use the printer backend too +module SSHKit + module DSL + def run_locally(&block) + SSHKit::Backend::Printer.new(SSHKit::Host.new(:local), &block).run + end + end +end + class ActiveSupport::TestCase end