From aa28ee0f3ed2be1d695c847988ee244cad1939d5 Mon Sep 17 00:00:00 2001 From: Igor Alexandrov Date: Sun, 18 Jun 2023 22:45:04 +0400 Subject: [PATCH] Inroduce Native::Cached builder --- lib/mrsk/commands/builder.rb | 8 +++++++- lib/mrsk/commands/builder/native.rb | 23 ++++------------------ lib/mrsk/commands/builder/native/cached.rb | 16 +++++++++++++++ test/commands/builder_test.rb | 20 +++++++++++++------ 4 files changed, 41 insertions(+), 26 deletions(-) create mode 100644 lib/mrsk/commands/builder/native/cached.rb diff --git a/lib/mrsk/commands/builder.rb b/lib/mrsk/commands/builder.rb index 45311e08..b100f498 100644 --- a/lib/mrsk/commands/builder.rb +++ b/lib/mrsk/commands/builder.rb @@ -7,8 +7,10 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base def target case - when !config.builder.multiarch? + when !config.builder.multiarch? && !config.builder.cache? native + when !config.builder.multiarch? && config.builder.cache? + native_cached when config.builder.local? && config.builder.remote? multiarch_remote when config.builder.remote? @@ -22,6 +24,10 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base @native ||= Mrsk::Commands::Builder::Native.new(config) end + def native_cached + @native ||= Mrsk::Commands::Builder::Native::Cached.new(config) + end + def native_remote @native ||= Mrsk::Commands::Builder::Native::Remote.new(config) end diff --git a/lib/mrsk/commands/builder/native.rb b/lib/mrsk/commands/builder/native.rb index 709c7878..e1b6880b 100644 --- a/lib/mrsk/commands/builder/native.rb +++ b/lib/mrsk/commands/builder/native.rb @@ -1,32 +1,17 @@ class Mrsk::Commands::Builder::Native < Mrsk::Commands::Builder::Base def create # No-op on native without cache - - if config.builder.cache? - docker :buildx, :create, "--use", "--driver=docker-container" - end end def remove # No-op on native without cache - - if config.builder.cache? - docker :buildx, :rm, builder_name - end end def push - if config.builder.cache? - docker :buildx, :build, - "--push", - *build_options, - build_context - else - combine \ - docker(:build, *build_options, build_context), - docker(:push, config.absolute_image), - docker(:push, config.latest_image) - end + combine \ + docker(:build, *build_options, build_context), + docker(:push, config.absolute_image), + docker(:push, config.latest_image) end def info diff --git a/lib/mrsk/commands/builder/native/cached.rb b/lib/mrsk/commands/builder/native/cached.rb new file mode 100644 index 00000000..9d615b28 --- /dev/null +++ b/lib/mrsk/commands/builder/native/cached.rb @@ -0,0 +1,16 @@ +class Mrsk::Commands::Builder::Native::Cached < Mrsk::Commands::Builder::Native + def create + docker :buildx, :create, "--use", "--driver=docker-container" + end + + def remove + docker :buildx, :rm, builder_name + end + + def push + docker :buildx, :build, + "--push", + *build_options, + build_context + end +end \ No newline at end of file diff --git a/test/commands/builder_test.rb b/test/commands/builder_test.rb index 57b3a24d..f385d127 100644 --- a/test/commands/builder_test.rb +++ b/test/commands/builder_test.rb @@ -6,10 +6,10 @@ class CommandsBuilderTest < ActiveSupport::TestCase end test "target multiarch by default" do - builder = new_builder_command + builder = new_builder_command(builder: { "cache" => { "type" => "gha" }}) assert_equal "multiarch", builder.name assert_equal \ - "docker buildx build --push --platform linux/amd64,linux/arm64 --builder mrsk-app-multiarch -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --file Dockerfile .", + "docker buildx build --push --platform linux/amd64,linux/arm64 --builder mrsk-app-multiarch -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 @@ -21,19 +21,27 @@ class CommandsBuilderTest < ActiveSupport::TestCase builder.push.join(" ") end + test "target native cached when multiarch is off and cache is set" do + builder = new_builder_command(builder: { "multiarch" => false, "cache" => { "type" => "gha" }}) + assert_equal "native/cached", builder.name + assert_equal \ + "docker buildx build --push -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 multiarch remote when local and remote is set" do - builder = new_builder_command(builder: { "local" => { }, "remote" => { } }) + builder = new_builder_command(builder: { "local" => { }, "remote" => { }, "cache" => { "type" => "gha" } }) assert_equal "multiarch/remote", builder.name assert_equal \ - "docker buildx build --push --platform linux/amd64,linux/arm64 --builder mrsk-app-multiarch-remote -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --file Dockerfile .", + "docker buildx build --push --platform linux/amd64,linux/arm64 --builder mrsk-app-multiarch-remote -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 native remote when only remote is set" do - builder = new_builder_command(builder: { "remote" => { "arch" => "amd64" } }) + builder = new_builder_command(builder: { "remote" => { "arch" => "amd64" }, "cache" => { "type" => "gha" } }) assert_equal "native/remote", builder.name assert_equal \ - "docker buildx build --push --platform linux/amd64 --builder mrsk-app-native-remote -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --file Dockerfile .", + "docker buildx build --push --platform linux/amd64 --builder mrsk-app-native-remote -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