Class: Kamal::Dev::Registry

Inherits:
Object
  • Object
show all
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

Examples:

Basic usage

registry = Kamal::Dev::Registry.new(config)
image = registry.image_name("myapp")
# => "ghcr.io/ljuti/myapp-dev"

With tag

registry.image_tag("myapp", "abc123")
# => "ghcr.io/ljuti/myapp-dev:abc123"

Docker login

registry.
# => ["docker", "login", "ghcr.io", "-u", "ljuti", "-p", "token"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Registry

Initialize registry with configuration

Parameters:

  • Configuration object



34
35
36
# File 'lib/kamal/dev/registry.rb', line 34

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (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

Returns:

  • true if username and password are set



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"

Parameters:

  • Image reference from config.image

Returns:

  • Full image name with registry



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

Deprecated.

Use full_image_name(config.image) instead

Generate image name without tag (DEPRECATED - kept for backward compatibility)

Parameters:

  • Service name (from config.service)

Returns:

  • Full image name without tag

Raises:

  • if username not configured



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

Parameters:

  • Base image name (can be full or short path)

  • Image tag (timestamp, git SHA, or custom)

Returns:

  • 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_commandArray<String>

Generate docker login command

Examples:

registry.
# => ["docker", "login", "ghcr.io", "-u", "ljuti", "-p", "token"]

Returns:

  • Docker login command array

Raises:

  • if credentials not configured



113
114
115
116
117
# File 'lib/kamal/dev/registry.rb', line 113

def 
  raise Kamal::Dev::RegistryError, "Registry credentials not configured" unless credentials_present?

  ["docker", "login", server, "-u", username, "-p", password]
end

#passwordString?

Registry password/token loaded from environment variable

Returns:

  • Password from ENV



55
56
57
# File 'lib/kamal/dev/registry.rb', line 55

def password
  config.registry_password
end

#serverString

Registry server URL

Returns:

  • Registry server (default: ghcr.io)



41
42
43
# File 'lib/kamal/dev/registry.rb', line 41

def server
  config.registry_server
end

#tag_with_git_shaString?

Generate git SHA-based tag

Format: short_sha (e.g., "abc123f")

Returns:

  • Short git commit SHA or nil if git not available



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_timestampString

Generate timestamp-based tag

Format: unix_timestamp (e.g., "1700000000")

Returns:

  • Unix timestamp tag



131
132
133
# File 'lib/kamal/dev/registry.rb', line 131

def tag_with_timestamp
  Time.now.to_i.to_s
end

#usernameString?

Registry username loaded from environment variable

Returns:

  • Username from ENV



48
49
50
# File 'lib/kamal/dev/registry.rb', line 48

def username
  config.registry_username
end