Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pack option to the builder options for cloud native buildpacks #916

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
897b3b4
Add a pack option to the builder options
nickhammond Aug 28, 2024
826308a
Clean things up via Rubocop
nickhammond Aug 28, 2024
d0ffb85
Utilize repository name for pack name
nickhammond Sep 4, 2024
24f4308
Catch up with main
nickhammond Sep 6, 2024
ae68193
pack arch no longer needed, update builder name in tests
nickhammond Sep 6, 2024
2c5f2a7
Don't need to inspect the builder if pack
nickhammond Sep 6, 2024
548452a
Merge branch 'basecamp:main' into buildpacks
nickhammond Sep 17, 2024
85a5a09
Merge branch 'basecamp:main' into buildpacks
nickhammond Sep 22, 2024
e252004
Use argumentize for secrets with pack
nickhammond Sep 24, 2024
7174174
Merge branch 'basecamp:main' into buildpacks
nickhammond Sep 27, 2024
f7147e0
Merge branch 'basecamp:main' into buildpacks
nickhammond Sep 27, 2024
c601241
Merge branch 'basecamp:main' into buildpacks
nickhammond Oct 1, 2024
dda8efe
Point to project.toml in docs
nickhammond Oct 1, 2024
5482052
Merge branch 'basecamp:main' into buildpacks
nickhammond Oct 2, 2024
89b4415
Ensure build args and secrets are used with pack
nickhammond Oct 2, 2024
1d55c59
Add in pack builder inspect for configured builder
nickhammond Oct 14, 2024
4822a9d
Merge branch 'basecamp:main' into buildpacks
nickhammond Oct 14, 2024
d538447
Add validator for buildpack arch
nickhammond Oct 17, 2024
145b73c
Add a no-op remove method for pack
nickhammond Oct 17, 2024
1ebc8b8
Merge branch 'basecamp:main' into buildpacks
nickhammond Oct 17, 2024
cde5c7a
Catch up with main
nickhammond Oct 28, 2024
8354fbe
Merge branch 'buildpacks' of github.com:nickhammond/kamal into buildp…
nickhammond Oct 28, 2024
9ac3d57
Add default creation time to now for image
nickhammond Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/kamal/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def docker(*args)
args.compact.unshift :docker
end

def pack(*args)
args.compact.unshift :pack
end

def git(*args, path: nil)
[ :git, *([ "-C", path ] if path), *args.compact ]
end
Expand Down
5 changes: 5 additions & 0 deletions lib/kamal/commands/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def target
else
native_remote
end
elsif config.builder.pack?
pack
else
multiarch
end
Expand Down Expand Up @@ -50,6 +52,9 @@ def multiarch_remote
@multiarch_remote ||= Kamal::Commands::Builder::Multiarch::Remote.new(config)
end

def pack
@pack ||= Kamal::Commands::Builder::Native::Pack.new(config)
end

def ensure_local_dependencies_installed
if name.native?
Expand Down
2 changes: 1 addition & 1 deletion lib/kamal/commands/builder/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class BuilderError < StandardError; end
ENDPOINT_DOCKER_HOST_INSPECT = "'{{.Endpoints.docker.Host}}'"

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
docker :image, :rm, "--force", config.absolute_image
Expand Down
26 changes: 26 additions & 0 deletions lib/kamal/commands/builder/native/pack.rb
Original file line number Diff line number Diff line change
@@ -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}",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kamal expects there to be a service label, this automatically adds the label via the paketo-buildpacks/image-labels buildpack.

secrets.map { |secret| ["--env", Kamal::Utils.sensitive(ENV[secret])] },
nickhammond marked this conversation as resolved.
Show resolved Hide resolved
"--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] }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this buildpack automatically so that we can label the image for Kamal

end
end
16 changes: 16 additions & 0 deletions lib/kamal/configuration/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def cached?
!!builder_config["cache"]
end

def pack?
!!builder_config["pack"]
end

def args
builder_config["args"] || {}
end
Expand Down Expand Up @@ -63,6 +67,18 @@ def local_host
builder_config["local"]["host"] if local?
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
builder_config["remote"]["arch"] if remote?
end
Expand Down
10 changes: 10 additions & 0 deletions lib/kamal/configuration/docs/builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ builder:
arch: arm64
host: ssh://docker@docker-builder

# Buildpack configuration
#
# The build configuration for using pack to build a Cloud Native Buildpack image.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add mention of project.toml to set your excluded options. https://buildpacks.io/docs/for-app-developers/how-to/build-inputs/use-project-toml/

Copy link
Contributor Author

@nickhammond nickhammond Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I was thinking about this and removing context: "." it doesn't matter as much since it's using the git clone. The exclusion list is really only relevant when you're using "." as your build context.

pack:
builder: heroku/builder:24
arch: amd64
buildpacks:
- heroku/ruby
- heroku/procfile

# Builder cache
#
# The type must be either 'gha' or 'registry'
Expand Down
8 changes: 8 additions & 0 deletions test/commands/builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ class CommandsBuilderTest < ActiveSupport::TestCase
builder.push.join(" ")
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
builder = new_builder_command(builder: { "args" => { "a" => 1, "b" => 2 } })
assert_equal \
Expand Down
18 changes: 18 additions & 0 deletions test/configuration/builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ class ConfigurationBuilderTest < ActiveSupport::TestCase
assert_equal false, config.builder.remote?
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
assert_nil config.builder.remote_arch
end
Expand Down
Loading