Skip to content

Commit

Permalink
Merge pull request #1354 from matthewbjones/feature/docker-build-cloud
Browse files Browse the repository at this point in the history
Adds support for Docker Build Cloud
  • Loading branch information
djmb authored Jan 20, 2025
2 parents a388937 + 2c9bba3 commit f8db5de
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 1 deletion.
8 changes: 7 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?, :cloud?, to: "config.builder"

include Clone

Expand All @@ -17,6 +17,8 @@ def target
else
remote
end
elsif cloud?
cloud
else
local
end
Expand All @@ -33,4 +35,8 @@ def local
def hybrid
@hybrid ||= Kamal::Commands::Builder::Hybrid.new(config)
end

def cloud
@cloud ||= Kamal::Commands::Builder::Cloud.new(config)
end
end
22 changes: 22 additions & 0 deletions lib/kamal/commands/builder/cloud.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Kamal::Commands::Builder::Cloud < Kamal::Commands::Builder::Base
# Expects `driver` to be of format "cloud docker-org-name/builder-name"

def create
docker :buildx, :create, "--driver", driver
end

def remove
docker :buildx, :rm, builder_name
end

private
def builder_name
driver.gsub(/[ \/]/, "-")
end

def inspect_buildx
pipe \
docker(:buildx, :inspect, builder_name),
grep("-q", "Endpoint:.*cloud://.*")
end
end
4 changes: 4 additions & 0 deletions lib/kamal/configuration/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def local?
!local_disabled? && (arches.empty? || local_arches.any?)
end

def cloud?
driver.start_with? "cloud"
end

def cached?
!!builder_config["cache"]
end
Expand Down
3 changes: 3 additions & 0 deletions lib/kamal/configuration/docs/builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ builder:
#
# The build driver to use, defaults to `docker-container`:
driver: docker
#
# If you want to use Docker Build Cloud (https://www.docker.com/products/build-cloud/), you can set the driver to:
driver: cloud org-name/builder-name

# Provenance
#
Expand Down
12 changes: 12 additions & 0 deletions test/cli/build_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ class CliBuildTest < CliTestCase
end
end

test "create cloud" do
run_command("create", fixture: :with_cloud_builder).tap do |output|
assert_match /docker buildx create --driver cloud example_org\/cloud_builder/, output
end
end

test "create with error" do
stub_setup
SSHKit::Backend::Abstract.any_instance.stubs(:execute)
Expand All @@ -252,6 +258,12 @@ class CliBuildTest < CliTestCase
end
end

test "remove cloud" do
run_command("remove", fixture: :with_cloud_builder).tap do |output|
assert_match /docker buildx rm cloud-example_org-cloud_builder/, output
end
end

test "details" do
SSHKit::Backend::Abstract.any_instance.stubs(:capture)
.with(:docker, :context, :ls, "&&", :docker, :buildx, :ls)
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 "cloud builder" do
builder = new_builder_command(builder: { "arch" => [ "#{local_arch}" ], "driver" => "cloud docker-org-name/builder-name" })
assert_equal "cloud", builder.name
assert_equal \
"docker buildx build --push --platform linux/#{local_arch} --builder cloud-docker-org-name-builder-name -t dhh/app:123 -t dhh/app:latest --label service=\"app\" --file Dockerfile .",
builder.push.join(" ")
end

test "build args" do
builder = new_builder_command(builder: { "args" => { "a" => 1, "b" => 2 } })
assert_equal \
Expand Down
40 changes: 40 additions & 0 deletions test/fixtures/deploy_with_cloud_builder.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
service: app
image: dhh/app
servers:
web:
- "1.1.1.1"
- "1.1.1.2"
workers:
- "1.1.1.3"
- "1.1.1.4"
registry:
username: user
password: pw

accessories:
mysql:
image: mysql:5.7
host: 1.1.1.3
port: 3306
env:
clear:
MYSQL_ROOT_HOST: '%'
secret:
- MYSQL_ROOT_PASSWORD
files:
- test/fixtures/files/my.cnf:/etc/mysql/my.cnf
directories:
- data:/var/lib/mysql
redis:
image: redis:latest
roles:
- web
port: 6379
directories:
- data:/data

readiness_delay: 0

builder:
arch: <%= Kamal::Utils.docker_arch == "arm64" ? "amd64" : "arm64" %>
driver: cloud example_org/cloud_builder

0 comments on commit f8db5de

Please sign in to comment.