Various code style improvements
This commit is contained in:
@@ -7,9 +7,9 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base
|
||||
|
||||
def target
|
||||
case
|
||||
when !config.builder.multiarch? && !config.builder.cache?
|
||||
when !config.builder.multiarch? && !config.builder.cached?
|
||||
native
|
||||
when !config.builder.multiarch? && config.builder.cache?
|
||||
when !config.builder.multiarch? && config.builder.cached?
|
||||
native_cached
|
||||
when config.builder.local? && config.builder.remote?
|
||||
multiarch_remote
|
||||
|
||||
@@ -3,6 +3,7 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
|
||||
class BuilderError < StandardError; end
|
||||
|
||||
delegate :argumentize, to: Mrsk::Utils
|
||||
delegate :args, :secrets, :dockerfile, :local_arch, :local_host, :remote_arch, :remote_host, :cache_from, :cache_to, to: :builder_config
|
||||
|
||||
def clean
|
||||
docker :image, :rm, "--force", config.absolute_image
|
||||
@@ -27,9 +28,9 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
|
||||
end
|
||||
|
||||
def build_cache
|
||||
if config.builder.cache?
|
||||
["--cache-to", config.builder.cache_to,
|
||||
"--cache-from", config.builder.cache_from]
|
||||
if cache_to && cache_from
|
||||
["--cache-to", cache_to,
|
||||
"--cache-from", cache_from]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -38,18 +39,22 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
|
||||
end
|
||||
|
||||
def build_args
|
||||
argumentize "--build-arg", config.builder.args, sensitive: true
|
||||
argumentize "--build-arg", args, sensitive: true
|
||||
end
|
||||
|
||||
def build_secrets
|
||||
argumentize "--secret", config.builder.secrets.collect { |secret| [ "id", secret ] }
|
||||
argumentize "--secret", secrets.collect { |secret| [ "id", secret ] }
|
||||
end
|
||||
|
||||
def build_dockerfile
|
||||
if Pathname.new(File.expand_path(config.builder.dockerfile)).exist?
|
||||
argumentize "--file", config.builder.dockerfile
|
||||
if Pathname.new(File.expand_path(dockerfile)).exist?
|
||||
argumentize "--file", dockerfile
|
||||
else
|
||||
raise BuilderError, "Missing #{config.builder.dockerfile}"
|
||||
raise BuilderError, "Missing #{dockerfile}"
|
||||
end
|
||||
end
|
||||
|
||||
def builder_config
|
||||
config.builder
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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(config.builder.local_arch), "--platform", "linux/#{config.builder.local_arch}"
|
||||
docker :buildx, :create, "--name", builder_name, builder_name_with_arch(local_arch), "--platform", "linux/#{local_arch}"
|
||||
end
|
||||
|
||||
def append_remote_buildx
|
||||
docker :buildx, :create, "--append", "--name", builder_name, builder_name_with_arch(config.builder.remote_arch), "--platform", "linux/#{config.builder.remote_arch}"
|
||||
docker :buildx, :create, "--append", "--name", builder_name, builder_name_with_arch(remote_arch), "--platform", "linux/#{remote_arch}"
|
||||
end
|
||||
|
||||
def create_contexts
|
||||
combine \
|
||||
create_context(config.builder.local_arch, config.builder.local_host),
|
||||
create_context(config.builder.remote_arch, config.builder.remote_host)
|
||||
create_context(local_arch, local_host),
|
||||
create_context(remote_arch, remote_host)
|
||||
end
|
||||
|
||||
def create_context(arch, host)
|
||||
@@ -41,8 +41,8 @@ class Mrsk::Commands::Builder::Multiarch::Remote < Mrsk::Commands::Builder::Mult
|
||||
|
||||
def remove_contexts
|
||||
combine \
|
||||
remove_context(config.builder.local_arch),
|
||||
remove_context(config.builder.remote_arch)
|
||||
remove_context(local_arch),
|
||||
remove_context(remote_arch)
|
||||
end
|
||||
|
||||
def remove_context(arch)
|
||||
|
||||
@@ -33,16 +33,16 @@ class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native
|
||||
end
|
||||
|
||||
def builder_name_with_arch
|
||||
"#{builder_name}-#{config.builder.remote_arch}"
|
||||
"#{builder_name}-#{remote_arch}"
|
||||
end
|
||||
|
||||
def platform
|
||||
"linux/#{config.builder.remote_arch}"
|
||||
"linux/#{remote_arch}"
|
||||
end
|
||||
|
||||
def create_context
|
||||
docker :context, :create,
|
||||
builder_name_with_arch, "--description", "'#{builder_name} #{config.builder.remote_arch} native host'", "--docker", "'host=#{config.builder.remote_host}'"
|
||||
builder_name_with_arch, "--description", "'#{builder_name} #{remote_arch} native host'", "--docker", "'host=#{remote_host}'"
|
||||
end
|
||||
|
||||
def remove_context
|
||||
|
||||
@@ -23,7 +23,7 @@ class Mrsk::Configuration::Builder
|
||||
!!@options["remote"]
|
||||
end
|
||||
|
||||
def cache?
|
||||
def cached?
|
||||
!!@options["cache"]
|
||||
end
|
||||
|
||||
@@ -60,39 +60,28 @@ class Mrsk::Configuration::Builder
|
||||
end
|
||||
|
||||
def cache_from
|
||||
if cache?
|
||||
if cached?
|
||||
case @options["cache"]["type"]
|
||||
when 'gha'
|
||||
"type=gha"
|
||||
when 'registry'
|
||||
[
|
||||
"type=registry",
|
||||
"ref=#{@server}/#{cache_image}"
|
||||
].compact.join(",")
|
||||
cache_from_config_for_registry
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def cache_to
|
||||
if cache?
|
||||
if cached?
|
||||
case @options["cache"]["type"]
|
||||
when "gha"
|
||||
[
|
||||
"type=gha",
|
||||
@options["cache"]&.fetch("options", nil),
|
||||
].compact.join(",")
|
||||
cache_to_config_for_gha
|
||||
when "registry"
|
||||
[
|
||||
"type=registry",
|
||||
@options["cache"]&.fetch("options", nil),
|
||||
"ref=#{@server}/#{cache_image}"
|
||||
].compact.join(",")
|
||||
cache_to_config_for_registry
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def valid?
|
||||
if @options["local"] && !@options["remote"]
|
||||
raise ArgumentError, "You must specify both local and remote builder config for remote multiarch builds"
|
||||
@@ -107,7 +96,25 @@ class Mrsk::Configuration::Builder
|
||||
@options["cache"]&.fetch("image", nil) || "#{@image}-build-cache"
|
||||
end
|
||||
|
||||
def current_branch
|
||||
`git rev-parse --abbrev-ref HEAD`.strip
|
||||
def cache_from_config_for_registry
|
||||
[
|
||||
"type=registry",
|
||||
"ref=#{@server}/#{cache_image}"
|
||||
].compact.join(",")
|
||||
end
|
||||
|
||||
def cache_to_config_for_gha
|
||||
[
|
||||
"type=gha",
|
||||
@options["cache"]&.fetch("options", nil),
|
||||
].compact.join(",")
|
||||
end
|
||||
|
||||
def cache_to_config_for_registry
|
||||
[
|
||||
"type=registry",
|
||||
@options["cache"]&.fetch("options", nil),
|
||||
"ref=#{@server}/#{cache_image}"
|
||||
].compact.join(",")
|
||||
end
|
||||
end
|
||||
@@ -68,8 +68,8 @@ class ConfigurationBuilderTest < ActiveSupport::TestCase
|
||||
assert_equal "unix:///Users/<%= `whoami`.strip %>/.docker/run/docker.sock", @config_with_builder_option.builder.local_host
|
||||
end
|
||||
|
||||
test "cache?" do
|
||||
assert_equal false, @config.builder.cache?
|
||||
test "cached?" do
|
||||
assert_equal false, @config.builder.cached?
|
||||
end
|
||||
|
||||
test "invalid cache type specified" do
|
||||
|
||||
Reference in New Issue
Block a user