Add a pack option to the builder options

This commit is contained in:
Nick Hammond
2024-08-27 22:25:56 -07:00
parent f48987aa03
commit 897b3b4e46
8 changed files with 88 additions and 1 deletions

View File

@@ -77,6 +77,10 @@ module Kamal::Commands
args.compact.unshift :docker args.compact.unshift :docker
end end
def pack(*args)
args.compact.unshift :pack
end
def git(*args, path: nil) def git(*args, path: nil)
[ :git, *([ "-C", path ] if path), *args.compact ] [ :git, *([ "-C", path ] if path), *args.compact ]
end end

View File

@@ -18,6 +18,8 @@ class Kamal::Commands::Builder < Kamal::Commands::Base
else else
native_remote native_remote
end end
elsif config.builder.pack?
pack
else else
multiarch multiarch
end end
@@ -50,6 +52,9 @@ class Kamal::Commands::Builder < Kamal::Commands::Base
@multiarch_remote ||= Kamal::Commands::Builder::Multiarch::Remote.new(config) @multiarch_remote ||= Kamal::Commands::Builder::Multiarch::Remote.new(config)
end end
def pack
@pack ||= Kamal::Commands::Builder::Native::Pack.new(config)
end
def ensure_local_dependencies_installed def ensure_local_dependencies_installed
if name.native? if name.native?

View File

@@ -5,7 +5,7 @@ class Kamal::Commands::Builder::Base < Kamal::Commands::Base
ENDPOINT_DOCKER_HOST_INSPECT = "'{{.Endpoints.docker.Host}}'" ENDPOINT_DOCKER_HOST_INSPECT = "'{{.Endpoints.docker.Host}}'"
delegate :argumentize, to: Kamal::Utils delegate :argumentize, to: Kamal::Utils
delegate :args, :secrets, :dockerfile, :target, :local_arch, :local_host, :remote_arch, :remote_host, :cache_from, :cache_to, :ssh, to: :builder_config delegate :args, :secrets, :dockerfile, :target, :local_arch, :local_host, :pack_arch, :pack_builder, :pack_buildpacks, :remote_arch, :remote_host, :cache_from, :cache_to, :ssh, to: :builder_config
def clean def clean
docker :image, :rm, "--force", config.absolute_image docker :image, :rm, "--force", config.absolute_image

View File

@@ -0,0 +1,26 @@
class Kamal::Commands::Builder::Native::Pack < Kamal::Commands::Builder::Native
def push
combine \
pack(:build,
config.absolute_image,
"--platform", platform,
"--builder", pack_builder,
buildpacks,
"-t", config.absolute_image,
"-t", config.latest_image,
"--env", "BP_IMAGE_LABELS=service=#{config.service}",
secrets.map { |secret| ["--env", Kamal::Utils.sensitive(ENV[secret])] },
"--path", build_context),
docker(:push, config.absolute_image),
docker(:push, config.latest_image)
end
private
def platform
"linux/#{pack_arch}"
end
def buildpacks
(pack_buildpacks << "paketo-buildpacks/image-labels").map { |buildpack| ["--buildpack", buildpack] }
end
end

View File

@@ -35,6 +35,10 @@ class Kamal::Configuration::Builder
!!builder_config["cache"] !!builder_config["cache"]
end end
def pack?
!!builder_config["pack"]
end
def args def args
builder_config["args"] || {} builder_config["args"] || {}
end end
@@ -63,6 +67,18 @@ class Kamal::Configuration::Builder
builder_config["local"]["host"] if local? builder_config["local"]["host"] if local?
end end
def pack_arch
builder_config["pack"]["arch"] if pack?
end
def pack_builder
builder_config["pack"]["builder"] if pack?
end
def pack_buildpacks
builder_config["pack"]["buildpacks"] if pack?
end
def remote_arch def remote_arch
builder_config["remote"]["arch"] if remote? builder_config["remote"]["arch"] if remote?
end end

View File

@@ -37,6 +37,16 @@ builder:
arch: arm64 arch: arm64
host: ssh://docker@docker-builder host: ssh://docker@docker-builder
# Buildpack configuration
#
# The build configuration for using pack to build a Cloud Native Buildpack image.
pack:
builder: heroku/builder:24
arch: amd64
buildpacks:
- heroku/ruby
- heroku/procfile
# Builder cache # Builder cache
# #
# The type must be either 'gha' or 'registry' # The type must be either 'gha' or 'registry'

View File

@@ -53,6 +53,14 @@ class CommandsBuilderTest < ActiveSupport::TestCase
builder.push.join(" ") builder.push.join(" ")
end end
test "target pack when pack is set" do
builder = new_builder_command(builder: { "pack" => { "arch" => "amd64" , "builder" => "heroku/builder:24", "buildpacks" => [ "heroku/ruby", "heroku/procfile" ] }})
assert_equal "native/pack", builder.name
assert_equal \
"pack build dhh/app:123 --platform linux/amd64 --builder heroku/builder:24 --buildpack heroku/ruby --buildpack heroku/procfile --buildpack paketo-buildpacks/image-labels -t dhh/app:123 -t dhh/app:latest --env BP_IMAGE_LABELS=service=app --path . && docker push dhh/app:123 && docker push dhh/app:latest",
builder.push.join(" ")
end
test "build args" do test "build args" do
builder = new_builder_command(builder: { "args" => { "a" => 1, "b" => 2 } }) builder = new_builder_command(builder: { "args" => { "a" => 1, "b" => 2 } })
assert_equal \ assert_equal \

View File

@@ -32,6 +32,24 @@ class ConfigurationBuilderTest < ActiveSupport::TestCase
assert_equal false, config.builder.remote? assert_equal false, config.builder.remote?
end end
test "pack?" do
refute config.builder.pack?
end
test "pack? with pack builder" do
@deploy[:builder] = { "pack" => {"builder" => "heroku/builder:24"} }
assert config.builder.pack?
end
test "pack details" do
@deploy[:builder] = { "pack" => {"arch" => "amd64", "builder" => "heroku/builder:24", "buildpacks" => ["heroku/ruby", "heroku/procfile"]} }
assert_equal "amd64", config.builder.pack_arch
assert_equal "heroku/builder:24", config.builder.pack_builder
assert_equal ["heroku/ruby", "heroku/procfile"], config.builder.pack_buildpacks
end
test "remote_arch" do test "remote_arch" do
assert_nil config.builder.remote_arch assert_nil config.builder.remote_arch
end end