Added support for multistage build cache

This commit is contained in:
Igor Alexandrov
2023-06-15 21:20:06 +04:00
parent f0301d2007
commit 4df3389d09
10 changed files with 349 additions and 60 deletions

View File

@@ -7,11 +7,11 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base
def target
case
when config.builder && config.builder["multiarch"] == false
when !config.builder.multiarch?
native
when config.builder && config.builder["local"] && config.builder["remote"]
when config.builder.local? && config.builder.remote?
multiarch_remote
when config.builder && config.builder["remote"]
when config.builder.remote?
native_remote
else
multiarch

View File

@@ -13,11 +13,11 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
end
def build_options
[ *build_tags, *build_labels, *build_args, *build_secrets, *build_dockerfile ]
[ *build_tags, *build_cache, *build_labels, *build_args, *build_secrets, *build_dockerfile ]
end
def build_context
context
config.builder.context
end
@@ -26,39 +26,30 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
[ "-t", config.absolute_image, "-t", config.latest_image ]
end
def build_cache
if config.builder.cache?
["--cache-to", config.builder.cache_to,
"--cache-from", config.builder.cache_from]
end
end
def build_labels
argumentize "--label", { service: config.service }
end
def build_args
argumentize "--build-arg", args, sensitive: true
argumentize "--build-arg", config.builder.args, sensitive: true
end
def build_secrets
argumentize "--secret", secrets.collect { |secret| [ "id", secret ] }
argumentize "--secret", config.builder.secrets.collect { |secret| [ "id", secret ] }
end
def build_dockerfile
if Pathname.new(File.expand_path(dockerfile)).exist?
argumentize "--file", dockerfile
if Pathname.new(File.expand_path(config.builder.dockerfile)).exist?
argumentize "--file", config.builder.dockerfile
else
raise BuilderError, "Missing #{dockerfile}"
raise BuilderError, "Missing #{config.builder.dockerfile}"
end
end
def args
(config.builder && config.builder["args"]) || {}
end
def secrets
(config.builder && config.builder["secrets"]) || []
end
def dockerfile
(config.builder && config.builder["dockerfile"]) || "Dockerfile"
end
def context
(config.builder && config.builder["context"]) || "."
end
end

View File

@@ -22,17 +22,17 @@ class Mrsk::Commands::Builder::Multiarch::Remote < Mrsk::Commands::Builder::Mult
end
def create_local_buildx
docker :buildx, :create, "--name", builder_name, builder_name_with_arch(local["arch"]), "--platform", "linux/#{local["arch"]}"
docker :buildx, :create, "--name", builder_name, builder_name_with_arch(config.builder.local_arch), "--platform", "linux/#{config.builder.local_arch}"
end
def append_remote_buildx
docker :buildx, :create, "--append", "--name", builder_name, builder_name_with_arch(remote["arch"]), "--platform", "linux/#{remote["arch"]}"
docker :buildx, :create, "--append", "--name", builder_name, builder_name_with_arch(config.builder.remote_arch), "--platform", "linux/#{config.builder.remote_arch}"
end
def create_contexts
combine \
create_context(local["arch"], local["host"]),
create_context(remote["arch"], remote["host"])
create_context(config.builder.local_arch, config.builder.local_host),
create_context(config.builder.remote_arch, config.builder.remote_host)
end
def create_context(arch, host)
@@ -41,19 +41,11 @@ class Mrsk::Commands::Builder::Multiarch::Remote < Mrsk::Commands::Builder::Mult
def remove_contexts
combine \
remove_context(local["arch"]),
remove_context(remote["arch"])
remove_context(config.builder.local_arch),
remove_context(config.builder.remote_arch)
end
def remove_context(arch)
docker :context, :rm, builder_name_with_arch(arch)
end
def local
config.builder["local"]
end
def remote
config.builder["remote"]
end
end

View File

@@ -1,17 +1,32 @@
class Mrsk::Commands::Builder::Native < Mrsk::Commands::Builder::Base
def create
# No-op on native
# 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
# No-op on native without cache
if config.builder.cache?
docker :buildx, :rm, builder_name
end
end
def push
combine \
docker(:build, *build_options, build_context),
docker(:push, config.absolute_image),
docker(:push, config.latest_image)
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
end
def info

View File

@@ -28,29 +28,21 @@ class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native
private
def arch
config.builder["remote"]["arch"]
end
def host
config.builder["remote"]["host"]
end
def builder_name
"mrsk-#{config.service}-native-remote"
end
def builder_name_with_arch
"#{builder_name}-#{arch}"
"#{builder_name}-#{config.builder.remote_arch}"
end
def platform
"linux/#{arch}"
"linux/#{config.builder.remote_arch}"
end
def create_context
docker :context, :create,
builder_name_with_arch, "--description", "'#{builder_name} #{arch} native host'", "--docker", "'host=#{host}'"
builder_name_with_arch, "--description", "'#{builder_name} #{config.builder.remote_arch} native host'", "--docker", "'host=#{config.builder.remote_host}'"
end
def remove_context