Skip to content

Commit

Permalink
Merge pull request #456 from basecamp/validate-image
Browse files Browse the repository at this point in the history
Validate the build image
  • Loading branch information
djmb authored Sep 12, 2023
2 parents d96e086 + 66291a2 commit 9d49b3e
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/kamal/cli/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def pull
execute *KAMAL.auditor.record("Pulled image with version #{KAMAL.config.version}"), verbosity: :debug
execute *KAMAL.builder.clean, raise_on_non_zero_exit: false
execute *KAMAL.builder.pull
execute *KAMAL.builder.validate_image
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/kamal/commands/builder.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "active_support/core_ext/string/filters"

class Kamal::Commands::Builder < Kamal::Commands::Base
delegate :create, :remove, :push, :clean, :pull, :info, to: :target
delegate :create, :remove, :push, :clean, :pull, :info, :validate_image, to: :target

def name
target.class.to_s.remove("Kamal::Commands::Builder::").underscore.inquiry
Expand Down
6 changes: 6 additions & 0 deletions lib/kamal/commands/builder/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def build_context
config.builder.context
end

def validate_image
pipe \
docker(:inspect, "-f", "'{{ .Config.Labels.service }}'", config.absolute_image),
[:grep, "-x", config.service, "||", "(echo \"Image #{config.absolute_image} is missing the `service` label\" && exit 1)"]
end


private
def build_tags
Expand Down
1 change: 1 addition & 0 deletions test/cli/build_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class CliBuildTest < CliTestCase
run_command("pull").tap do |output|
assert_match /docker image rm --force dhh\/app:999/, output
assert_match /docker pull dhh\/app:999/, output
assert_match "docker inspect -f '{{ .Config.Labels.service }}' dhh/app:999 | grep -x app || (echo \"Image dhh/app:999 is missing the `service` label\" && exit 1)", output
end
end

Expand Down
4 changes: 4 additions & 0 deletions test/commands/builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ class CommandsBuilderTest < ActiveSupport::TestCase
builder.push.join(" ")
end

test "validate image" do
assert_equal "docker inspect -f '{{ .Config.Labels.service }}' dhh/app:123 | grep -x app || (echo \"Image dhh/app:123 is missing the `service` label\" && exit 1)", new_builder_command.validate_image.join(" ")
end

private
def new_builder_command(additional_config = {})
Kamal::Commands::Builder.new(Kamal::Configuration.new(@config.merge(additional_config), version: "123"))
Expand Down

0 comments on commit 9d49b3e

Please sign in to comment.