From b6934b0f41dc7239101d3860d6275870b0f4ae6a Mon Sep 17 00:00:00 2001 From: Samuel Sieg Date: Sat, 4 Mar 2023 10:59:23 +0100 Subject: [PATCH 1/2] Allow configuring the Dockerfile used for building --- lib/mrsk/commands/builder/base.rb | 11 ++++++++++- test/commands/builder_test.rb | 25 ++++++++++++++++--------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/lib/mrsk/commands/builder/base.rb b/lib/mrsk/commands/builder/base.rb index d282fd01..23428fdc 100644 --- a/lib/mrsk/commands/builder/base.rb +++ b/lib/mrsk/commands/builder/base.rb @@ -10,7 +10,8 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base end def build_options - [ *build_tags, *build_labels, *build_args, *build_secrets ] + [ *build_tags, *build_labels, *build_args, *build_secrets, *build_dockerfile ] + end end private @@ -30,6 +31,10 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base argumentize "--secret", secrets.collect { |secret| [ "id", secret ] } end + def build_dockerfile + argumentize "--file", dockerfile + end + def args (config.builder && config.builder["args"]) || {} end @@ -37,4 +42,8 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base def secrets (config.builder && config.builder["secrets"]) || [] end + + def dockerfile + (config.builder && config.builder["dockerfile"]) || "Dockerfile" + end end diff --git a/test/commands/builder_test.rb b/test/commands/builder_test.rb index df7c2912..f528d557 100644 --- a/test/commands/builder_test.rb +++ b/test/commands/builder_test.rb @@ -9,7 +9,7 @@ class CommandsBuilderTest < ActiveSupport::TestCase builder = new_builder_command 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\" .", + "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 .", builder.push.join(" ") end @@ -17,7 +17,7 @@ class CommandsBuilderTest < ActiveSupport::TestCase builder = new_builder_command(builder: { "multiarch" => false }) assert_equal "native", builder.name assert_equal \ - "docker build -t dhh/app:123 -t dhh/app:latest --label service=\"app\" . && docker push dhh/app:123", + "docker build -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --file Dockerfile . && docker push dhh/app:123", builder.push.join(" ") end @@ -25,7 +25,7 @@ class CommandsBuilderTest < ActiveSupport::TestCase builder = new_builder_command(builder: { "local" => { }, "remote" => { } }) 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\" .", + "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 .", builder.push.join(" ") end @@ -33,42 +33,49 @@ class CommandsBuilderTest < ActiveSupport::TestCase builder = new_builder_command(builder: { "remote" => { "arch" => "amd64" } }) 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\" .", + "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 .", builder.push.join(" ") end test "build args" do builder = new_builder_command(builder: { "args" => { "a" => 1, "b" => 2 } }) assert_equal \ - "-t dhh/app:123 -t dhh/app:latest --label service=\"app\" --build-arg a=\"1\" --build-arg b=\"2\"", + "-t dhh/app:123 -t dhh/app:latest --label service=\"app\" --build-arg a=\"1\" --build-arg b=\"2\" --file Dockerfile", builder.target.build_options.join(" ") end test "build secrets" do builder = new_builder_command(builder: { "secrets" => ["token_a", "token_b"] }) assert_equal \ - "-t dhh/app:123 -t dhh/app:latest --label service=\"app\" --secret id=\"token_a\" --secret id=\"token_b\"", + "-t dhh/app:123 -t dhh/app:latest --label service=\"app\" --secret id=\"token_a\" --secret id=\"token_b\" --file Dockerfile", + builder.target.build_options.join(" ") + end + + test "build dockerfile" do + builder = new_builder_command(builder: { "dockerfile" => "Dockerfile.xyz" }) + assert_equal \ + "-t dhh/app:123 -t dhh/app:latest --label service=\"app\" --file Dockerfile.xyz", builder.target.build_options.join(" ") end test "native push with build args" do builder = new_builder_command(builder: { "multiarch" => false, "args" => { "a" => 1, "b" => 2 } }) assert_equal \ - "docker build -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --build-arg a=\"1\" --build-arg b=\"2\" . && docker push dhh/app:123", + "docker build -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --build-arg a=\"1\" --build-arg b=\"2\" --file Dockerfile . && docker push dhh/app:123", builder.push.join(" ") end test "multiarch push with build args" do builder = new_builder_command(builder: { "args" => { "a" => 1, "b" => 2 } }) 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\" --build-arg a=\"1\" --build-arg b=\"2\" .", + "docker buildx build --push --platform linux/amd64,linux/arm64 --builder mrsk-app-multiarch -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --build-arg a=\"1\" --build-arg b=\"2\" --file Dockerfile .", builder.push.join(" ") end test "native push with with build secrets" do builder = new_builder_command(builder: { "multiarch" => false, "secrets" => [ "a", "b" ] }) assert_equal \ - "docker build -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --secret id=\"a\" --secret id=\"b\" . && docker push dhh/app:123", + "docker build -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --secret id=\"a\" --secret id=\"b\" --file Dockerfile . && docker push dhh/app:123", builder.push.join(" ") end From ff88ee0b229cd2b62e6b684195cff61b80d41fff Mon Sep 17 00:00:00 2001 From: Samuel Sieg Date: Sat, 4 Mar 2023 10:59:52 +0100 Subject: [PATCH 2/2] Allow setting the build context used for building --- lib/mrsk/commands/builder/base.rb | 7 +++++++ lib/mrsk/commands/builder/multiarch.rb | 2 +- lib/mrsk/commands/builder/native.rb | 2 +- lib/mrsk/commands/builder/native/remote.rb | 2 +- test/commands/builder_test.rb | 7 +++++++ 5 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/mrsk/commands/builder/base.rb b/lib/mrsk/commands/builder/base.rb index 23428fdc..91276260 100644 --- a/lib/mrsk/commands/builder/base.rb +++ b/lib/mrsk/commands/builder/base.rb @@ -12,6 +12,9 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base def build_options [ *build_tags, *build_labels, *build_args, *build_secrets, *build_dockerfile ] end + + def build_context + context end private @@ -46,4 +49,8 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base def dockerfile (config.builder && config.builder["dockerfile"]) || "Dockerfile" end + + def context + (config.builder && config.builder["context"]) || "." + end end diff --git a/lib/mrsk/commands/builder/multiarch.rb b/lib/mrsk/commands/builder/multiarch.rb index 43fb17d4..3ecd7b97 100644 --- a/lib/mrsk/commands/builder/multiarch.rb +++ b/lib/mrsk/commands/builder/multiarch.rb @@ -13,7 +13,7 @@ class Mrsk::Commands::Builder::Multiarch < Mrsk::Commands::Builder::Base "--platform", "linux/amd64,linux/arm64", "--builder", builder_name, *build_options, - "." + build_context end def info diff --git a/lib/mrsk/commands/builder/native.rb b/lib/mrsk/commands/builder/native.rb index 338e5acf..c4208d6e 100644 --- a/lib/mrsk/commands/builder/native.rb +++ b/lib/mrsk/commands/builder/native.rb @@ -9,7 +9,7 @@ class Mrsk::Commands::Builder::Native < Mrsk::Commands::Builder::Base def push combine \ - docker(:build, *build_options, "."), + docker(:build, *build_options, build_context), docker(:push, config.absolute_image) end diff --git a/lib/mrsk/commands/builder/native/remote.rb b/lib/mrsk/commands/builder/native/remote.rb index 97a6193f..f461c589 100644 --- a/lib/mrsk/commands/builder/native/remote.rb +++ b/lib/mrsk/commands/builder/native/remote.rb @@ -17,7 +17,7 @@ class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native "--platform", platform, "--builder", builder_name, *build_options, - "." + build_context end def info diff --git a/test/commands/builder_test.rb b/test/commands/builder_test.rb index f528d557..77ff07bc 100644 --- a/test/commands/builder_test.rb +++ b/test/commands/builder_test.rb @@ -58,6 +58,13 @@ class CommandsBuilderTest < ActiveSupport::TestCase builder.target.build_options.join(" ") end + test "build context" do + builder = new_builder_command(builder: { "context" => ".." }) + 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 ..", + builder.push.join(" ") + end + test "native push with build args" do builder = new_builder_command(builder: { "multiarch" => false, "args" => { "a" => 1, "b" => 2 } }) assert_equal \