Inroduce Native::Cached builder

This commit is contained in:
Igor Alexandrov
2023-06-18 22:45:04 +04:00
parent 4df3389d09
commit aa28ee0f3e
4 changed files with 41 additions and 26 deletions

View File

@@ -7,8 +7,10 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base
def target def target
case case
when !config.builder.multiarch? when !config.builder.multiarch? && !config.builder.cache?
native native
when !config.builder.multiarch? && config.builder.cache?
native_cached
when config.builder.local? && config.builder.remote? when config.builder.local? && config.builder.remote?
multiarch_remote multiarch_remote
when config.builder.remote? when config.builder.remote?
@@ -22,6 +24,10 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base
@native ||= Mrsk::Commands::Builder::Native.new(config) @native ||= Mrsk::Commands::Builder::Native.new(config)
end end
def native_cached
@native ||= Mrsk::Commands::Builder::Native::Cached.new(config)
end
def native_remote def native_remote
@native ||= Mrsk::Commands::Builder::Native::Remote.new(config) @native ||= Mrsk::Commands::Builder::Native::Remote.new(config)
end end

View File

@@ -1,32 +1,17 @@
class Mrsk::Commands::Builder::Native < Mrsk::Commands::Builder::Base class Mrsk::Commands::Builder::Native < Mrsk::Commands::Builder::Base
def create def create
# No-op on native without cache # No-op on native without cache
if config.builder.cache?
docker :buildx, :create, "--use", "--driver=docker-container"
end
end end
def remove def remove
# No-op on native without cache # No-op on native without cache
if config.builder.cache?
docker :buildx, :rm, builder_name
end
end end
def push def push
if config.builder.cache? combine \
docker :buildx, :build, docker(:build, *build_options, build_context),
"--push", docker(:push, config.absolute_image),
*build_options, docker(:push, config.latest_image)
build_context
else
combine \
docker(:build, *build_options, build_context),
docker(:push, config.absolute_image),
docker(:push, config.latest_image)
end
end end
def info def info

View File

@@ -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

View File

@@ -6,10 +6,10 @@ class CommandsBuilderTest < ActiveSupport::TestCase
end end
test "target multiarch by default" do 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 "multiarch", builder.name
assert_equal \ 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(" ") builder.push.join(" ")
end end
@@ -21,19 +21,27 @@ class CommandsBuilderTest < ActiveSupport::TestCase
builder.push.join(" ") builder.push.join(" ")
end 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 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 "multiarch/remote", builder.name
assert_equal \ 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(" ") builder.push.join(" ")
end end
test "target native remote when only remote is set" do 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 "native/remote", builder.name
assert_equal \ 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(" ") builder.push.join(" ")
end end