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 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
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 @@ -81,6 +81,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
7 changes: 6 additions & 1 deletion lib/kamal/commands/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Kamal::Commands::Builder < Kamal::Commands::Base
delegate :create, :remove, :push, :clean, :pull, :info, :inspect_builder, :validate_image, :first_mirror, to: :target
delegate :local?, :remote?, to: "config.builder"
delegate :local?, :remote?, :pack?, to: "config.builder"

include Clone

Expand All @@ -17,6 +17,8 @@ def target
else
remote
end
elsif pack?
pack
else
local
end
Expand All @@ -34,6 +36,9 @@ def hybrid
@hybrid ||= Kamal::Commands::Builder::Hybrid.new(config)
end

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

def ensure_local_dependencies_installed
if name.native?
Expand Down
3 changes: 2 additions & 1 deletion lib/kamal/commands/builder/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class BuilderError < StandardError; end
delegate :argumentize, to: Kamal::Utils
delegate \
:args, :secrets, :dockerfile, :target, :arches, :local_arches, :remote_arches, :remote,
:pack?, :pack_builder, :pack_buildpacks,
:cache_from, :cache_to, :ssh, :driver, :docker_driver?,
to: :builder_config

Expand Down Expand Up @@ -33,7 +34,7 @@ def info
end

def inspect_builder
docker :buildx, :inspect, builder_name unless docker_driver?
docker :buildx, :inspect, builder_name unless docker_driver? || pack?
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we could extract a buildx? method here?

def buildx?
  !docker_driver? && !pack?
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@djmb We could also run pack builder inspect which returns a bunch of information about the default builder. It's a lot of information but might be useful to help triage if you're not sure what builder you're using. The Pack CLI lets you set your default builder so I have mine set to heroku/builder:24 via pack config default-builder heroku/builder:24

end

def build_options
Expand Down
26 changes: 26 additions & 0 deletions lib/kamal/commands/builder/pack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Kamal::Commands::Builder::Pack < Kamal::Commands::Builder::Base
def push
combine \
pack(:build,
config.repository,
"--platform", platform,
"--builder", pack_builder,
buildpacks,
"-t", config.absolute_image,
"-t", config.latest_image,
"--env", "BP_IMAGE_LABELS=service=#{config.service}",
*argumentize("--env", secrets, sensitive: true),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is using environment variables the standard way to get secrets into a 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.

@djmb Yes, they only have the --env flag.

I just tested building with a few secrets because I was concerned they'd end up in the final image but they don't.

I just found this in the docs site though. TLDR; It's just a build-time env var, they're not available at image runtime. So they're naturally "secret", neat.

https://buildpacks.io/docs/for-platform-operators/how-to/integrate-ci/pack/cli/pack_build/#options

  -e, --env stringArray              Build-time environment variable, in the form 'VAR=VALUE' or 'VAR'.
                                     When using latter value-less form, value will be taken from current
                                       environment at the time this command is executed.
                                     This flag may be specified multiple times and will override
                                       individual values defined by --env-file.
                                     Repeat for each env in order (comma-separated lists not accepted)
                                     NOTE: These are NOT available at image runtime.

"--path", build_context),
docker(:push, config.absolute_image),
docker(:push, config.latest_image)
end

private
def platform
"linux/#{local_arches.first}"
Copy link
Contributor Author

@nickhammond nickhammond Sep 6, 2024

Choose a reason for hiding this comment

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

Pack only supports building for one platform, make it obvious in docs

Copy link
Collaborator

Choose a reason for hiding this comment

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

We can add a validation for this in Kamal::Configuration::Validator::Builder.

end

def buildpacks
(pack_buildpacks << "paketo-buildpacks/image-labels").map { |buildpack| [ "--buildpack", buildpack ] }
Copy link

Choose a reason for hiding this comment

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

I've filed buildpacks/pack#2268 for adding support for --label to pack build, which if/when added would mean the buildpack injection here could be dropped :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Love this, thank you @edmorley!

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

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

def args
builder_config["args"] || {}
end
Expand All @@ -81,6 +85,14 @@ def driver
builder_config.fetch("driver", "docker-container")
end

def pack_builder
builder_config["pack"]["builder"] if pack?
end

def pack_buildpacks
builder_config["pack"]["buildpacks"] if pack?
end

def local_disabled?
builder_config["local"] == false
end
Expand Down
14 changes: 13 additions & 1 deletion lib/kamal/configuration/docs/builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#
# Options go under the builder key in the root configuration.
builder:

# Arch
#
# The architectures to build for — you can set an array or just a single value.
Expand All @@ -31,6 +30,19 @@ builder:
# Defaults to true:
local: true

# 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.

#
# For additional buildpack customization options you can create a project descriptor
# file(project.toml) that the Pack CLI will automatically use.
# See https://buildpacks.io/docs/for-app-developers/how-to/build-inputs/use-project-toml/ for more information.
pack:
builder: heroku/builder:24
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 @@ -61,6 +61,14 @@ class CommandsBuilderTest < ActiveSupport::TestCase
builder.push.join(" ")
end

test "target pack when pack is set" do
builder = new_builder_command(image: "dhh/app", builder: { "arch" => "amd64", "pack" => { "builder" => "heroku/builder:24", "buildpacks" => [ "heroku/ruby", "heroku/procfile" ] } })
assert_equal "pack", builder.name
assert_equal \
"pack build dhh/app --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
17 changes: 17 additions & 0 deletions test/configuration/builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ class ConfigurationBuilderTest < ActiveSupport::TestCase
assert_equal false, config.builder.remote?
end

test "pack?" do
assert_not config.builder.pack?
end

test "pack? with pack builder" do
@deploy[:builder] = { "arch" => "arm64", "pack" => { "builder" => "heroku/builder:24" } }

assert config.builder.pack?
end

test "pack details" do
@deploy[:builder] = { "arch" => "amd64", "pack" => { "builder" => "heroku/builder:24", "buildpacks" => [ "heroku/ruby", "heroku/procfile" ] } }

assert_equal "heroku/builder:24", config.builder.pack_builder
assert_equal [ "heroku/ruby", "heroku/procfile" ], config.builder.pack_buildpacks
end

test "remote" do
assert_nil config.builder.remote
end
Expand Down