Class: Kamal::Dev::Builder
- Inherits:
-
Object
- Object
- Kamal::Dev::Builder
- Defined in:
- lib/kamal/dev/builder.rb
Overview
Builder for building and pushing Docker images
Wraps Docker build and push operations with:
- Build progress display
- Tag management (timestamp, git SHA, custom)
- Error handling for build failures
- Registry authentication
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
Instance Method Summary collapse
-
#build(dockerfile:, context: ".", tag: nil, image_base: nil, build_args: {}, secrets: {}) ⇒ String
Build Docker image from Dockerfile.
-
#docker_available? ⇒ Boolean
Check if Docker is available.
-
#image_exists?(image_ref) ⇒ Boolean
Check if image exists locally.
-
#initialize(config, registry) ⇒ Builder
constructor
Initialize builder with configuration and registry.
-
#login ⇒ Boolean
Authenticate with Docker registry.
-
#push(image_ref) ⇒ Boolean
Push Docker image to registry.
-
#tag_with_git_sha(base_image) ⇒ String?
Tag image with git SHA.
-
#tag_with_timestamp(base_image) ⇒ String
Tag image with timestamp.
Constructor Details
#initialize(config, registry) ⇒ Builder
Initialize builder with configuration and registry
31 32 33 34 |
# File 'lib/kamal/dev/builder.rb', line 31 def initialize(config, registry) @config = config @registry = registry end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
25 26 27 |
# File 'lib/kamal/dev/builder.rb', line 25 def config @config end |
#registry ⇒ Object (readonly)
Returns the value of attribute registry.
25 26 27 |
# File 'lib/kamal/dev/builder.rb', line 25 def registry @registry end |
Instance Method Details
#build(dockerfile:, context: ".", tag: nil, image_base: nil, build_args: {}, secrets: {}) ⇒ String
Build Docker image from Dockerfile
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/kamal/dev/builder.rb', line 46 def build(dockerfile:, context: ".", tag: nil, image_base: nil, build_args: {}, secrets: {}) tag ||= registry. # Use image_base if provided (new format), otherwise fall back to service name (old format) image_ref = if image_base registry.image_tag(image_base, tag) else registry.image_tag(config.service, tag) end # If git clone is enabled, wrap Dockerfile with entrypoint injection if config.git_clone_enabled? build_with_entrypoint( dockerfile: dockerfile, context: context, image: image_ref, build_args: build_args, secrets: secrets ) else command = build_command( dockerfile: dockerfile, context: context, image: image_ref, build_args: build_args, secrets: secrets ) execute_with_output(command, "Building image #{image_ref}...") end image_ref end |
#docker_available? ⇒ Boolean
Check if Docker is available
114 115 116 117 |
# File 'lib/kamal/dev/builder.rb', line 114 def docker_available? result = execute_command(["docker", "version"]) result[:success] end |
#image_exists?(image_ref) ⇒ Boolean
Check if image exists locally
123 124 125 126 |
# File 'lib/kamal/dev/builder.rb', line 123 def image_exists?(image_ref) result = execute_command(["docker", "image", "inspect", image_ref]) result[:success] end |
#login ⇒ Boolean
Authenticate with Docker registry
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/kamal/dev/builder.rb', line 97 def login command = registry.login_command # Login command uses password on command line, so we execute silently result = execute_command(command) unless result[:success] raise Kamal::Dev::RegistryError, "Docker login failed: #{result[:error]}" end true end |
#push(image_ref) ⇒ Boolean
Push Docker image to registry
85 86 87 88 89 90 91 |
# File 'lib/kamal/dev/builder.rb', line 85 def push(image_ref) command = ["docker", "push", image_ref] execute_with_output(command, "Pushing image #{image_ref}...") true end |
#tag_with_git_sha(base_image) ⇒ String?
Tag image with git SHA
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/kamal/dev/builder.rb', line 145 def tag_with_git_sha(base_image) tag = registry.tag_with_git_sha return nil unless tag new_image = "#{base_image}:#{tag}" execute_command(["docker", "tag", base_image, new_image]) new_image end |
#tag_with_timestamp(base_image) ⇒ String
Tag image with timestamp
132 133 134 135 136 137 138 139 |
# File 'lib/kamal/dev/builder.rb', line 132 def (base_image) tag = registry. new_image = "#{base_image}:#{tag}" execute_command(["docker", "tag", base_image, new_image]) new_image end |