Class: Kamal::Dev::Registry
- Inherits:
-
Object
- Object
- Kamal::Dev::Registry
- Defined in:
- lib/kamal/dev/registry.rb
Overview
Registry configuration and authentication for image building and pushing
Provides methods for:
- Registry server configuration (default: ghcr.io)
- Credential loading from environment variables
- Image naming conventions (registry/user/service-dev:tag)
- Docker login command generation
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#credentials_present? ⇒ Boolean
Check if credentials are present.
-
#full_image_name(image_ref) ⇒ String
Get full image name with registry server prepended if needed.
-
#image_name(service) ⇒ String
deprecated
Deprecated.
Use full_image_name(config.image) instead
-
#image_tag(image_base, tag) ⇒ String
Generate full image reference with tag.
-
#initialize(config) ⇒ Registry
constructor
Initialize registry with configuration.
-
#login_command ⇒ Array<String>
Generate docker login command.
-
#password ⇒ String?
Registry password/token loaded from environment variable.
-
#server ⇒ String
Registry server URL.
-
#tag_with_git_sha ⇒ String?
Generate git SHA-based tag.
-
#tag_with_timestamp ⇒ String
Generate timestamp-based tag.
-
#username ⇒ String?
Registry username loaded from environment variable.
Constructor Details
#initialize(config) ⇒ Registry
Initialize registry with configuration
34 35 36 |
# File 'lib/kamal/dev/registry.rb', line 34 def initialize(config) @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
29 30 31 |
# File 'lib/kamal/dev/registry.rb', line 29 def config @config end |
Instance Method Details
#credentials_present? ⇒ Boolean
Check if credentials are present
122 123 124 |
# File 'lib/kamal/dev/registry.rb', line 122 def credentials_present? !!(username && password) end |
#full_image_name(image_ref) ⇒ String
Get full image name with registry server prepended if needed
Supports both patterns:
- Full path: "ghcr.io/org/app" → "ghcr.io/org/app"
- Short path: "org/app" → "ghcr.io/org/app" (registry prepended)
- Name only: "app" → "ghcr.io/app"
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/kamal/dev/registry.rb', line 68 def full_image_name(image_ref) # Check if image already includes a registry # Registry indicators: has a . (ghcr.io) or : (localhost:5000) in first component first_component = image_ref.split("/").first if first_component.include?(".") || first_component.include?(":") # Already has registry: "ghcr.io/org/app", "docker.io/library/ruby", "localhost:5000/app" image_ref else # No registry: "org/app" or "app" - prepend registry server "#{server}/#{image_ref}" end end |
#image_name(service) ⇒ String
Use full_image_name(config.image) instead
Generate image name without tag (DEPRECATED - kept for backward compatibility)
88 89 90 91 92 |
# File 'lib/kamal/dev/registry.rb', line 88 def image_name(service) raise Kamal::Dev::RegistryError, "Registry username not configured" unless username "#{server}/#{username}/#{service}-dev" end |
#image_tag(image_base, tag) ⇒ String
Generate full image reference with tag
99 100 101 102 103 104 |
# File 'lib/kamal/dev/registry.rb', line 99 def image_tag(image_base, tag) base = (image_base.is_a?(String) && (image_base.include?("/") || image_base.include?("."))) ? full_image_name(image_base) : image_name(image_base) # Backward compatibility "#{base}:#{tag}" end |
#login_command ⇒ Array<String>
Generate docker login command
113 114 115 116 117 |
# File 'lib/kamal/dev/registry.rb', line 113 def login_command raise Kamal::Dev::RegistryError, "Registry credentials not configured" unless credentials_present? ["docker", "login", server, "-u", username, "-p", password] end |
#password ⇒ String?
Registry password/token loaded from environment variable
55 56 57 |
# File 'lib/kamal/dev/registry.rb', line 55 def password config.registry_password end |
#server ⇒ String
Registry server URL
41 42 43 |
# File 'lib/kamal/dev/registry.rb', line 41 def server config.registry_server end |
#tag_with_git_sha ⇒ String?
Generate git SHA-based tag
Format: short_sha (e.g., "abc123f")
140 141 142 143 144 145 146 |
# File 'lib/kamal/dev/registry.rb', line 140 def tag_with_git_sha sha, _status = Open3.capture2("git", "rev-parse", "--short", "HEAD", err: :close) sha = sha.strip sha.empty? ? nil : sha rescue nil end |
#tag_with_timestamp ⇒ String
Generate timestamp-based tag
Format: unix_timestamp (e.g., "1700000000")
131 132 133 |
# File 'lib/kamal/dev/registry.rb', line 131 def Time.now.to_i.to_s end |
#username ⇒ String?
Registry username loaded from environment variable
48 49 50 |
# File 'lib/kamal/dev/registry.rb', line 48 def username config.registry_username end |